Skip to content

Commit

Permalink
Fix GeoJSONSource#loaded sometimes returning true while there are sti…
Browse files Browse the repository at this point in the history
…ll pending loads (maplibre#669) (maplibre#684)

* Fix GeoJSONSource#loaded sometimes returning true while there are still pending loads (maplibre#669)

* Add changelog entry
  • Loading branch information
vanilla-lake authored Dec 2, 2021
1 parent e643eed commit 240d1d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Fix type check for non dom environment. (#334)
- Fix precision problem in patterns when overzoomed in OpenGL ES devices.
- Fix padding-top of the popup to improve readability of popup text (#354).
- Fix GeoJSONSource#loaded sometimes returning true while there are still pending loads (#669)

## 1.15.2

Expand Down
13 changes: 7 additions & 6 deletions src/source/geojson_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class GeoJSONSource extends Evented implements Source {
workerOptions: any;
map: Map;
actor: Actor;
_loaded: boolean;
_pendingLoads: number;
_collectResourceTiming: boolean;
_resourceTiming: Array<PerformanceResourceTiming>;
_removed: boolean;
Expand All @@ -101,7 +101,7 @@ class GeoJSONSource extends Evented implements Source {
this.isTileClipped = true;
this.reparseOverscaled = true;
this._removed = false;
this._loaded = false;
this._pendingLoads = 0;

this.actor = dispatcher.getActor();
this.setEventedParent(eventedParent);
Expand Down Expand Up @@ -265,7 +265,6 @@ class GeoJSONSource extends Evented implements Source {
* using geojson-vt or supercluster as appropriate.
*/
_updateWorkerData(callback: Callback<void>) {
this._loaded = false;
const options = extend({}, this.workerOptions);
const data = this._data;
if (typeof data === 'string') {
Expand All @@ -275,16 +274,18 @@ class GeoJSONSource extends Evented implements Source {
options.data = JSON.stringify(data);
}

this._pendingLoads++;

// target {this.type}.loadData rather than literally geojson.loadData,
// so that other geojson-like source types can easily reuse this
// implementation
this.actor.send(`${this.type}.loadData`, options, (err, result) => {
this._pendingLoads--;

if (this._removed || (result && result.abandoned)) {
return;
}

this._loaded = true;

if (result && result.resourceTiming && result.resourceTiming[this.id])
this._resourceTiming = result.resourceTiming[this.id].slice(0);
// Any `loadData` calls that piled up while we were processing
Expand All @@ -300,7 +301,7 @@ class GeoJSONSource extends Evented implements Source {
}

loaded(): boolean {
return this._loaded;
return this._pendingLoads === 0;
}

loadTile(tile: Tile, callback: Callback<void>) {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/source/geojson_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ test('GeoJSONSource#setData', (t) => {
source.setData('http://localhost/nonexistent');
});

t.test('only marks source as loaded when there are no pending loads', (t) => {
const source = createSource();
source.once('data', () => {
t.notOk(source.loaded());
source.once('data', () => {
t.ok(source.loaded());
t.end();
});
});
source.setData({});
source.setData({});
});

t.end();
});

Expand Down

0 comments on commit 240d1d8

Please sign in to comment.