diff --git a/debug/index.html b/debug/index.html index d8d6e5e74a6..05c708336ce 100644 --- a/debug/index.html +++ b/debug/index.html @@ -58,7 +58,8 @@ zoom: 12.5, center: [-77.01866, 38.888], style: 'mapbox://styles/mapbox/streets-v8', - hash: true + hash: true, + maxWorkers: 3 }); map.addControl(new mapboxgl.Navigation()); diff --git a/js/style/style.js b/js/style/style.js index fa6ec4fc9a5..5716cfe491d 100644 --- a/js/style/style.js +++ b/js/style/style.js @@ -19,9 +19,9 @@ var StyleFunction = require('./style_function'); module.exports = Style; -function Style(stylesheet, animationLoop) { +function Style(stylesheet, animationLoop, maxWorkers) { this.animationLoop = animationLoop || new AnimationLoop(); - this.dispatcher = new Dispatcher(Math.max(browser.hardwareConcurrency - 1, 1), this); + this.dispatcher = new Dispatcher(maxWorkers || 1, this); this.spriteAtlas = new SpriteAtlas(512, 512); this.lineAtlas = new LineAtlas(256, 512); diff --git a/js/ui/map.js b/js/ui/map.js index f754f9dd27c..1021ad6f6c0 100755 --- a/js/ui/map.js +++ b/js/ui/map.js @@ -52,7 +52,8 @@ var defaultOptions = { failIfMajorPerformanceCaveat: false, preserveDrawingBuffer: false, - trackResize: true + trackResize: true, + maxWorkers: Math.max(browser.hardwareConcurrency - 1, 1) }; /** @@ -105,10 +106,16 @@ var defaultOptions = { var Map = module.exports = function(options) { options = util.extend({}, defaultOptions, options); + + if (options.maxWorkers < 1) { + throw new Error('maxWorkers must an integer greater than or equal to 1.'); + } + this._interactive = options.interactive; this._failIfMajorPerformanceCaveat = options.failIfMajorPerformanceCaveat; this._preserveDrawingBuffer = options.preserveDrawingBuffer; this._trackResize = options.trackResize; + this._maxWorkers = options.maxWorkers; if (typeof options.container === 'string') { this._container = document.getElementById(options.container); @@ -577,7 +584,7 @@ util.extend(Map.prototype, /** @lends Map.prototype */{ } else if (style instanceof Style) { this.style = style; } else { - this.style = new Style(style, this.animationLoop); + this.style = new Style(style, this.animationLoop, this._maxWorkers); } this.style diff --git a/test/js/style/style.test.js b/test/js/style/style.test.js index 26920b3d333..3aab22335b0 100644 --- a/test/js/style/style.test.js +++ b/test/js/style/style.test.js @@ -9,7 +9,6 @@ var Style = require('../../../js/style/style'); var VectorTileSource = require('../../../js/source/vector_tile_source'); var StyleLayer = require('../../../js/style/style_layer'); var util = require('../../../js/util/util'); -var browser = require('../../../js/util/browser'); function createStyleJSON(properties) { return util.extend({ @@ -1123,8 +1122,8 @@ test('Style#query*Features', function(t) { }); test('Style creates correct number of workers', function(t) { - var style = new Style(createStyleJSON()); - t.equal(style.dispatcher.actors.length, browser.hardwareConcurrency - 1); + var style = new Style(createStyleJSON(), null, 3); + t.equal(style.dispatcher.actors.length, 3); t.ok(style); t.end(); }); diff --git a/test/js/ui/map.test.js b/test/js/ui/map.test.js index 244f6b71e5c..21a3d932019 100755 --- a/test/js/ui/map.test.js +++ b/test/js/ui/map.test.js @@ -6,6 +6,7 @@ var window = require('../../../js/util/browser').window; var Map = require('../../../js/ui/map'); var Style = require('../../../js/style/style'); var LngLat = require('../../../js/geo/lng_lat'); +var browser = require('../../../js/util/browser'); var sinon = require('sinon'); var fixed = require('../../testutil/fixed'); @@ -930,6 +931,21 @@ test('Map', function(t) { t.end(); }); + t.test('maxWorkers option', function(t) { + var map = createMap({}); + // TODO: it's not great to check this private member here; better would + // be to mock Style and then just check that the correct maxWorkers + // param is being passed. + t.equal(map._maxWorkers, browser.hardwareConcurrency - 1, 'maxWorkers defaults to hardwareConcurrency - 1'); + map = createMap({ maxWorkers: 3 }); + t.equal(map._maxWorkers, 3, 'maxWorkers option is used'); + t.throws(function () { + createMap({ maxWorkers: 0 }); + }); + t.end(); + }); + + t.end(); });