From a9a0dc9efd0713c906565b35e609bfd7171ff1a9 Mon Sep 17 00:00:00 2001 From: Stepan Kuzmin Date: Mon, 8 May 2023 15:29:12 +0300 Subject: [PATCH] Postpone tiles preloading until source is loaded --- src/source/source.js | 1 + src/source/source_cache.js | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/source/source.js b/src/source/source.js index a3a1e92a424..951f055c7d5 100644 --- a/src/source/source.js +++ b/src/source/source.js @@ -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; diff --git a/src/source/source_cache.js b/src/source/source_cache.js index f7d2dc41ce3..e00ebc77872 100644 --- a/src/source/source_cache.js +++ b/src/source/source_cache.js @@ -994,6 +994,17 @@ class SourceCache extends Evented { * @returns {Object} Returns `this` | Promise. */ _preloadTiles(transform: Transform | Array, callback: Callback) { + 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 = new Map(); const transforms = Array.isArray(transform) ? transform : [transform]; @@ -1020,10 +1031,9 @@ class SourceCache extends Evented { } const tileIDs = Array.from(coveringTilesIDs.values()); - const tileZoom = this.transform ? this.transform.tileZoom : this.map.transform.tileZoom; asyncAll(tileIDs, (tileID, done) => { - const tile = new Tile(tileID, this._source.tileSize * tileID.overscaleFactor(), tileZoom, this.map.painter, this._isRaster); + const tile = new Tile(tileID, this._source.tileSize * tileID.overscaleFactor(), this.transform.tileZoom, this.map.painter, this._isRaster); this._loadTile(tile, (err) => { if (this._source.type === 'raster-dem' && tile.dem) this._backfillDEM(tile); done(err, tile);