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

+36 -4
View File
@@ -135,6 +135,9 @@ export function createCesiumGlobe(
let photorealStatus = { route: 'none' };
let photorealGen = 0;
let terrainGen = 0;
let baseGen = 0;
/** Aborts the style load of the `setBase` in flight, if any. */
let baseLoad = null;
let baseCredit = null;
// The vector base's sources' credits.
const vectorCredits = [];
@@ -240,11 +243,36 @@ export function createCesiumGlobe(
async function setBase(base) {
if (base.vector && !can('vectorBase'))
throw new TypeError('This renderer draws no vector base');
// The last call wins: one superseded (or disposed) before its base
// changes changes nothing and resolves with the base as it stands.
const gen = ++baseGen;
baseLoad?.abort();
baseLoad = null;
const current = () => gen === baseGen && !disposed;
const superseded = () => ({
photoreal: photorealStatus,
imagery: [],
settled: false,
});
if (disposed) return superseded();
// The style loads before the base changes: one that fails changes
// nothing.
const style = base.vector
? await loadVectorStyle(base.vector.styleUrl)
: null;
let style = null;
if (base.vector) {
const load = new AbortController();
baseLoad = load;
try {
style = 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 handle of imagery.list()) handle.remove();
baseCredit?.();
baseCredit = base.credit && credits ? credits.addStatic(base.credit) : null;
@@ -268,7 +296,8 @@ export function createCesiumGlobe(
return {
photoreal: photorealStatus,
imagery: layers,
settled: isSettled('base'),
// A base a later call replaced has not settled.
settled: current() && isSettled('base'),
};
}
@@ -579,6 +608,9 @@ export function createCesiumGlobe(
disposed = true;
photorealGen += 1;
terrainGen += 1;
baseGen += 1;
baseLoad?.abort();
baseLoad = null;
for (const off of removers) off();
shading = null;
dropShadingMaterial();
+76
View File
@@ -470,6 +470,82 @@ test('setBase replaces the imagery and says what it drew', async () => {
await assert.rejects(port.setBase({ vector: { styleUrl: 'x' } }), /vector/);
});
/** `globalThis.fetch` answering each URL when the test says, honouring aborts. */
function heldFetch() {
const held = [];
const original = globalThis.fetch;
const options = { honourAbort: true };
globalThis.fetch = (url, { signal } = {}) =>
new Promise((resolve, reject) => {
if (options.honourAbort)
signal?.addEventListener('abort', () => reject(signal.reason));
held.push({
url,
signal,
answer: (body) =>
resolve({ ok: true, status: 200, json: async () => body }),
});
});
return { held, options, restore: () => (globalThis.fetch = original) };
}
const EMPTY_STYLE = { version: 8, sources: {}, layers: [] };
test('a slow vector setBase a later setBase supersedes changes nothing', async () => {
const { held, options, restore } = heldFetch();
try {
const { port } = setup(new Set([...ALL, 'vectorBase']));
const slow = port.setBase({
vector: { styleUrl: 'https://s.invalid/a.json' },
});
const fast = await port.setBase({
imagery: [{ kind: 'xyz', url: url('one') }],
});
assert.equal(held.length, 1);
assert.equal(held[0].signal.aborted, true, 'its style load is aborted');
const status = await slow;
assert.deepEqual(status.imagery, []);
assert.equal(status.settled, false);
assert.deepEqual(port.imagery.list(), fast.imagery);
// One whose load ignores the abort and lands late changes nothing either.
options.honourAbort = false;
const late = port.setBase({
vector: { styleUrl: 'https://s.invalid/b.json' },
});
const kept = await port.setBase({
imagery: [{ kind: 'xyz', url: url('two') }],
});
held[1].answer(EMPTY_STYLE);
assert.deepEqual((await late).imagery, []);
assert.deepEqual(port.imagery.list(), kept.imagery);
} finally {
restore();
}
});
test('a vector setBase in flight when the globe is disposed resolves, changing nothing', async () => {
const { held, restore } = heldFetch();
try {
const { port, dispose } = setup(new Set([...ALL, 'vectorBase']));
const before = await port.setBase({
imagery: [{ kind: 'xyz', url: url('one') }],
});
const pending = port.setBase({
vector: { styleUrl: 'https://s.invalid/a.json' },
});
dispose();
const status = await pending;
assert.deepEqual(status.imagery, []);
assert.equal(status.settled, false);
assert.deepEqual(port.imagery.list(), before.imagery);
assert.equal(held[0].signal.aborted, true);
assert.deepEqual((await port.setBase({ imagery: [] })).imagery, []);
} finally {
restore();
}
});
test('globe visibility, settling and progress', () => {
const { scene, port, tick } = setup();
port.setGlobeVisible(false);