From 202ad9c97d0be75d0b75d287ed0e20a640a06762 Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Wed, 17 Aug 2016 18:08:57 -0400 Subject: [PATCH] Move the static worker pool instance to style.js This is still not great, but it's better than having it in Dispatcher. Ultimately the right place to put it is at the root level, i.e. mapbox-gl.js, and then pass it through, but that will be a noisy change due the number of 'new Map()' and 'new Style()' in the unit tests --- js/style/style.js | 5 ++++- js/util/dispatcher.js | 9 ++++----- test/js/util/dispatcher.test.js | 19 +++++-------------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/js/style/style.js b/js/style/style.js index 85497c89a83..dc9f98f1adc 100644 --- a/js/style/style.js +++ b/js/style/style.js @@ -18,12 +18,15 @@ var QueryFeatures = require('../source/query_features'); var SourceCache = require('../source/source_cache'); var styleSpec = require('./style_spec'); var StyleFunction = require('./style_function'); +var WorkerPool = require('../util/worker_pool'); + +var workerPool = new WorkerPool(); module.exports = Style; function Style(stylesheet, animationLoop, workerCount) { this.animationLoop = animationLoop || new AnimationLoop(); - this.dispatcher = new Dispatcher(workerCount || 1, this); + this.dispatcher = new Dispatcher(workerPool, workerCount || 1, this); this.spriteAtlas = new SpriteAtlas(1024, 1024); this.lineAtlas = new LineAtlas(256, 512); diff --git a/js/util/dispatcher.js b/js/util/dispatcher.js index 1515734f26b..c12a11724ba 100644 --- a/js/util/dispatcher.js +++ b/js/util/dispatcher.js @@ -1,11 +1,9 @@ 'use strict'; -var WorkerPool = require('./worker_pool'); var util = require('./util'); var Actor = require('./actor'); module.exports = Dispatcher; -Dispatcher.workerPool = new WorkerPool(); /** * Responsible for sending messages from a {@link Source} to an associated @@ -14,11 +12,12 @@ Dispatcher.workerPool = new WorkerPool(); * @interface Dispatcher * @private */ -function Dispatcher(length, parent) { +function Dispatcher(workerPool, length, parent) { + this.workerPool = workerPool; this.actors = []; this.currentActor = 0; this.id = util.uniqueId(); - var workers = Dispatcher.workerPool.acquire(this.id, length); + var workers = this.workerPool.acquire(this.id, length); for (var i = 0; i < workers.length; i++) { var worker = workers[i]; var actor = new Actor(worker, parent, this.id); @@ -68,7 +67,7 @@ Dispatcher.prototype = { }, remove: function() { - Dispatcher.workerPool.release(this.id); + this.workerPool.release(this.id); this.actors = []; } }; diff --git a/test/js/util/dispatcher.test.js b/test/js/util/dispatcher.test.js index 49b603ec759..bb71257c661 100644 --- a/test/js/util/dispatcher.test.js +++ b/test/js/util/dispatcher.test.js @@ -4,7 +4,7 @@ var test = require('tap').test; var proxyquire = require('proxyquire'); var Dispatcher = require('../../../js/util/dispatcher'); var WebWorker = require('../../../js/util/web_worker'); -var originalWorkerPool = Dispatcher.workerPool; +var WorkerPool = require('../../../js/util/worker_pool'); test('Dispatcher', function (t) { t.test('requests and releases workers from pool', function (t) { @@ -12,7 +12,7 @@ test('Dispatcher', function (t) { var workers = [new WebWorker(), new WebWorker()]; var releaseCalled = []; - Dispatcher.workerPool = { + var workerPool = { acquire: function (id, count) { t.equal(count, 2); return workers; @@ -22,13 +22,12 @@ test('Dispatcher', function (t) { } }; - dispatcher = new Dispatcher(2, {}); + dispatcher = new Dispatcher(workerPool, 2, {}); t.same(dispatcher.actors.map(function (actor) { return actor.target; }), workers); dispatcher.remove(); t.equal(dispatcher.actors.length, 0, 'actors discarded'); t.same(releaseCalled, [dispatcher.id]); - restoreWorkerPool(); t.end(); }); @@ -38,7 +37,8 @@ test('Dispatcher', function (t) { var ids = []; function Actor (target, parent, mapId) { ids.push(mapId); } - var dispatchers = [new Dispatcher(1, {}), new Dispatcher(1, {})]; + var workerPool = new WorkerPool(); + var dispatchers = [new Dispatcher(workerPool, 1, {}), new Dispatcher(workerPool, 1, {})]; t.same(ids, dispatchers.map(function (d) { return d.id; })); t.end(); @@ -47,12 +47,3 @@ test('Dispatcher', function (t) { t.end(); }); -test('after', function (t) { - restoreWorkerPool(); - t.end(); -}); - -function restoreWorkerPool () { - Dispatcher.workerPool = originalWorkerPool; -} -