Skip to content

Commit

Permalink
Move worker source registration to Source.addType
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Aug 24, 2016
1 parent f4a1a8d commit a05ee84
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 48 deletions.
30 changes: 25 additions & 5 deletions js/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var Evented = require('../util/evented');
var util = require('../util/util');
var Dispatcher = require('../util/dispatcher');
var getWorkerPool = require('../global_worker_pool');

var sourceTypes = {
'vector': require('../source/vector_tile_source'),
Expand Down Expand Up @@ -56,22 +58,40 @@ Source.getCustomTypeNames = function () {
/**
* Adds a [custom source type](#Custom Sources), making it available for use with
* {@link Map#addSource}.
* @private
*
* @param {string} name The name of the source type; source definition objects use this name in the `{type: ...}` field.
* @param {Function} SourceType A {@link Source} constructor.
* @param {Function} callback called after SourceType has been added and, if relevant, its worker code has been sent to the workers.
* @private
*/
Source.addType = function (name, SourceType) {
Source.addType = function (name, SourceType, callback) {
if (Source.getType(name)) {
throw new Error('A source type named ' + name + ' already exists.');
}

Source.setType(name, SourceType);

// an internal event, used to notify Style instances that there is a new
// custom source type.
Source.fire('_add', { name: name });
if (SourceType.workerSourceURL) {
getDispatcher().broadcast('load worker source', {
name: name,
url: SourceType.workerSourceURL
}, function (err) {
callback(err);
});
} else {
callback();
}
};

// A Dispatcher instance for use in registering custom worker sources.
var dispatcher;
function getDispatcher () {
if (!dispatcher) {
dispatcher = new Dispatcher(getWorkerPool(), {});
}
return dispatcher;
}


/**
* 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
2 changes: 1 addition & 1 deletion js/source/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Worker(self) {
this.workerSources = {};

this.self.registerWorkerSource = function (name, WorkerSource) {
if (this.workerSources[name]) {
if (this.workerSourceTypes[name]) {
util.warnOnce('Worker source named "' + name + '" already registered.');
}
this.workerSourceTypes[name] = WorkerSource;
Expand Down
48 changes: 6 additions & 42 deletions js/style/style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'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 @@ -39,15 +38,11 @@ function Style(stylesheet, animationLoop) {
'_forwardSourceEvent',
'_forwardTileEvent',
'_forwardLayerEvent',
'_redoPlacement',
'_handleAddSourceType',
'_registerCustomSource'
'_redoPlacement'
], this);

this._resetUpdates();

Source.on('_add', this._handleAddSourceType);

var stylesheetLoaded = function(err, stylesheet) {
if (err) {
this.fire('error', {error: err});
Expand Down Expand Up @@ -76,19 +71,11 @@ function Style(stylesheet, animationLoop) {
this.fire('load');
}.bind(this);

// register any existing custom source types with the workers
util.asyncAll(Source.getCustomTypeNames(), this._registerCustomSource, 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));
if (typeof stylesheet === 'string') {
ajax.getJSON(normalizeURL(stylesheet), stylesheetLoaded);
} else {
browser.frame(stylesheetLoaded.bind(this, null, stylesheet));
}

this.on('source.load', function(event) {
var source = event.source;
Expand Down Expand Up @@ -694,29 +681,6 @@ Style.prototype = util.inherit(Evented, {
return action.call(validateStyle, this, result);
},

_handleAddSourceType: function (event) {
this._registerCustomSource(event.name, function (err) {
if (err) {
this.fire('error', {error: err});
return;
}
this.fire('source-type.add', event);
}.bind(this));
},

_registerCustomSource: function (name, callback) {
var SourceType = Source.getType(name);
assert(SourceType);
if (SourceType.workerSourceURL) {
this.dispatcher.broadcast('load worker source', {
name: name,
url: SourceType.workerSourceURL
}, callback);
} else {
callback();
}
},

_remove: function() {
this.dispatcher.remove();
Source.off('_add', this._handleAddSourceType);
Expand Down

0 comments on commit a05ee84

Please sign in to comment.