mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Keep history entries untouched by unfinished globe picks and restore each visited entry's layers
A picked engine now lives in the switch until it runs; only then is the address written, so a pick superseded by Back or one that fails rewrites no entry. An engine-changing history visit holds the outgoing share link, which neither restores nor writes the visited entry, and the running engine takes the address back once the switch settles, restoring any entry visited while it was held or not yet listening. Handed-off layers keep running where the incoming address agrees with the outgoing one and follow a visited entry where it differs. A failed recovery keeps the error on the loading screen with an engine picker for a retry, and a failing reveal of the new globe no longer blocks the cover removal or later requests.
This commit is contained in:
1 parent
9cc34e324f
commit
a066a2d1ca
10 files changed
+539
-113
No files matched your search
@@ -1,11 +1,16 @@
|
||||
// The page's engine switch: one layer session, one engine at a time.
|
||||
//
|
||||
// The address (?engine=) is the source of truth. A switch from the picker
|
||||
// (select) and a history visit (Back/Forward, a pasted address) are the same
|
||||
// operation: the running engine hands the session off and is released, the
|
||||
// requested engine starts on the retained session. Requests that arrive while
|
||||
// a switch or a recovery runs are never dropped: once the current attempt
|
||||
// settles, the address is read again and reconciled.
|
||||
// The address (?engine=) is the source of truth, except while a picked
|
||||
// engine starts: that choice lives here until the engine runs, and only then
|
||||
// is written to the address as a new history entry. A switch from the picker
|
||||
// (select) and a history visit (Back/Forward, a pasted address) both hand the
|
||||
// session off: the running engine is released, the requested engine starts on
|
||||
// the retained session. They differ in what the incoming engine shows: a pick
|
||||
// keeps the running view and layers, a visit restores the entry's view and
|
||||
// the layers its entry changed (as a same-engine Back does) on the retained
|
||||
// layer models.
|
||||
// Requests that arrive while a switch or a recovery runs are never dropped:
|
||||
// once the current attempt settles, the address is read again and reconciled.
|
||||
|
||||
const ENGINES = Object.freeze(['cesium', 'navara']);
|
||||
|
||||
@@ -25,13 +30,13 @@ export const FIRST_FRAME_WAIT_MS = 10_000;
|
||||
* @param {string} options.kind The engine already running.
|
||||
* @param {() => string} options.requested The engine the address asks for now.
|
||||
* @param {AbortSignal} options.signal The page lifetime.
|
||||
* @param {(from: string, to: string) => Promise<void>} options.leave Hand the
|
||||
* session off from the running engine and release that engine.
|
||||
* @param {(from: string, to: string, change: {visit: boolean}) => Promise<void>} options.leave
|
||||
* Hand the session off from the running engine and release that engine.
|
||||
* @param {(kind: string, attempt: {generation: number, recovery: boolean}) => Promise<void>} options.enter
|
||||
* Start an engine on the retained session. It resolves once the engine runs
|
||||
* with the session attached; when it rejects it has already released what
|
||||
* it started.
|
||||
* @param {{select: (from: ?string, to: string) => *, commit: (token: *) => void, restore: (kind: string) => void}} options.address
|
||||
* @param {{select: (from: ?string) => *, commit: (token: *, kind: string) => void, restore: (kind: string) => void}} options.address
|
||||
* @param {(state: {failed: boolean}) => Promise<void>|void} [options.settle] Runs
|
||||
* once the address is reconciled, before the machine reports idle.
|
||||
* @param {(kind: string) => void} [options.remember] Store a picked engine.
|
||||
@@ -61,10 +66,12 @@ export function createEngineSwitch({
|
||||
let busy = false;
|
||||
let generation = 0;
|
||||
// Every request (select or address visit) bumps this; a visit also bumps
|
||||
// `visits`, which retires a select's pending history entry.
|
||||
// `visits`.
|
||||
let requests = 0;
|
||||
let visits = 0;
|
||||
let queued = null;
|
||||
// The pick being started, held here rather than in the address: a visit
|
||||
// made meanwhile retires it and leaves every entry as it was.
|
||||
let pending = null;
|
||||
let running = null;
|
||||
|
||||
@@ -79,7 +86,7 @@ export function createEngineSwitch({
|
||||
const publish = () => onChange?.(state());
|
||||
|
||||
function prepare(next) {
|
||||
pending = { to: next, visits, token: address.select(current, next) };
|
||||
pending = { to: next, token: address.select(current) };
|
||||
}
|
||||
|
||||
/** The picker's choice. While a switch runs it is queued, not dropped. */
|
||||
@@ -100,8 +107,9 @@ export function createEngineSwitch({
|
||||
if (signal.aborted) return Promise.resolve();
|
||||
requests++;
|
||||
visits++;
|
||||
// A later visit supersedes a select queued before it.
|
||||
// A later visit supersedes a pick queued or starting before it.
|
||||
queued = null;
|
||||
pending = null;
|
||||
return busy ? running : run();
|
||||
}
|
||||
|
||||
@@ -109,7 +117,9 @@ export function createEngineSwitch({
|
||||
busy = true;
|
||||
publish();
|
||||
running = drive()
|
||||
.catch((error) => onFailure?.(error, { recovered: false, kind: null }))
|
||||
.catch((error) =>
|
||||
onFailure?.(error, { recovered: current !== null, kind: current }),
|
||||
)
|
||||
.finally(() => {
|
||||
busy = false;
|
||||
running = null;
|
||||
@@ -123,7 +133,12 @@ export function createEngineSwitch({
|
||||
await reconcileAddress();
|
||||
if (signal.aborted) return;
|
||||
const seen = requests;
|
||||
await settle?.({ failed });
|
||||
try {
|
||||
await settle?.({ failed });
|
||||
} catch (error) {
|
||||
// Revealing the globe is cosmetic; the engine runs either way.
|
||||
console.error('The globe switch could not settle:', error);
|
||||
}
|
||||
if (signal.aborted || seen === requests) return;
|
||||
}
|
||||
}
|
||||
@@ -137,7 +152,7 @@ export function createEngineSwitch({
|
||||
queued = null;
|
||||
if (next !== current) prepare(next);
|
||||
}
|
||||
const target = requested();
|
||||
const target = pending?.to ?? requested();
|
||||
if (target === current) return;
|
||||
// After a failed recovery, wait for the person's next request instead
|
||||
// of retrying the same address in a loop.
|
||||
@@ -150,12 +165,13 @@ export function createEngineSwitch({
|
||||
async function attempt(target) {
|
||||
const id = ++generation;
|
||||
const visitsAtStart = visits;
|
||||
const visit = pending?.to !== target;
|
||||
publish();
|
||||
try {
|
||||
if (current !== null) {
|
||||
const from = current;
|
||||
try {
|
||||
await leave(from, target);
|
||||
await leave(from, target, { visit });
|
||||
} finally {
|
||||
current = null;
|
||||
}
|
||||
@@ -166,18 +182,17 @@ export function createEngineSwitch({
|
||||
if (signal.aborted) return;
|
||||
current = working = target;
|
||||
failed = false;
|
||||
if (pending?.to === target && pending.visits === visits) {
|
||||
address.commit(pending.token);
|
||||
if (!visit && pending?.to === target) {
|
||||
address.commit(pending.token, target);
|
||||
remember?.(target);
|
||||
}
|
||||
pending = null;
|
||||
} catch (error) {
|
||||
if (signal.aborted) return;
|
||||
pending = null;
|
||||
// Point this history entry back at the working engine unless the
|
||||
// person has since moved to another entry. Nothing is pushed, so a
|
||||
// failed switch leaves no extra entry behind.
|
||||
if (visits === visitsAtStart && requested() === target)
|
||||
// A failed pick never touched the address. A failed visit points its
|
||||
// entry at the working engine, unless the person has since moved on.
|
||||
if (visit && visits === visitsAtStart && requested() === target)
|
||||
address.restore(working);
|
||||
if (working === target) {
|
||||
live = null;
|
||||
@@ -205,10 +220,10 @@ export function createEngineSwitch({
|
||||
}
|
||||
|
||||
/**
|
||||
* The engine choice in the page address. A picked engine replaces the
|
||||
* current entry while it starts; only a successful switch adds the history
|
||||
* entry (the earlier entry keeps the outgoing engine and view), so Back
|
||||
* returns to the previous engine and a failed switch adds nothing.
|
||||
* The engine choice in the page address. A pick leaves the address alone
|
||||
* while its engine starts; a successful one adds the history entry (the
|
||||
* earlier entry keeps the outgoing engine and view), so Back returns to the
|
||||
* previous engine, and a failed or superseded pick changes no entry.
|
||||
*/
|
||||
export function createEngineAddress({ location, history, flush = () => {} }) {
|
||||
const withEngine = (engine) => {
|
||||
@@ -217,15 +232,15 @@ export function createEngineAddress({ location, history, flush = () => {} }) {
|
||||
return url.href;
|
||||
};
|
||||
return Object.freeze({
|
||||
select(from, to) {
|
||||
/** Snapshot the outgoing entry: its engine and its latest view. */
|
||||
select(from) {
|
||||
flush();
|
||||
const previous = from ? withEngine(from) : null;
|
||||
history.replaceState(history.state, '', withEngine(to));
|
||||
return previous;
|
||||
return from ? withEngine(from) : null;
|
||||
},
|
||||
commit(previous) {
|
||||
if (!previous) return;
|
||||
const next = location.href;
|
||||
commit(previous, engine) {
|
||||
const next = withEngine(engine);
|
||||
// Without a running engine to return to, the pick names this entry.
|
||||
if (!previous) return history.replaceState(history.state, '', next);
|
||||
history.replaceState(history.state, '', previous);
|
||||
history.pushState(null, '', next);
|
||||
},
|
||||
@@ -236,21 +251,26 @@ export function createEngineAddress({ location, history, flush = () => {} }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Hand the session off before the running engine is released: record which
|
||||
* layers run so the next engine retains them, and park the cameras and the
|
||||
* Hand the session off before the running engine is released: record the
|
||||
* layer models so the next engine retains them, and park the cameras and the
|
||||
* Director scene instead of ending them. Every engine change in this page
|
||||
* hands off, whatever started it; the incoming engine reads the view
|
||||
* (camera, style, map) from the address it starts with, which for a history
|
||||
* visit may differ from the view that was showing.
|
||||
* hands off, whatever started it. The incoming engine reads the view
|
||||
* (camera, style, map) from the address it starts with. Each layer records
|
||||
* whether it runs and whether the outgoing address listed it
|
||||
* (`addressLayerIds`): the layer-state coordinator keeps it running as it
|
||||
* was where the incoming address agrees (always after a pick) and follows a
|
||||
* visited entry where it differs (handoffTargetEnabled).
|
||||
*/
|
||||
export function handOffSession(session) {
|
||||
export function handOffSession(session, { addressLayerIds = null } = {}) {
|
||||
const manager = session.manager;
|
||||
const listed = addressLayerIds ? new Set(addressLayerIds) : null;
|
||||
session.handoffLayers = new Map(
|
||||
[...manager.layers].map(([id, entry]) => [
|
||||
id,
|
||||
{
|
||||
entry: entry.module.attachRenderer ? entry : null,
|
||||
enabled: manager.isEffectivelyEnabled(id),
|
||||
durable: listed ? listed.has(id) : undefined,
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
@@ -127,7 +127,7 @@ function harness({
|
||||
signal: lifetime.signal,
|
||||
async leave(from, to) {
|
||||
log.push(`leave:${from}->${to}`);
|
||||
handOffSession(session);
|
||||
handOffSession(session, { addressLayerIds: session.addressLayerIds });
|
||||
parked.add(from);
|
||||
running = null;
|
||||
},
|
||||
@@ -189,13 +189,25 @@ test('a picked engine hands the session off and adds one history entry', async (
|
||||
|
||||
test('Back and Forward hand the session off exactly like the picker', async () => {
|
||||
const h = harness();
|
||||
await h.machine.select('navara');
|
||||
const picked = h.log.splice(0).filter((line) => line !== 'flush');
|
||||
// The outgoing address lists flights; directions run off the record.
|
||||
h.session.addressLayerIds = ['flights'];
|
||||
let captured;
|
||||
h.session.director.suspendScene = () => {
|
||||
captured = h.session.handoffLayers;
|
||||
h.log.push('suspend:director');
|
||||
};
|
||||
await h.machine.select('navara');
|
||||
const picked = h.log.splice(0).filter((line) => line !== 'flush');
|
||||
const record = (handoff) =>
|
||||
Object.fromEntries(
|
||||
[...handoff].map(([id, { enabled, durable }]) => [
|
||||
id,
|
||||
{ enabled, durable },
|
||||
]),
|
||||
);
|
||||
const afterPick = record(captured);
|
||||
assert.deepEqual(afterPick.flights, { enabled: true, durable: true });
|
||||
assert.deepEqual(afterPick.directions, { enabled: false, durable: false });
|
||||
h.browser.go(-1);
|
||||
await h.machine.reconcile();
|
||||
assert.equal(h.machine.state().kind, 'cesium');
|
||||
@@ -210,7 +222,9 @@ test('Back and Forward hand the session off exactly like the picker', async () =
|
||||
),
|
||||
);
|
||||
assert.ok(!h.log.includes('destroy:director'));
|
||||
assert.deepEqual(captured.get('flights').enabled, true);
|
||||
// The visit records the same running state and outgoing address; the
|
||||
// layer-state coordinator compares that with the visited entry.
|
||||
assert.deepEqual(record(captured), afterPick);
|
||||
assert.equal(
|
||||
captured.get('flights').entry,
|
||||
h.session.manager.layers.get('flights'),
|
||||
@@ -249,11 +263,32 @@ test('a visit during a switch is reconciled once the attempt settles', async ()
|
||||
'enter:cesium:warm',
|
||||
],
|
||||
);
|
||||
// The pick was superseded before it settled: no entry was pushed for it.
|
||||
assert.equal(h.browser.entries.length, 2);
|
||||
// The pick was superseded before it settled: no entry was pushed for it,
|
||||
// and neither entry was rewritten.
|
||||
assert.deepEqual(h.browser.engines(), ['cesium', 'cesium']);
|
||||
assert.deepEqual(h.remembered, []);
|
||||
});
|
||||
|
||||
test('a pick that fails after Back rewrites no entry', async () => {
|
||||
const gate = deferred();
|
||||
const h = harness({
|
||||
urls: ['/?engine=cesium#a', '/?engine=cesium#b'],
|
||||
outcome: (kind) => (kind === 'navara' ? gate.promise : undefined),
|
||||
});
|
||||
const switching = h.machine.select('navara');
|
||||
h.browser.go(-1);
|
||||
void h.machine.reconcile();
|
||||
gate.reject(new Error('Navara refused'));
|
||||
await switching;
|
||||
assert.equal(h.machine.state().kind, 'cesium');
|
||||
assert.equal(h.browser.index, 0);
|
||||
assert.deepEqual(h.browser.engines(), ['cesium', 'cesium']);
|
||||
assert.deepEqual(
|
||||
h.browser.entries.map(({ url }) => new URL(url).hash),
|
||||
['#a', '#b'],
|
||||
);
|
||||
});
|
||||
|
||||
test('rollback goes to the last engine that worked, not the one that just failed', async () => {
|
||||
const gate = deferred();
|
||||
const h = harness({
|
||||
@@ -279,7 +314,7 @@ test('rollback goes to the last engine that worked, not the one that just failed
|
||||
]);
|
||||
// The visited entry now names the engine that runs; no entry was added.
|
||||
assert.equal(h.browser.index, 0);
|
||||
assert.deepEqual(h.browser.engines(), ['navara', 'navara']);
|
||||
assert.deepEqual(h.browser.engines(), ['navara', 'cesium']);
|
||||
});
|
||||
|
||||
test('a pick made during a switch is queued, not dropped', async () => {
|
||||
@@ -388,6 +423,8 @@ test('after a failed recovery the page stays usable and the next pick starts an
|
||||
assert.equal(h.machine.state().kind, 'navara');
|
||||
assert.equal(h.machine.state().failed, false);
|
||||
assert.equal(h.log.filter((line) => line.startsWith('leave')).length, 1);
|
||||
// Nothing ran to return to: the pick names the current entry.
|
||||
assert.deepEqual(h.browser.engines(), ['navara']);
|
||||
});
|
||||
|
||||
test('pagehide during a start neither rolls back nor starts another engine', async () => {
|
||||
@@ -537,15 +574,16 @@ test('the address keeps the outgoing view on the earlier entry', () => {
|
||||
location: browser.location,
|
||||
history: browser.history,
|
||||
});
|
||||
const token = address.select('cesium', 'navara');
|
||||
assert.deepEqual(browser.engines(), ['navara']);
|
||||
const token = address.select('cesium');
|
||||
// The address stays as it is while the picked engine starts.
|
||||
assert.deepEqual(browser.engines(), [null]);
|
||||
// The incoming engine writes its own view while it starts.
|
||||
browser.history.replaceState(
|
||||
null,
|
||||
'',
|
||||
'/?look=cinematic&engine=navara#lat=2',
|
||||
);
|
||||
address.commit(token);
|
||||
address.commit(token, 'navara');
|
||||
assert.deepEqual(
|
||||
browser.entries.map(({ url }) => url.replace(ORIGIN, '/')),
|
||||
[
|
||||
@@ -554,3 +592,40 @@ test('the address keeps the outgoing view on the earlier entry', () => {
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('a settle failure is not fatal and later requests are still reconciled', async () => {
|
||||
const h = harness();
|
||||
let settles = 0;
|
||||
const errors = [];
|
||||
const original = console.error;
|
||||
console.error = (...args) => errors.push(args);
|
||||
try {
|
||||
const machine = createEngineSwitch({
|
||||
kind: 'cesium',
|
||||
requested: h.browser.requested,
|
||||
signal: h.lifetime.signal,
|
||||
leave: async () => {},
|
||||
enter: async () => {},
|
||||
settle: async () => {
|
||||
settles += 1;
|
||||
if (settles === 1) {
|
||||
// A pick arrives while the first reveal fails.
|
||||
void machine.select('cesium');
|
||||
throw new Error('cover failed');
|
||||
}
|
||||
},
|
||||
address: createEngineAddress({
|
||||
location: h.browser.location,
|
||||
history: h.browser.history,
|
||||
}),
|
||||
onFailure: () => assert.fail('a settle error must not report a failure'),
|
||||
});
|
||||
await machine.select('navara');
|
||||
assert.equal(machine.state().kind, 'cesium');
|
||||
assert.equal(machine.state().failed, false);
|
||||
assert.equal(settles, 2);
|
||||
assert.equal(errors.length, 1);
|
||||
} finally {
|
||||
console.error = original;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user