Skip to content

Commit

Permalink
Move the static worker pool instance to style.js
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Anand Thakker committed Aug 17, 2016
1 parent 44d23b5 commit 202ad9c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
5 changes: 4 additions & 1 deletion js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 4 additions & 5 deletions js/util/dispatcher.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -68,7 +67,7 @@ Dispatcher.prototype = {
},

remove: function() {
Dispatcher.workerPool.release(this.id);
this.workerPool.release(this.id);
this.actors = [];
}
};
Expand Down
19 changes: 5 additions & 14 deletions test/js/util/dispatcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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) {
var dispatcher;
var workers = [new WebWorker(), new WebWorker()];

var releaseCalled = [];
Dispatcher.workerPool = {
var workerPool = {
acquire: function (id, count) {
t.equal(count, 2);
return workers;
Expand All @@ -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();
});

Expand All @@ -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();
Expand All @@ -47,12 +47,3 @@ test('Dispatcher', function (t) {
t.end();
});

test('after', function (t) {
restoreWorkerPool();
t.end();
});

function restoreWorkerPool () {
Dispatcher.workerPool = originalWorkerPool;
}

0 comments on commit 202ad9c

Please sign in to comment.