From 85097c3ec03abcf27da78c37ac3a2a1bb651c20c Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Wed, 19 Oct 2016 19:44:30 +0300 Subject: [PATCH] convert Worker to ES6 class syntax, camelCase worker callbacks --- js/source/geojson_source.js | 4 +- js/source/tile.js | 2 +- js/source/vector_tile_source.js | 8 +-- js/source/worker.js | 85 +++++++++++------------ js/style/style.js | 4 +- js/util/actor.js | 2 +- js/util/web_worker.js | 2 +- test/js/source/vector_tile_source.test.js | 6 +- test/js/source/worker.test.js | 10 +-- test/js/style/style.test.js | 18 ++--- test/js/ui/map.test.js | 4 +- 11 files changed, 72 insertions(+), 73 deletions(-) diff --git a/js/source/geojson_source.js b/js/source/geojson_source.js index f01348cf88a..319b3c82041 100644 --- a/js/source/geojson_source.js +++ b/js/source/geojson_source.js @@ -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(); @@ -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() { diff --git a/js/source/tile.js b/js/source/tile.js index 176ac2b45c9..e3aa23ffc1e 100644 --- a/js/source/tile.js +++ b/js/source/tile.js @@ -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, diff --git a/js/source/vector_tile_source.js b/js/source/vector_tile_source.js index ca71f4c3344..1dda7066702 100644 --- a/js/source/vector_tile_source.js +++ b/js/source/vector_tile_source.js @@ -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) { @@ -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); } } diff --git a/js/source/worker.js b/js/source/worker.js index 086bed385d1..fff70139f19 100644 --- a/js/source/worker.js +++ b/js/source/worker.js @@ -2,71 +2,66 @@ 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 @@ -74,24 +69,24 @@ util.extend(Worker.prototype, { * 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]) { @@ -108,4 +103,8 @@ util.extend(Worker.prototype, { return this.workerSources[mapId][type]; } -}); +} + +module.exports = function createWorker(self) { + return new Worker(self); +}; diff --git a/js/style/style.js b/js/style/style.js index 20a249e2784..6682c64d1b4 100644 --- a/js/style/style.js +++ b/js/style/style.js @@ -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) { @@ -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); diff --git a/js/util/actor.js b/js/util/actor.js index 6191c766927..4bfc621ba82 100644 --- a/js/util/actor.js +++ b/js/util/actor.js @@ -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 diff --git a/js/util/web_worker.js b/js/util/web_worker.js index f3495acaa93..11438afd64c 100644 --- a/js/util/web_worker.js +++ b/js/util/web_worker.js @@ -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); diff --git a/test/js/source/vector_tile_source.test.js b/test/js/source/vector_tile_source.test.js index 9cf046e8325..0dc2e15e43f 100644 --- a/test/js/source/vector_tile_source.test.js +++ b/test/js/source/vector_tile_source.test.js @@ -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(); }; @@ -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(); }); }); diff --git a/test/js/source/worker.test.js b/test/js/source/worker.test.js index 37e7b6ea6d7..24e89acb77d 100644 --- a/test/js/source/worker.test.js +++ b/test/js/source/worker.test.js @@ -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, @@ -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' }, ]); @@ -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'}); }); diff --git a/test/js/style/style.test.js b/test/js/style/style.test.js index dc02b9083a5..0656325b985 100644 --- a/test/js/style/style.test.js +++ b/test/js/style/style.test.js @@ -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(); }; @@ -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(); }; @@ -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(); @@ -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(); @@ -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']); }; @@ -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']); }; @@ -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']); }; @@ -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(); } }; @@ -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'); diff --git a/test/js/ui/map.test.js b/test/js/ui/map.test.js index 4b6c43bfd53..02ab9957afe 100755 --- a/test/js/ui/map.test.js +++ b/test/js/ui/map.test.js @@ -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']); }; @@ -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']); };