Skip to content

Commit

Permalink
convert Worker to ES6 class syntax, camelCase worker callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Oct 19, 2016
1 parent e7df00f commit d261612
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 73 deletions.
4 changes: 2 additions & 2 deletions js/source/geojson_source.js
Expand Up @@ -174,7 +174,7 @@ class GeoJSONSource extends Evented {
showCollisionBoxes: this.map.showCollisionBoxes
};

tile.workerID = this.dispatcher.send('load tile', params, (err, data) => {
tile.workerID = this.dispatcher.send('loadTile', params, (err, data) => {

tile.unloadVectorData();

Expand Down Expand Up @@ -203,7 +203,7 @@ class GeoJSONSource extends Evented {

unloadTile(tile) {
tile.unloadVectorData();
this.dispatcher.send('remove tile', { uid: tile.uid, type: this.type, source: this.id }, () => {}, tile.workerID);
this.dispatcher.send('removeTile', { uid: tile.uid, type: this.type, source: this.id }, () => {}, tile.workerID);
}

serialize() {
Expand Down
2 changes: 1 addition & 1 deletion js/source/tile.js
Expand Up @@ -127,7 +127,7 @@ class Tile {

this.state = 'reloading';

source.dispatcher.send('redo placement', {
source.dispatcher.send('redoPlacement', {
type: source.type,
uid: this.uid,
source: source.id,
Expand Down
8 changes: 4 additions & 4 deletions js/source/vector_tile_source.js
Expand Up @@ -66,12 +66,12 @@ class VectorTileSource extends Evented {
};

if (!tile.workerID) {
tile.workerID = this.dispatcher.send('load tile', params, done.bind(this));
tile.workerID = this.dispatcher.send('loadTile', params, done.bind(this));
} else if (tile.state === 'loading') {
// schedule tile reloading after it has been loaded
tile.reloadCallback = callback;
} else {
this.dispatcher.send('reload tile', params, done.bind(this), tile.workerID);
this.dispatcher.send('reloadTile', params, done.bind(this), tile.workerID);
}

function done(err, data) {
Expand Down Expand Up @@ -99,12 +99,12 @@ class VectorTileSource extends Evented {
}

abortTile(tile) {
this.dispatcher.send('abort tile', { uid: tile.uid, type: this.type, source: this.id }, null, tile.workerID);
this.dispatcher.send('abortTile', { uid: tile.uid, type: this.type, source: this.id }, null, tile.workerID);
}

unloadTile(tile) {
tile.unloadVectorData();
this.dispatcher.send('remove tile', { uid: tile.uid, type: this.type, source: this.id }, null, tile.workerID);
this.dispatcher.send('removeTile', { uid: tile.uid, type: this.type, source: this.id }, null, tile.workerID);
}
}

Expand Down
85 changes: 42 additions & 43 deletions js/source/worker.js
Expand Up @@ -2,96 +2,91 @@

const Actor = require('../util/actor');
const StyleLayerIndex = require('../style/style_layer_index');
const util = require('../util/util');

const VectorTileWorkerSource = require('./vector_tile_worker_source');
const GeoJSONWorkerSource = require('./geojson_worker_source');
const assert = require('assert');

module.exports = function createWorker(self) {
return new Worker(self);
};
class Worker {
constructor(self) {
this.self = self;
this.actor = new Actor(self, this);

function Worker(self) {
this.self = self;
this.actor = new Actor(self, this);
this.layerIndexes = {};

this.layerIndexes = {};
this.workerSourceTypes = {
vector: VectorTileWorkerSource,
geojson: GeoJSONWorkerSource
};

this.workerSourceTypes = {
vector: VectorTileWorkerSource,
geojson: GeoJSONWorkerSource
};
// [mapId][sourceType] => worker source instance
this.workerSources = {};

// [mapId][sourceType] => worker source instance
this.workerSources = {};

this.self.registerWorkerSource = function (name, WorkerSource) {
if (this.workerSourceTypes[name]) {
throw new Error(`Worker source with name "${name}" already registered.`);
}
this.workerSourceTypes[name] = WorkerSource;
}.bind(this);
}
this.self.registerWorkerSource = (name, WorkerSource) => {
if (this.workerSourceTypes[name]) {
throw new Error(`Worker source with name "${name}" already registered.`);
}
this.workerSourceTypes[name] = WorkerSource;
};
}

util.extend(Worker.prototype, {
'set layers': function(mapId, layerDefinitions) {
setLayers(mapId, layerDefinitions) {
this.getLayerIndex(mapId).replace(layerDefinitions);
},
}

'update layers': function(mapId, layerDefinitions) {
updateLayers(mapId, layerDefinitions) {
this.getLayerIndex(mapId).update(layerDefinitions);
},
}

'load tile': function(mapId, params, callback) {
loadTile(mapId, params, callback) {
assert(params.type);
this.getWorkerSource(mapId, params.type).loadTile(params, callback);
},
}

'reload tile': function(mapId, params, callback) {
reloadTile(mapId, params, callback) {
assert(params.type);
this.getWorkerSource(mapId, params.type).reloadTile(params, callback);
},
}

'abort tile': function(mapId, params) {
abortTile(mapId, params) {
assert(params.type);
this.getWorkerSource(mapId, params.type).abortTile(params);
},
}

'remove tile': function(mapId, params) {
removeTile(mapId, params) {
assert(params.type);
this.getWorkerSource(mapId, params.type).removeTile(params);
},
}

'redo placement': function(mapId, params, callback) {
redoPlacement(mapId, params, callback) {
assert(params.type);
this.getWorkerSource(mapId, params.type).redoPlacement(params, callback);
},
}

/**
* Load a {@link WorkerSource} script at params.url. The script is run
* (using importScripts) with `registerWorkerSource` in scope, which is a
* function taking `(name, workerSourceObject)`.
* @private
*/
'load worker source': function(map, params, callback) {
loadWorkerSource(map, params, callback) {
try {
this.self.importScripts(params.url);
callback();
} catch (e) {
callback(e);
}
},
}

getLayerIndex: function(mapId) {
getLayerIndex(mapId) {
let layerIndexes = this.layerIndexes[mapId];
if (!layerIndexes) {
layerIndexes = this.layerIndexes[mapId] = new StyleLayerIndex();
}
return layerIndexes;
},
}

getWorkerSource: function(mapId, type) {
getWorkerSource(mapId, type) {
if (!this.workerSources[mapId])
this.workerSources[mapId] = {};
if (!this.workerSources[mapId][type]) {
Expand All @@ -108,4 +103,8 @@ util.extend(Worker.prototype, {

return this.workerSources[mapId][type];
}
});
}

module.exports = function createWorker(self) {
return new Worker(self);
};
4 changes: 2 additions & 2 deletions js/style/style.js
Expand Up @@ -185,7 +185,7 @@ class Style extends Evented {
}

_updateWorkerLayers(ids) {
this.dispatcher.broadcast(ids ? 'update layers' : 'set layers', this._serializeLayers(ids));
this.dispatcher.broadcast(ids ? 'updateLayers' : 'setLayers', this._serializeLayers(ids));
}

_serializeLayers(ids) {
Expand Down Expand Up @@ -679,7 +679,7 @@ class Style extends Evented {
return callback(null, null);
}

this.dispatcher.broadcast('load worker source', {
this.dispatcher.broadcast('loadWorkerSource', {
name: name,
url: SourceType.workerSourceURL
}, callback);
Expand Down
2 changes: 1 addition & 1 deletion js/util/actor.js
Expand Up @@ -68,7 +68,7 @@ class Actor {
delete this.callbacks[data.id];
if (callback) callback(data.error || null, data.data);
} else if (typeof data.id !== 'undefined' && this.parent[data.type]) {
// data.type == 'load tile', 'remove tile', etc.
// data.type == 'loadTile', 'removeTile', etc.
this.parent[data.type](data.sourceMapId, data.data, done);
} else if (typeof data.id !== 'undefined' && this.parent.getWorkerSource) {
// data.type == sourcetype.method
Expand Down
2 changes: 1 addition & 1 deletion js/util/web_worker.js
Expand Up @@ -11,7 +11,7 @@ module.exports = function () {
parentBus.target = workerBus;
workerBus.target = parentBus;
// workerBus substitutes the WebWorker global `self`, and Worker uses
// self.importScripts for the 'load worker source' target.
// self.importScripts for the 'loadWorkerSource' target.
workerBus.importScripts = function () {};

new Worker(workerBus);
Expand Down
6 changes: 3 additions & 3 deletions test/js/source/vector_tile_source.test.js
Expand Up @@ -118,7 +118,7 @@ test('VectorTileSource', (t) => {
});

source.dispatcher.send = function(type, params) {
t.equal(type, 'load tile');
t.equal(type, 'loadTile');
t.equal(expectedURL, params.url);
t.end();
};
Expand Down Expand Up @@ -149,13 +149,13 @@ test('VectorTileSource', (t) => {
state: 'loading',
loadVectorData: function () {
this.state = 'loaded';
events.push('tile loaded');
events.push('tileLoaded');
}
};
source.loadTile(tile, () => {});
t.equal(tile.state, 'loading');
source.loadTile(tile, () => {
t.same(events, ['load tile', 'tile loaded', 'reload tile', 'tile loaded']);
t.same(events, ['loadTile', 'tileLoaded', 'reloadTile', 'tileLoaded']);
t.end();
});
});
Expand Down
10 changes: 5 additions & 5 deletions test/js/source/worker.test.js
Expand Up @@ -12,7 +12,7 @@ test('load tile', (t) => {
t.test('calls callback on error', (t) => {
window.useFakeXMLHttpRequest();
const worker = new Worker(_self);
worker['load tile'](0, {
worker.loadTile(0, {
type: 'vector',
source: 'source',
uid: 0,
Expand All @@ -37,17 +37,17 @@ test('redo placement', (t) => {
};
});

worker['redo placement'](0, {type: 'test', mapbox: true});
worker.redoPlacement(0, {type: 'test', mapbox: true});
});

test('isolates different instances\' data', (t) => {
const worker = new Worker(_self);

worker['set layers'](0, [
worker.setLayers(0, [
{ id: 'one', type: 'circle' }
]);

worker['set layers'](1, [
worker.setLayers(1, [
{ id: 'one', type: 'circle' },
{ id: 'two', type: 'circle' },
]);
Expand Down Expand Up @@ -75,5 +75,5 @@ test('worker source messages dispatched to the correct map instance', (t) => {
};
});

worker['load tile'](999, {type: 'test'});
worker.loadTile(999, {type: 'test'});
});
18 changes: 9 additions & 9 deletions test/js/style/style.test.js
Expand Up @@ -219,7 +219,7 @@ test('Style#_updateWorkerLayers', (t) => {
style.addLayer({id: 'third', source: 'source', type: 'fill', 'source-layer': 'source-layer' });

style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'set layers');
t.equal(key, 'setLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['first', 'second', 'third']);
t.end();
};
Expand Down Expand Up @@ -247,7 +247,7 @@ test('Style#_updateWorkerLayers with specific ids', (t) => {

style.on('style.load', () => {
style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['second', 'third']);
t.end();
};
Expand Down Expand Up @@ -827,7 +827,7 @@ test('Style#setFilter', (t) => {

style.on('style.load', () => {
style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value[0].id, 'symbol');
t.deepEqual(value[0].filter, ['==', 'id', 1]);
t.end();
Expand Down Expand Up @@ -865,7 +865,7 @@ test('Style#setFilter', (t) => {
style.update({}, {}); // flush pending operations

style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value[0].id, 'symbol');
t.deepEqual(value[0].filter, ['==', 'id', 2]);
t.end();
Expand All @@ -881,7 +881,7 @@ test('Style#setFilter', (t) => {

style.on('style.load', () => {
style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['symbol']);
};

Expand Down Expand Up @@ -939,7 +939,7 @@ test('Style#setLayerZoomRange', (t) => {

style.on('style.load', () => {
style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['symbol']);
};

Expand All @@ -955,7 +955,7 @@ test('Style#setLayerZoomRange', (t) => {

style.on('style.load', () => {
style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['symbol']);
};

Expand Down Expand Up @@ -1256,7 +1256,7 @@ test('Style#addSourceType', (t) => {

// expect no call to load worker source
style.dispatcher.broadcast = function (type) {
if (type === 'load worker source') {
if (type === 'loadWorkerSource') {
t.fail();
}
};
Expand All @@ -1273,7 +1273,7 @@ test('Style#addSourceType', (t) => {
SourceType.workerSourceURL = 'worker-source.js';

style.dispatcher.broadcast = function (type, params) {
if (type === 'load worker source') {
if (type === 'loadWorkerSource') {
t.equal(_types['bar'], SourceType);
t.equal(params.name, 'bar');
t.equal(params.url, 'worker-source.js');
Expand Down
4 changes: 2 additions & 2 deletions test/js/ui/map.test.js
Expand Up @@ -668,7 +668,7 @@ test('Map', (t) => {

map.on('style.load', () => {
map.style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['symbol']);
};

Expand Down Expand Up @@ -708,7 +708,7 @@ test('Map', (t) => {

map.on('style.load', () => {
map.style.dispatcher.broadcast = function(key, value) {
t.equal(key, 'update layers');
t.equal(key, 'updateLayers');
t.deepEqual(value.map((layer) => { return layer.id; }), ['symbol']);
};

Expand Down

0 comments on commit d261612

Please sign in to comment.