Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tiles preloading when a source is not yet loaded #12699

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface Source {

fire(event: Event): mixed;
on(type: MapEvent, listener: (Object) => any): Evented;
off(type: MapEvent, listener: (Object) => any): Evented;
setEventedParent(parent: ?Evented, data?: Object | () => Object): Evented;

+onAdd?: (map: Map) => void;
Expand Down
11 changes: 11 additions & 0 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,17 @@ class SourceCache extends Evented {
* @returns {Object} Returns `this` | Promise.
*/
_preloadTiles(transform: Transform | Array<Transform>, callback: Callback<any>) {
if (!this._sourceLoaded) {
const waitUntilSourceLoaded = () => {
if (!this._sourceLoaded) return;
this._source.off('data', waitUntilSourceLoaded);
this._preloadTiles(transform, callback);
};

this._source.on('data', waitUntilSourceLoaded);
return;
}

const coveringTilesIDs: Map<number, OverscaledTileID> = new Map();
const transforms = Array.isArray(transform) ? transform : [transform];

Expand Down
21 changes: 21 additions & 0 deletions test/unit/source/source_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1897,5 +1897,26 @@ test('SourceCache#_preloadTiles', (t) => {
sourceCache.getSource().onAdd();
});

t.test('waits until source is loaded before preloading tiles', (t) => {
const transform = new Transform();
transform.resize(511, 511);
transform.zoom = 0;

const {sourceCache} = createSourceCache({
loadTile (tile) {
t.ok(sourceCache._sourceLoaded, 'source is loaded before preloading tiles');
t.equal(tile.tileID.key, new OverscaledTileID(0, 0, 0, 0, 0).key);
t.end();
}
});

// Marks source as not loaded
sourceCache._sourceLoaded = false;
sourceCache._preloadTiles(transform);

// Fires event that marks source as loaded
sourceCache.getSource().onAdd();
});

t.end();
});