diff --git a/src/maps/globeEngine.js b/src/maps/globeEngine.js index 3e55e9c71..0f0ec8652 100644 --- a/src/maps/globeEngine.js +++ b/src/maps/globeEngine.js @@ -221,10 +221,18 @@ export const globeMapEngine = Object.freeze({ return layer.on('tileError', () => listener({})); }, isDestroyed: (value) => Boolean(value) && released.has(value), + /** + * Never use `value` again: a photoreal handle is hidden, and an imagery + * spec a source made with a `destroy` of its own (a source that keeps + * tokens for its layer, say) is told, as Cesium's engine destroys a + * provider. + */ release(value) { if (!value || typeof value !== 'object' || released.has(value)) return; if (value.globe && value.spec) applyPhotoreal(value, false); released.add(value); + if (typeof value.destroy === 'function' && !value.isDestroyed?.()) + value.destroy(); }, loadPhotoreal, }); diff --git a/src/maps/globeEngine.test.mjs b/src/maps/globeEngine.test.mjs index 246326deb..228b516eb 100644 --- a/src/maps/globeEngine.test.mjs +++ b/src/maps/globeEngine.test.mjs @@ -291,3 +291,23 @@ test('a stack kept after a failed switch and a failed recovery still falls back controller.destroy(); } }); + +test('a released imagery spec with a destroy of its own is told, once', () => { + let destroyed = 0; + const spec = { + kind: 'xyz', + url: 'https://example.com/{z}/{x}/{y}.png', + destroy() { + destroyed++; + }, + isDestroyed: () => destroyed > 0, + }; + engine.release(spec); + engine.release(spec); + assert.equal(destroyed, 1); + assert.equal(engine.isDestroyed(spec), true); + // The app's own specs are frozen and have none: released quietly. + const own = engine.imagery.esri(); + engine.release(own); + assert.equal(engine.isDestroyed(own), true); +});