Skip to content

Commit

Permalink
Register existing custom sources before style load
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Aug 15, 2016
1 parent f9d3ea6 commit a5ccc4f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
12 changes: 12 additions & 0 deletions js/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var sourceTypes = {
'image': require('../source/image_source')
};

var coreTypes = ['vector', 'raster', 'geojson', 'video', 'image'];

/*
* Creates a tiled data source instance given an options object.
*
Expand Down Expand Up @@ -38,6 +40,16 @@ exports.setType = function (name, type) {
sourceTypes[name] = type;
};

/**
* Returns the names of any registered non-core source types.
* @private
*/
exports.getCustomTypeNames = function () {
return Object.keys(sourceTypes).filter(function (type) {
return coreTypes.indexOf(type) < 0;
});
};

/**
* The `Source` interface must be implemented by each source type, including "core" types (`vector`, `raster`, `video`, etc.) and all custom, third-party types.
*
Expand Down
32 changes: 27 additions & 5 deletions js/style/style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var assert = require('assert');
var Evented = require('../util/evented');
var StyleLayer = require('./style_layer');
var ImageSprite = require('./image_sprite');
Expand Down Expand Up @@ -70,11 +71,32 @@ function Style(stylesheet, animationLoop, workerCount) {
this.fire('load');
}.bind(this);

if (typeof stylesheet === 'string') {
ajax.getJSON(normalizeURL(stylesheet), stylesheetLoaded);
} else {
browser.frame(stylesheetLoaded.bind(this, null, stylesheet));
}
// register any existing custom source types with the workers
util.asyncAll(Source.getCustomTypeNames(), function (type, done) {
var CustomSource = Source.getType(type);
assert(CustomSource);

if (CustomSource.workerSourceURL) {
this.dispatcher.broadcast('load worker source', {
name: type,
url: CustomSource.workerSourceURL
}, done);
} else {
return done();
}
}.bind(this), function (err) {
if (err) {
this.fire('error', {error: err});
return;
}

if (typeof stylesheet === 'string') {
ajax.getJSON(normalizeURL(stylesheet), stylesheetLoaded);
} else {
browser.frame(stylesheetLoaded.bind(this, null, stylesheet));
}
}.bind(this));


this.on('source.load', function(event) {
var source = event.source;
Expand Down
33 changes: 31 additions & 2 deletions test/js/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ test('Style', function(t) {
});
});

t.test('registers existing custom sources', function (t) {
function SourceType () {}
SourceType.workerSourceURL = 'worker-source.js';
function Dispatcher () {}
Dispatcher.prototype = {
broadcast: function (type, params, callback) {
if (type === 'load worker source') {
t.equal(params.name, 'my-source-type');
t.equal(params.url, 'worker-source.js');
setTimeout(callback, 0);
}
}
};

t.plan(2);

var Style = proxyquire('../../../js/style/style', {
'../source/source': {
getType: function () { return SourceType; },
setType: function () {},
getCustomTypeNames: function () { return ['my-source-type']; }
},
'../util/dispatcher': Dispatcher
});

new Style(createStyleJSON());
});

t.end();
});

Expand Down Expand Up @@ -1184,11 +1212,12 @@ test('Style#addSourceType', function (t) {
var Style = proxyquire('../../../js/style/style', {
'../source/source': {
getType: function (name) { return _types[name]; },
setType: function (name, create) { _types[name] = create; }
setType: function (name, create) { _types[name] = create; },
getTypeNames: function () { return []; }
}
});

t.test('adds factory function', function (t) {
t.test('adds source type', function (t) {
var style = new Style(createStyleJSON());
var SourceType = function () {};

Expand Down

0 comments on commit a5ccc4f

Please sign in to comment.