diff --git a/js/mapbox-gl.js b/js/mapbox-gl.js index 7618b57e3bd..c0e6ceeebe4 100644 --- a/js/mapbox-gl.js +++ b/js/mapbox-gl.js @@ -1,9 +1,13 @@ 'use strict'; +var browser = require('./util/browser'); + // jshint -W079 var mapboxgl = module.exports = {}; mapboxgl.version = require('../package.json').version; +mapboxgl.workerCount = Math.max(browser.hardwareConcurrency - 1, 1); + mapboxgl.Map = require('./ui/map'); mapboxgl.Control = require('./ui/control/control'); diff --git a/js/util/worker_pool.js b/js/util/worker_pool.js index 3ac9e1b0e0f..47724a580e0 100644 --- a/js/util/worker_pool.js +++ b/js/util/worker_pool.js @@ -2,23 +2,28 @@ var assert = require('assert'); var WebWorker = require('./web_worker'); -var browser = require('./browser'); module.exports = WorkerPool; -WorkerPool.WORKER_COUNT = Math.max(browser.hardwareConcurrency - 1, 1); - +/** + * Constructs a worker pool. + * @private + */ function WorkerPool() { - this.workerCount = WorkerPool.WORKER_COUNT; this.active = {}; } WorkerPool.prototype = { acquire: function (mapId) { if (!this.workers) { - assert(typeof this.workerCount === 'number'); + // Lazily look up the value of mapboxgl.workerCount. This allows + // client code a chance to set it while circumventing cyclic + // dependency problems + var workerCount = require('../mapbox-gl').workerCount; + assert(typeof workerCount === 'number' && workerCount < Infinity); + this.workers = []; - while (this.workers.length < this.workerCount) { + while (this.workers.length < workerCount) { this.workers.push(new WebWorker()); } } diff --git a/test/js/mapbox-gl.js b/test/js/mapbox-gl.js index efcda29fa37..c85a6d7f511 100644 --- a/test/js/mapbox-gl.js +++ b/test/js/mapbox-gl.js @@ -1,6 +1,7 @@ 'use strict'; var test = require('tap').test; +var proxyquire = require('proxyquire'); var mapboxgl = require('../../../js/mapbox-gl'); test('mapboxgl', function(t) { @@ -8,5 +9,13 @@ test('mapboxgl', function(t) { t.ok(mapboxgl.version); t.end(); }); + + t.test('.workerCount defaults to hardwareConcurrency - 1', function (t) { + var mapboxgl = proxyquire('../../../js/mapbox-gl', { + './util/browser': { hardwareConcurrency: 15 } + }); + t.equal(mapboxgl.workerCount, 14); + t.end(); + }); t.end(); }); diff --git a/test/js/util/dispatcher.test.js b/test/js/util/dispatcher.test.js index 328ce02a01a..193b0bb2a53 100644 --- a/test/js/util/dispatcher.test.js +++ b/test/js/util/dispatcher.test.js @@ -4,7 +4,6 @@ var test = require('tap').test; var proxyquire = require('proxyquire'); var Dispatcher = require('../../../js/util/dispatcher'); var WebWorker = require('../../../js/util/web_worker'); -var WorkerPool = require('../../../js/util/worker_pool'); test('Dispatcher', function (t) { t.test('requests and releases workers from pool', function (t) { @@ -31,19 +30,18 @@ test('Dispatcher', function (t) { }); test('creates Actors with unique map id', function (t) { - var Dispatcher = proxyquire('../../../js/util/dispatcher', { './actor': Actor }); + var Dispatcher = proxyquire('../../../js/util/dispatcher', {'./actor': Actor }); + var WorkerPool = proxyquire('../../../js/util/worker_pool', { + '../mapbox-gl': { workerCount: 1 } + }); var ids = []; function Actor (target, parent, mapId) { ids.push(mapId); } - var previousWorkerCount = WorkerPool.WORKER_COUNT; - WorkerPool.WORKER_COUNT = 1; - var workerPool = new WorkerPool(); var dispatchers = [new Dispatcher(workerPool, {}), new Dispatcher(workerPool, {})]; t.same(ids, dispatchers.map(function (d) { return d.id; })); - WorkerPool.WORKER_COUNT = previousWorkerCount; t.end(); }); diff --git a/test/js/util/worker_pool.test.js b/test/js/util/worker_pool.test.js index a1a0a9bb8ee..63700a51705 100644 --- a/test/js/util/worker_pool.test.js +++ b/test/js/util/worker_pool.test.js @@ -2,30 +2,20 @@ var test = require('tap').test; var proxyquire = require('proxyquire'); -var WorkerPool = require('../../../js/util/worker_pool'); test('WorkerPool', function (t) { - t.test('.WORKER_COUNT', function (t) { + t.test('#acquire', function (t) { var WorkerPool = proxyquire('../../../js/util/worker_pool', { - './browser': { hardwareConcurrency: 15 } + '../mapbox-gl': { workerCount: 4 } }); - t.equal(WorkerPool.WORKER_COUNT, 14); - - WorkerPool.WORKER_COUNT = 4; - t.end(); - }); - - t.test('#acquire', function (t) { - // make sure we're actually creating some workers - t.ok(WorkerPool.WORKER_COUNT > 0); var pool = new WorkerPool(); t.notOk(pool.workers); var workers1 = pool.acquire('map-1'); var workers2 = pool.acquire('map-2'); - t.equal(workers1.length, WorkerPool.WORKER_COUNT); - t.equal(workers2.length, WorkerPool.WORKER_COUNT); + t.equal(workers1.length, 4); + t.equal(workers2.length, 4); // check that the two different dispatchers' workers arrays correspond workers1.forEach(function (w, i) { t.equal(w, workers2[i]); }); @@ -33,6 +23,10 @@ test('WorkerPool', function (t) { }); t.test('#release', function (t) { + var WorkerPool = proxyquire('../../../js/util/worker_pool', { + '../mapbox-gl': { workerCount: 4 } + }); + var pool = new WorkerPool(); pool.acquire('map-1'); var workers = pool.acquire('map-2'); @@ -48,7 +42,7 @@ test('WorkerPool', function (t) { t.comment('terminates workers if no dispatchers are active'); pool.release('map-1'); - t.equal(terminated, WorkerPool.WORKER_COUNT); + t.equal(terminated, 4); t.notOk(pool.workers); t.end();