diff --git a/src/maps/controller.js b/src/maps/controller.js index 5d4ca8396..d2dd9b7b2 100644 --- a/src/maps/controller.js +++ b/src/maps/controller.js @@ -248,13 +248,7 @@ export class MapSourceController { recovery && recovery.id !== stack.id && this.isStackAvailable(recovery.id); - // A stack that stays after the failed switch still falls back on its - // tile failures, heard for this switch. - if (!recovers && this._imageryLayer && this._activeResolution) { - this._removeImageryErrorListener?.(); - this._removeImageryErrorListener = null; - this._watchProvider(this._activeResolution, gen); - } + let recovered = false; if (recovers) { // Whatever settles now is the controller's choice, not the caller's. this._switchOrigin = 'automatic'; @@ -262,12 +256,20 @@ export class MapSourceController { const activation = await this._activate(recovery, gen); if (gen !== this._switchGen) return this.getState(); this._activeId = activation?.effectiveStackId || recovery.id; + recovered = true; } catch (recoveryError) { if (gen !== this._switchGen) return this.getState(); this._lastError = recoveryError?.message || String(recoveryError); this._onError?.(this._lastError, recovery); } } + // A stack that stays after the failed switch (and a failed recovery) + // still falls back on its tile failures, heard for this switch. + if (!recovered && this._imageryLayer && this._activeResolution) { + this._removeImageryErrorListener?.(); + this._removeImageryErrorListener = null; + this._watchProvider(this._activeResolution, gen); + } if (!silent) this._emitChange('error'); } finally { if (gen === this._switchGen) { diff --git a/src/maps/globeEngine.test.mjs b/src/maps/globeEngine.test.mjs index 81ef36b50..0dedb549d 100644 --- a/src/maps/globeEngine.test.mjs +++ b/src/maps/globeEngine.test.mjs @@ -214,3 +214,65 @@ test('a photoreal stack that fails to come back leaves the stack before it in pl assert.equal(controller.getState().activeId, 'osm'); controller.destroy(); }); + +test('a stack kept after a failed switch and a failed recovery still falls back on tile failures', async () => { + const { host, renderer } = scene(['photoreal']); + let osmTries = 0; + const failing = { + ...engine, + imagery: { + ...engine.imagery, + osm() { + osmTries += 1; + throw new Error('osm refused'); + }, + }, + }; + const loaded = await failing.loadPhotoreal(host, { googleApiKey: 'key' }); + const controller = new MapSourceController(host, { + registry: createDefaultMapSources({ + googleTileset: loaded.tileset, + engine: failing, + }), + engine: failing, + googleTileset: loaded.tileset, + onError: () => {}, + }); + const tileErrors = new Set(); + const add = renderer.globe.imagery.add; + renderer.globe.imagery.add = (...args) => { + const layer = add(...args); + const on = layer.on; + layer.on = (event, listener) => { + const off = on(event, listener); + if (event !== 'tileError') return off; + tileErrors.add(listener); + return () => { + tileErrors.delete(listener); + off(); + }; + }; + return layer; + }; + await controller.setStack('esri-imagery', { silent: true }); + renderer.globe.setPhotoreal = async (spec) => ({ + route: spec ? 'none' : 'google', + error: { code: 'refused', message: 'refused' }, + }); + const warn = console.warn; + console.warn = () => {}; + try { + // OSM fails, and so does its recovery (photoreal): Esri stays. + await controller.setStack('osm', { silent: true }); + assert.equal(controller.getState().activeId, 'esri-imagery'); + assert.equal(renderer.globe.imagery.list().length, 1, 'its imagery stays'); + const tries = osmTries; + for (const listener of [...tileErrors]) listener({}); + for (const listener of [...tileErrors]) listener({}); + for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 0)); + assert.ok(osmTries > tries, 'its tile failures still try its fallback'); + } finally { + console.warn = warn; + controller.destroy(); + } +});