setBase: the last call wins; an overtaken or disposed one changes nothing

A vector setBase awaited its style before changing anything, with no
generation guard, so a slow earlier call landed over a later one, and one
in flight at disposal rejected. Cesium and the stub now count calls like
setPhotoreal and setTerrain, abort the overtaken style load, and resolve
an overtaken call with imagery: [] and settled: false.
This commit is contained in:
Sameh Khamis committed 2026-11-11 19:25:04 -08:00
1 parent 5fae3cf485
commit 72d8dd606d
6 files changed
+196 -7

No files matched your search

+32 -1
View File
@@ -1418,14 +1418,42 @@ export function createStubRenderer(options = {}) {
// `surfaceIdentity`: bumped by whatever changes what heights answer against.
let surfaceVersion = 0;
let terrainKey = 'null';
// The last `setBase` wins; its style load in flight, if any.
let baseGen = 0;
let baseLoad = null;
const globe = {
async setBase(base) {
log('globe', 'setBase', [base]);
if (base.vector && !capabilities.has('vectorBase'))
throw new TypeError('This renderer draws no vector base');
const gen = ++baseGen;
baseLoad?.abort();
baseLoad = null;
const current = () => gen === baseGen && !destroyed;
// Superseded (or destroyed) before its base changes: nothing changes.
const superseded = () => ({
photoreal: photorealStatus,
imagery: [],
settled: false,
});
if (destroyed) return superseded();
// The style is loaded, as a renderer drawing it loads it, so one that
// fails changes nothing; the stub paints none of it.
if (base.vector) await loadVectorStyle(base.vector.styleUrl);
if (base.vector) {
const load = new AbortController();
baseLoad = load;
try {
await loadVectorStyle(base.vector.styleUrl, undefined, {
signal: load.signal,
});
} catch (error) {
if (!current()) return superseded();
throw error;
} finally {
if (baseLoad === load) baseLoad = null;
}
if (!current()) return superseded();
}
for (const layer of imageryLayers.slice()) layer.remove();
if (base.photoreal !== undefined)
await globe.setPhotoreal(base.photoreal);
@@ -1781,6 +1809,9 @@ export function createStubRenderer(options = {}) {
// A flight in progress ends cancelled and gives back its input hold.
camera.endFlight();
destroyed = true;
baseGen += 1;
baseLoad?.abort();
baseLoad = null;
if (options.container) canvas.remove?.();
for (const clear of onceTimers) clear();
onceTimers.clear();
+41
View File
@@ -197,3 +197,44 @@ test('the stub camera ends no move while it follows', () => {
assert.deepEqual(seen, ['start', 'end']);
r.destroy();
});
test('a slow vector setBase a later setBase supersedes changes nothing, nor one the destroy ends', async () => {
const held = [];
const original = globalThis.fetch;
globalThis.fetch = (url, { signal } = {}) =>
new Promise((resolve) => {
held.push({
signal,
answer: () =>
resolve({
ok: true,
status: 200,
json: async () => ({ version: 8, sources: {}, layers: [] }),
}),
});
});
try {
const r = createStubRenderer();
const slow = r.globe.setBase({
vector: { styleUrl: 'https://s.invalid/a' },
});
const fast = await r.globe.setBase({
imagery: [{ kind: 'xyz', url: 'https://one.invalid/{z}/{x}/{y}.png' }],
});
assert.equal(held[0].signal.aborted, true, 'its style load is aborted');
held[0].answer();
const status = await slow;
assert.deepEqual([status.imagery, status.settled], [[], false]);
assert.deepEqual(r.globe.imagery.list(), fast.imagery);
const ended = r.globe.setBase({
vector: { styleUrl: 'https://s.invalid/b' },
});
r.destroy();
held[1].answer();
assert.deepEqual((await ended).imagery, []);
assert.deepEqual(r.globe.imagery.list(), fast.imagery);
} finally {
globalThis.fetch = original;
}
});