Skip to content

Commit

Permalink
Pause SourceCache while worker layer update is pending
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Jun 26, 2017
1 parent ce2b53e commit 6a20f9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SourceCache extends Evented {

// for sources with mutable data, this event fires when the underlying data
// to a source is changed. (i.e. GeoJSONSource#setData and ImageSource#serCoordinates)
if (this._sourceLoaded && e.dataType === "source" && e.sourceDataType === 'content') {
if (this._sourceLoaded && !this.paused && e.dataType === "source" && e.sourceDataType === 'content') {
this.reload();
if (this.transform) {
this.update(this.transform);
Expand Down Expand Up @@ -97,6 +97,19 @@ class SourceCache extends Evented {
return this._source;
}

pause() {
this._paused = true;
}

resume() {
if (!this._paused) return;
const shouldReload = this._shouldReloadOnResume;
this._paused = false;
this._shouldReloadOnResume = false;
if (shouldReload) this.reload();
if (this.transform) this.update(this.transform);
}

_loadTile(tile, callback) {
return this._source.loadTile(tile, callback);
}
Expand Down Expand Up @@ -139,6 +152,10 @@ class SourceCache extends Evented {
}

reload() {
if (this._paused) {
this._shouldReloadOnResume = true;
}

this._cache.reset();
for (const i in this._tiles) {
this._reloadTile(i, 'reloading');
Expand Down Expand Up @@ -308,7 +325,8 @@ class SourceCache extends Evented {
*/
update(transform) {
this.transform = transform;
if (!this._sourceLoaded) { return; }
if (!this._sourceLoaded || this._paused) { return; }

let i;
let coord;
let tile;
Expand Down Expand Up @@ -507,6 +525,9 @@ class SourceCache extends Evented {
* @private
*/
clearTiles() {
this._shouldReloadOnResume = false;
this._paused = false;

for (const id in this._tiles)
this._removeTile(id);
this._cache.reset();
Expand Down
10 changes: 9 additions & 1 deletion src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,12 @@ class Style extends Evented {
// https://github.com/mapbox/mapbox-gl-js/issues/3633
const removed = this._removedLayers[id];
delete this._removedLayers[id];
this._updatedSources[layer.source] = removed.type !== layer.type ? 'clear' : 'reload';
if (removed.type !== layer.type) {
this._updatedSources[layer.source] = 'clear';
} else {
this._updatedSources[layer.source] = 'reload';
this.sourceCaches[layer.source].pause();
}
}
this._updateLayer(layer);

Expand Down Expand Up @@ -507,6 +512,7 @@ class Style extends Evented {
this._updatedSymbolOrder = true;
if (layer.source && !this._updatedSources[layer.source]) {
this._updatedSources[layer.source] = 'reload';
this.sourceCaches[layer.source].pause();
}
}
}
Expand Down Expand Up @@ -720,6 +726,7 @@ class Style extends Evented {
this._updatedLayers[layer.id] = true;
if (layer.source && !this._updatedSources[layer.source]) {
this._updatedSources[layer.source] = 'reload';
this.sourceCaches[layer.source].pause();
}
this._changed = true;
}
Expand Down Expand Up @@ -845,6 +852,7 @@ class Style extends Evented {
}

_reloadSource(id) {
this.sourceCaches[id].resume();
this.sourceCaches[id].reload();
}

Expand Down

0 comments on commit 6a20f9e

Please sign in to comment.