Fix Stories navigation, sharing, and voice entry

This commit is contained in:
Manjunath committed 2026-11-16 20:12:10 +05:30
1 parent 5418438339
commit 9e9ce1db22
43 files changed
+744 -137

No files matched your search

+3
View File
@@ -14,6 +14,7 @@ const wantsStories = () => {
* @param {{viewer: import('cesium').Viewer,
* director: import('../scenes/director.js').SceneDirector,
* store: ReturnType<typeof import('../ui/stores/stories.js').useStoriesStore>,
* onEnter?: () => void,
* onLoadFailed?: () => void,
* loadModules?: () => Promise<[object, object]>}} options `onLoadFailed`
* tells the person a click did nothing because Stories could not load;
@@ -26,6 +27,7 @@ export function bootStories({
viewer,
director,
store,
onEnter,
onLoadFailed,
available = true,
loadModules = () =>
@@ -67,6 +69,7 @@ export function bootStories({
viewer,
director,
store,
onEnter,
});
store.connect(real, listStories());
return real;
+18 -4
View File
@@ -70,7 +70,14 @@ test('the shell loads the controller on first open, then steps aside', async ()
setActivePinia(createPinia());
history.replaceState(null, '', '/');
const store = useStoriesStore();
const shell = bootStories({ viewer, director, store });
const entries = [];
const shell = bootStories({
viewer,
director,
store,
onEnter: () =>
entries.push(document.body.classList.contains('stories-mode')),
});
assert.equal(store.controller, shell);
assert.equal(store.stories.length, 0, 'no catalog until Stories is used');
const engineLoaded = () =>
@@ -78,7 +85,14 @@ test('the shell loads the controller on first open, then steps aside', async ()
/stories\/(controller|catalog|protoEngine)\.js$/.test(u),
);
assert.equal(engineLoaded(), false, 'GEV starts without the Stories chunk');
await shell.open();
let current = true;
const staleOpen = shell.open({ isCurrent: () => current });
current = false;
assert.equal(await staleOpen, false);
assert.equal(store.mode, 'closed');
assert.deepEqual(entries, []);
assert.equal(await shell.open(), true);
assert.deepEqual(entries, [true]);
assert.equal(engineLoaded(), true);
assert.notEqual(store.controller, shell);
assert.ok(store.stories.length > 0);
@@ -115,10 +129,10 @@ test('a failed load is reported and the next open tries again', async () => {
? Promise.reject(new Error('offline'))
: Promise.all([import('./controller.js'), import('./catalog.js')]),
});
await shell.open();
assert.equal(await shell.open(), undefined);
assert.equal(failures, 1);
assert.equal(store.mode, 'closed');
await shell.open();
assert.equal(await shell.open(), true);
assert.equal(attempts, 2, 'the failure was not kept');
assert.equal(store.mode, 'explore');
shell.destroy();
+150 -35
View File
@@ -19,6 +19,20 @@ import { registerNavigationAuthorityListener } from '../navigationPolicy.js';
const PARAM_STORIES = 'stories';
const PARAM_STORY = 'story';
const PARAM_STORY_TIME = 'storyTime';
const PARAM_SELECTED_STORY = 'selectedStory';
function exploreSelection(params) {
const id = params.get(PARAM_SELECTED_STORY);
const story = id ? storyById(id) : null;
return story && !story.mine ? story.id : null;
}
function storyTime(params) {
const value = params.get(PARAM_STORY_TIME);
const ms = Number(value);
return value && /^\d+$/.test(value) && Number.isSafeInteger(ms) ? ms : 0;
}
/**
* Stories: Explore (the story list over the person's own Cesium globe, with
@@ -34,19 +48,28 @@ const PARAM_STORY = 'story';
*
* @param {{viewer: Cesium.Viewer,
* director: import('../scenes/director.js').SceneDirector,
* store: ReturnType<typeof import('../ui/stores/stories.js').useStoriesStore>}} options
* store: ReturnType<typeof import('../ui/stores/stories.js').useStoriesStore>,
* onEnter?: () => void}} options
*/
export function createStoriesController({ viewer, director, store }) {
export function createStoriesController({ viewer, director, store, onEnter }) {
let player = null;
let saved = null;
// The view a Close is about to restore, once a stopping scene has settled.
let pendingView = null;
// Reopening reuses the saved view, but supersedes the old close request.
let closeGeneration = 0;
let removeReadout = null;
let destroyed = false;
// Close steps back over Stories' own entries; the landing popstate is ours.
let closingHistory = false;
// A Back to Explore traversal is on its way.
let steppingBack = false;
let backTimer = 0;
let readyTimer = 0;
function cancelInitialLink() {
clearTimeout(readyTimer);
readyTimer = 0;
}
// A call made while a traversal is landing runs from the entry it lands on,
// so two traversals never stack (Close after Back to Explore would step
// one entry too far).
@@ -78,7 +101,7 @@ export function createStoriesController({ viewer, director, store }) {
// A draft's THUMBNAIL or tags.
'story-details-changed',
]);
const unsubscribeScenes = director.subscribe?.((change) => {
const unsubscribeScenes = director.subscribe?.(({ change }) => {
if (!LIST_CHANGES.has(change?.type) || destroyed) return;
store.stories = markRaw(listStories());
if (removePins) {
@@ -96,6 +119,8 @@ export function createStoriesController({ viewer, director, store }) {
const url = new URL(location.href);
url.searchParams.delete(PARAM_STORIES);
url.searchParams.delete(PARAM_STORY);
url.searchParams.delete(PARAM_STORY_TIME);
url.searchParams.delete(PARAM_SELECTED_STORY);
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
if (Object.keys(params).length) url.hash = '';
return url.toString();
@@ -168,6 +193,7 @@ export function createStoriesController({ viewer, director, store }) {
}
const onPopState = () => {
if (destroyed) return;
cancelInitialLink();
const queued = afterTraversal;
afterTraversal = null;
try {
@@ -177,6 +203,8 @@ export function createStoriesController({ viewer, director, store }) {
}
};
function handlePopState() {
clearTimeout(backTimer);
backTimer = 0;
steppingBack = false;
if (closingHistory) {
closingHistory = false;
@@ -189,12 +217,24 @@ export function createStoriesController({ viewer, director, store }) {
}
const params = new URLSearchParams(location.search);
const id = params.get(PARAM_STORY);
if (id && storyById(id)) controller.openStory(id, { fromHistory: true });
if (id && storyById(id))
controller.openStory(id, {
fromHistory: true,
atMs: storyTime(params),
});
else if (params.has(PARAM_STORIES))
store.mode === 'closed'
? controller.open({ fromHistory: true })
: controller.backToExplore({ fromHistory: true });
else if (store.mode !== 'closed') controller.close({ fromHistory: true });
else if (pendingView) {
// Another browser navigation landed while the scene was stopping.
// The destination owns the globe now, so discard the earlier restore.
pendingView = null;
closeGeneration += 1;
document.body.classList.remove('stories-mode');
shareLink()?.holdWrites?.(false);
}
}
addEventListener('popstate', onPopState);
@@ -209,9 +249,8 @@ export function createStoriesController({ viewer, director, store }) {
time: viewer.clock.currentTime.clone(),
animate: viewer.clock.shouldAnimate,
openedAt: Date.now(),
// The share state (layers, style, camera) as it was, for after a GEV
// scene has changed it.
hash: location.hash,
// Story history entries have no hash. Capture the live globe state.
hash: shareLink()?.snapshotHash?.() ?? location.hash,
ranScene: false,
// Whether a scene played during this visit, which selects it in
// AUTHORING (ranScene clears once Back to Explore has undone the look).
@@ -585,7 +624,9 @@ export function createStoriesController({ viewer, director, store }) {
saved = pendingView;
pendingView = null;
} else saveView();
closeGeneration += 1;
document.body.classList.add('stories-mode');
onEnter?.();
return true;
}
@@ -678,34 +719,41 @@ export function createStoriesController({ viewer, director, store }) {
}
const controller = {
open({ fromHistory = false } = {}) {
open({ fromHistory = false, isCurrent } = {}) {
if (destroyed || (isCurrent && !isCurrent())) return false;
cancelInitialLink();
if (!fromHistory && traversing()) {
afterTraversal = () => controller.open();
return;
if (!isCurrent) afterTraversal = () => controller.open();
return false;
}
if (store.mode !== 'closed') return;
if (!enterStories()) return;
store.picked = null;
if (store.mode !== 'closed' || !enterStories()) return false;
store.picked = fromHistory
? exploreSelection(new URLSearchParams(location.search))
: null;
showExplore();
if (!fromHistory) navigate({ [PARAM_STORIES]: 'explore' }, true);
return true;
},
close({ fromHistory = false, keepCamera = false } = {}) {
if (destroyed) return;
cancelInitialLink();
if (store.mode === 'closed') return;
if (!fromHistory && traversing()) {
afterTraversal = () => controller.close({ keepCamera });
return;
}
const generation = ++closeGeneration;
dropPlayer();
stopPins();
viewer.camera.cancelFlight();
// GEV comes back on screen now; `stories-mode` (keyboard guard, voice
// refusal) stays until the view is back, so nothing acts on GEV under
// a scene that is still tearing down.
// Close the Stories screens now and keep their input guard until the
// scene and share view have finished restoring.
document.body.classList.remove('stories-scene');
store.mode = 'closed';
store.storyId = null;
store.picked = null;
store.expanded = false;
store.toast = '';
const was = saved;
saved = null;
pendingView = was;
@@ -727,9 +775,14 @@ export function createStoriesController({ viewer, director, store }) {
canvas.removeEventListener('pointerdown', onDrag);
stopWatching();
// Reopened meanwhile (the reopen took `was` over), or torn down.
if (pendingView !== was || destroyed || viewer.isDestroyed?.()) return;
if (
generation !== closeGeneration ||
pendingView !== was ||
destroyed ||
viewer.isDestroyed?.()
)
return;
pendingView = null;
document.body.classList.remove('stories-mode');
if (!navigated) restoreView(was, { keepCamera });
// AUTHORING shows what it showed before, unless the person opened
// the story there.
@@ -740,6 +793,7 @@ export function createStoriesController({ viewer, director, store }) {
// over the earlier entry instead.
if (!keepCamera && !navigated) restoreShareState(was);
shareLink()?.holdWrites?.(false);
document.body.classList.remove('stories-mode');
};
if (fromHistory) afterLanding();
else leaveHistory(afterLanding);
@@ -759,6 +813,8 @@ export function createStoriesController({ viewer, director, store }) {
* @param {{autoplay?: boolean, fromHistory?: boolean, atMs?: number}} [opts]
*/
openStory(id, { autoplay = false, fromHistory = false, atMs = 0 } = {}) {
if (destroyed) return;
cancelInitialLink();
const story = storyById(id);
if (!story) return;
if (!fromHistory && traversing()) {
@@ -777,6 +833,9 @@ export function createStoriesController({ viewer, director, store }) {
store.sheet = 'peek';
store.bigPlay = !autoplay;
syncSceneShots(story);
atMs = Number.isFinite(atMs)
? Math.max(0, Math.min(story.durationMs, Math.round(atMs)))
: 0;
store.playback = {
wallMs: atMs,
worldMs: null,
@@ -796,15 +855,26 @@ export function createStoriesController({ viewer, director, store }) {
// Opened paused, a scene frames its first shot (or the moment asked).
if (atMs || !autoplay) player.seek(atMs);
if (autoplay) player.play();
if (!fromHistory) navigate({ [PARAM_STORY]: story.id }, true);
if (!fromHistory)
navigate(
atMs
? { [PARAM_STORY]: story.id, [PARAM_STORY_TIME]: atMs }
: { [PARAM_STORY]: story.id },
true,
);
if (!store.hintSeen) {
store.hintSeen = true;
store.flashHint(t('Drag the map to explore · Resume any time'), 4200);
}
},
backToExplore({ fromHistory = false } = {}) {
// A second press while the first is landing does nothing more.
if (!fromHistory && traversing()) return;
if (destroyed) return;
cancelInitialLink();
// A second Back keeps the traversal, but cancels a story queued since.
if (!fromHistory && traversing()) {
if (steppingBack) afterTraversal = null;
return;
}
dropPlayer();
store.expanded = false;
if (!fromHistory) {
@@ -816,7 +886,9 @@ export function createStoriesController({ viewer, director, store }) {
if (params.has(PARAM_STORY) && to != null && depth() > to) {
steppingBack = true;
// A step that never lands gives up, as Close's does.
setTimeout(() => {
clearTimeout(backTimer);
backTimer = setTimeout(() => {
backTimer = 0;
if (!steppingBack) return;
steppingBack = false;
const queued = afterTraversal;
@@ -827,6 +899,10 @@ export function createStoriesController({ viewer, director, store }) {
return;
}
navigate({ [PARAM_STORIES]: 'explore' }, true);
} else {
const params = new URLSearchParams(location.search);
if (params.has(PARAM_SELECTED_STORY))
store.picked = exploreSelection(params);
}
showExplore();
},
@@ -855,7 +931,10 @@ export function createStoriesController({ viewer, director, store }) {
player.pause();
// After Close has settled: the scene's teardown restores GEV's look
// and panels, which would fold the Scenes panel again.
await controller.close({ keepCamera: true });
const closing = controller.close({ keepCamera: true });
const generation = closeGeneration;
await closing;
if (destroyed || generation !== closeGeneration) return;
director.selectShot?.(s.sceneId, shotId);
director.styleManager?.setPanelCollapsed?.('scene-panel', false, {
explicit: true,
@@ -871,15 +950,35 @@ export function createStoriesController({ viewer, director, store }) {
shareLink() {
const here = new URL(location.href);
const url = new URL(here.pathname, here.origin);
for (const key of [PARAM_STORIES, PARAM_STORY])
if (here.searchParams.has(key))
url.searchParams.set(key, here.searchParams.get(key));
const id = store.mode === 'story' ? store.storyId : store.picked;
const story = id ? storyById(id) : null;
if (store.mode === 'story' && story && !story.mine) {
url.searchParams.set(PARAM_STORY, story.id);
const ms = Math.round(store.playback.wallMs);
if (Number.isSafeInteger(ms) && ms > 0)
url.searchParams.set(PARAM_STORY_TIME, String(ms));
} else {
url.searchParams.set(PARAM_STORIES, 'explore');
if (story && !story.mine)
url.searchParams.set(PARAM_SELECTED_STORY, story.id);
}
return url.toString();
},
destroy() {
if (destroyed) return;
controller.close();
destroyed = true;
cancelInitialLink();
clearTimeout(backTimer);
clearTimeout(landingTimer);
steppingBack = closingHistory = false;
afterTraversal = landed = null;
dropPlayer();
stopPins();
viewer.camera.cancelFlight();
store.mode = 'closed';
store.storyId = null;
store.picked = null;
store.expanded = false;
// Torn down mid-close: what the close would undo once settled is
// undone now (keyboard and voice guard, held share writes).
document.body.classList.remove('stories-mode', 'stories-scene');
@@ -893,7 +992,6 @@ export function createStoriesController({ viewer, director, store }) {
canvas.removeEventListener('pointerup', onExploreInput);
canvas.removeEventListener('pointercancel', onExploreInput);
canvas.removeEventListener('wheel', onExploreInput);
stopTour();
canvas.removeEventListener('pointerup', onUp);
},
};
@@ -901,17 +999,21 @@ export function createStoriesController({ viewer, director, store }) {
// A shared link opens where it points once GEV has finished loading.
const params = new URLSearchParams(location.search);
if (params.has(PARAM_STORY) || params.has(PARAM_STORIES)) {
const id = params.get(PARAM_STORY);
const started = Date.now();
const whenReady = () => {
readyTimer = 0;
if (destroyed) return;
const params = new URLSearchParams(location.search);
if (!params.has(PARAM_STORY) && !params.has(PARAM_STORIES)) return;
const id = params.get(PARAM_STORY);
const atMs = storyTime(params);
const loading = document.getElementById('loading-screen');
if (
loading &&
!loading.classList.contains('hidden') &&
Date.now() - started < 30000
) {
setTimeout(whenReady, 300);
readyTimer = setTimeout(whenReady, 300);
return;
}
// A reload, or Back/Forward into a Stories entry: its history is
@@ -920,11 +1022,19 @@ export function createStoriesController({ viewer, director, store }) {
// GEV's boot wrote its share hash onto the entry: Stories' entries
// carry none.
const own = Object.fromEntries(
[...params].filter(([k]) => k === PARAM_STORIES || k === PARAM_STORY),
[...params].filter(
([key]) =>
key === PARAM_STORIES ||
key === PARAM_STORY ||
(key === PARAM_STORY_TIME && id && atMs) ||
(key === PARAM_SELECTED_STORY &&
!id &&
params.has(PARAM_STORIES)),
),
);
history.replaceState(history.state, '', urlFor(own));
if (id && storyById(id))
controller.openStory(id, { fromHistory: true });
controller.openStory(id, { fromHistory: true, atMs });
else {
controller.open({ fromHistory: true });
if (id) store.flashToast(t('That story is not available here'));
@@ -933,19 +1043,24 @@ export function createStoriesController({ viewer, director, store }) {
}
// A new link: Explore sits under a story it opens, so Back lands there.
const landing = { stories: 'explore', depth: 0, explore: 0 };
const selected =
!id && params.has(PARAM_STORIES) ? exploreSelection(params) : null;
history.replaceState(
{ ...(history.state ?? {}), gevStories: landing },
'',
urlFor({ [PARAM_STORIES]: 'explore' }),
urlFor({
[PARAM_STORIES]: 'explore',
...(selected ? { [PARAM_SELECTED_STORY]: selected } : {}),
}),
);
if (id && storyById(id)) controller.openStory(id);
if (id && storyById(id)) controller.openStory(id, { atMs });
else {
controller.open({ fromHistory: true });
// A story saved on someone else's device, or no longer here.
if (id) store.flashToast(t('That story is not available here'));
}
};
setTimeout(whenReady, 300);
readyTimer = setTimeout(whenReady, 300);
}
if (import.meta.env?.DEV) globalThis.__gevStories = { viewer, controller };
+236 -31
View File
@@ -6,6 +6,7 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import { createPinia, setActivePinia } from 'pinia';
import { createStateChannel } from '../app/stateChannel.js';
registerHooks({
load(url, context, next) {
@@ -123,21 +124,31 @@ function setup(options = {}) {
const store = useStoriesStore();
const viewer = fakeViewer();
const director = fakeDirector(options);
const controller = createStoriesController({ viewer, director, store });
const controller = createStoriesController({
viewer,
director,
store,
onEnter: options.onEnter,
});
store.connect(controller, listStories());
return { store, viewer, director, controller };
}
test('Close steps back over every entry Stories pushed', async () => {
const { controller } = setup();
const entered = [];
const { controller } = setup({
onEnter: () =>
entered.push(document.body.classList.contains('stories-mode')),
});
const steps = [];
const go = history.go;
history.go = (n) => steps.push(n);
try {
controller.open();
assert.equal(controller.open(), true);
assert.equal(history.state.gevStories.depth, 1);
controller.openStory('gev:flights-radar');
assert.equal(history.state.gevStories.depth, 2);
assert.deepEqual(entered, [true]);
await controller.close();
// Back after Close would otherwise land on ?story=… and reopen it.
assert.deepEqual(steps, [-2]);
@@ -158,8 +169,13 @@ test('Close from a Stories link clears Stories from the URL in place', async ()
});
test('Cockpit keeps the camera: Stories does not open', () => {
const { controller, store, director } = setup({ refuse: true });
controller.open();
const { controller, store, director } = setup({
refuse: true,
onEnter: () => assert.fail('refused entry should not stop voice'),
});
assert.equal(controller.open({ isCurrent: () => false }), false);
assert.deepEqual(director.claims, []);
assert.equal(controller.open(), false);
assert.equal(store.mode, 'closed');
assert.equal(director.stops, 0);
// The navigation policy shows the refusal; it needs a sentence for the noun.
@@ -169,17 +185,21 @@ test('Cockpit keeps the camera: Stories does not open', () => {
});
test('reopening while a scene tears down keeps the view from before Stories', async () => {
const { controller, viewer, director } = setup();
const { controller, viewer, director, store } = setup();
const before = viewer.camera.positionWC;
controller.open();
controller.openStory('gev:bhote-koshi-nepal-scene');
director.running = true;
const closing = controller.close();
const toast = store.hostedToast;
const closing = controller.openInGev();
// The scene has moved the camera; the person opens Stories again at once.
viewer.camera.positionWC = new Cesium.Cartesian3(9, 9, 9);
controller.open();
director.running = false;
await closing;
assert.deepEqual(director.selected, []);
assert.deepEqual(director.styleManager.panels, []);
assert.equal(store.hostedToast, toast);
assert.equal(
viewer.camera.views.length,
0,
@@ -204,14 +224,36 @@ test('after a GEV scene, the person’s view comes back once the scene has torn
controller.destroy();
});
test('a share link carries the story, not the sender’s camera and layers', () => {
const { controller } = setup();
test('Share links to the selection and captures the playing story’s timeline', () => {
const { controller, store } = setup();
controller.open();
assert.equal(new URL(controller.shareLink()).search, '?stories=explore');
controller.pick('gev:orbital-watch');
assert.equal(
new URL(controller.shareLink()).search,
'?stories=explore&selectedStory=gev%3Aorbital-watch',
);
controller.openStory('gev:flights-radar');
store.playback = { ...store.playback, playing: true, wallMs: 1250.6 };
const link = new URL(controller.shareLink());
assert.equal(link.searchParams.get('story'), 'gev:flights-radar');
assert.deepEqual(
[...link.searchParams],
[
['story', 'gev:flights-radar'],
['storyTime', '1251'],
],
);
assert.equal(link.hash, '');
assert.equal(link.searchParams.has('x'), false, 'no other query parameters');
store.playback.wallMs = 1850;
assert.equal(
new URL(controller.shareLink()).searchParams.get('storyTime'),
'1850',
);
controller.backToExplore({ fromHistory: true });
assert.equal(
new URL(controller.shareLink()).search,
'?stories=explore&selectedStory=gev%3Aflights-radar',
);
controller.destroy();
});
@@ -227,7 +269,7 @@ function recordGo(t) {
}
test('Back to Explore after a related story steps past both stories', (t) => {
const { controller } = setup();
const { controller, store } = setup();
const steps = recordGo(t);
controller.open();
controller.openStory('gev:flights-radar');
@@ -235,9 +277,19 @@ test('Back to Explore after a related story steps past both stories', (t) => {
assert.equal(history.state.gevStories.depth, 3);
assert.equal(history.state.gevStories.explore, 1);
controller.backToExplore();
// A second click before the traversal lands does nothing more.
controller.openStory('gev:flights-radar'); // queued behind Back
// The latest Back cancels that queued story without another traversal.
controller.backToExplore();
assert.deepEqual(steps, [-2]);
history.replaceState(
{ gevStories: { stories: 'explore', depth: 1, explore: 1 } },
'',
'/?stories=explore',
);
dispatchEvent(new PopStateEvent('popstate'));
assert.equal(store.mode, 'explore');
assert.equal(store.storyId, null);
assert.equal(history.state.gevStories.depth, 1, 'no queued story pushed');
controller.destroy();
});
@@ -246,13 +298,13 @@ test('a second Close after a quick reopen still waits for the scene to tear down
controller.open();
controller.openStory('gev:bhote-koshi-nepal-scene');
director.running = true;
controller.close();
const first = controller.close({ keepCamera: true });
controller.open(); // reopened while the scene is still stopping
const closing = controller.close(); // Explore: no GEV story on screen
assert.ok(closing, 'this Close waits too');
assert.equal(viewer.camera.views.length, 0, 'nothing restored yet');
setTimeout(() => (director.running = false), 120);
await closing;
await Promise.all([first, closing]);
assert.equal(viewer.camera.views.length, 1);
controller.destroy();
});
@@ -262,7 +314,7 @@ test('a reload of a Stories entry keeps its history; Close steps all of it back'
history.replaceState(
{ gevStories: { story: 'gev:flights-radar', depth: 2, explore: 1 } },
'',
'/?story=gev%3Aflights-radar',
'/?story=gev%3Aflights-radar&storyTime=1250',
);
const steps = recordGo(t);
const store = useStoriesStore();
@@ -274,6 +326,8 @@ test('a reload of a Stories entry keeps its history; Close steps all of it back'
store.connect(controller, listStories());
await new Promise((resolve) => setTimeout(resolve, 400));
assert.equal(store.storyId, 'gev:flights-radar');
assert.equal(store.playback.wallMs, 1250);
assert.equal(store.playback.playing, false);
assert.equal(history.state.gevStories.depth, 2, 'no extra entry pushed');
await controller.close();
assert.deepEqual(steps, [-2]);
@@ -282,7 +336,7 @@ test('a reload of a Stories entry keeps its history; Close steps all of it back'
test('a new story link puts Explore under the story', async (t) => {
setActivePinia(createPinia());
history.replaceState(null, '', '/?story=gev%3Aflights-radar');
history.replaceState(null, '', '/?story=gev%3Aflights-radar&storyTime=1250');
const steps = recordGo(t);
const store = useStoriesStore();
const controller = createStoriesController({
@@ -293,8 +347,11 @@ test('a new story link puts Explore under the story', async (t) => {
store.connect(controller, listStories());
await new Promise((resolve) => setTimeout(resolve, 400));
assert.equal(store.storyId, 'gev:flights-radar');
assert.equal(store.playback.wallMs, 1250);
assert.equal(store.playback.playing, false);
assert.deepEqual(history.state.gevStories, {
story: 'gev:flights-radar',
storyTime: 1250,
depth: 1,
explore: 0,
});
@@ -377,6 +434,29 @@ function land(hash = '#v=2&lat=1') {
dispatchEvent(new PopStateEvent('popstate'));
}
test('a second browser Back during scene teardown supersedes the Stories restore', async () => {
const { controller, director, store, viewer } = setup();
const share = director.styleManager.shareLinkManager;
const holds = [];
share.holdWrites = (held) => holds.push(held);
director.selection = () => ({ sceneId: 'previous', shotId: 'shot-1' });
controller.open();
controller.openStory('gev:bhote-koshi-nepal-scene');
director.running = true;
land();
assert.equal(store.mode, 'closed');
land('#v=2&lat=2');
director.running = false;
await tick(0);
assert.equal(location.hash, '#v=2&lat=2');
assert.equal(viewer.camera.views.length, 0);
assert.deepEqual(share.restored, []);
assert.deepEqual(director.selected, []);
assert.deepEqual(holds, [true, false]);
assert.equal(document.body.classList.contains('stories-mode'), false);
controller.destroy();
});
test('Close after a GEV scene puts the share state back once the step back lands', async (t) => {
const { controller, director } = setup();
const steps = recordGo(t);
@@ -386,8 +466,10 @@ test('Close after a GEV scene puts the share state back once the step back lands
await controller.close();
assert.deepEqual(steps, [-2]);
assert.deepEqual(restored, [], 'not before the landing');
assert.equal(document.body.classList.contains('stories-mode'), true);
land();
assert.deepEqual(restored, ['v=2&lat=1']);
assert.equal(document.body.classList.contains('stories-mode'), false);
controller.destroy();
});
@@ -435,7 +517,7 @@ test('a reloaded Stories entry drops the share hash GEV wrote on boot', async ()
history.replaceState(
{ gevStories: { stories: 'explore', depth: 1, explore: 1 } },
'',
'/?stories=explore#v=2&lat=7',
'/?stories=explore&selectedStory=gev%3Aorbital-watch#v=2&lat=7',
);
const store = useStoriesStore();
const controller = createStoriesController({
@@ -447,7 +529,19 @@ test('a reloaded Stories entry drops the share hash GEV wrote on boot', async ()
await new Promise((resolve) => setTimeout(resolve, 400));
assert.equal(store.mode, 'explore');
assert.equal(location.hash, '');
assert.equal(new URL(location.href).searchParams.get('stories'), 'explore');
assert.equal(store.picked, 'gev:orbital-watch');
assert.equal(
location.search,
'?stories=explore&selectedStory=gev%3Aorbital-watch',
);
const exploreState = history.state;
const exploreUrl = location.href;
controller.openStory('gev:flights-radar');
assert.equal(location.search, '?story=gev%3Aflights-radar');
history.replaceState(exploreState, '', exploreUrl);
dispatchEvent(new PopStateEvent('popstate'));
assert.equal(store.mode, 'explore');
assert.equal(store.picked, 'gev:orbital-watch');
controller.destroy();
});
@@ -478,13 +572,13 @@ test('a scene saved to Stories from AUTHORING joins the list at once', () => {
shotCount: 2,
},
];
listener({ type: 'stories-changed' });
listener({ change: { type: 'stories-changed' } });
const story = store.byId('gev:my-scene');
assert.equal(story?.title, 'Harbour at dawn');
assert.equal(story.mine, true);
assert.equal(store.stories[0].id, 'gev:my-scene', 'the person’s own first');
mine = [];
listener({ type: 'stories-changed' });
listener({ change: { type: 'stories-changed' } });
assert.equal(store.byId('gev:my-scene'), null, 'removed from Stories');
controller.destroy();
assert.equal(listener, null, 'unsubscribed');
@@ -492,10 +586,13 @@ test('a scene saved to Stories from AUTHORING joins the list at once', () => {
test('Back to Explore after a scene puts the look back once the scene has torn down, then frames Explore', async () => {
const { controller, viewer, director } = setup();
history.replaceState(null, '', '/');
const flights = [];
viewer.camera.flyTo = (f) => flights.push(f);
viewer.camera.flyToBoundingSphere = (f) => flights.push(f);
const share = director.styleManager.shareLinkManager;
const original = 'v=2&lat=1&style=normal';
share.snapshotHash = () => original;
// GEV's restore writes the hash it applies; Explore's address drops it.
share.restore = function (hash) {
this.restored.push(hash);
@@ -506,13 +603,18 @@ test('Back to Explore after a scene puts the look back once the scene has torn d
controller.openStory('gev:bhote-koshi-nepal-scene');
director.running = true;
flights.length = 0;
controller.backToExplore({ fromHistory: true });
history.replaceState(
{ gevStories: { stories: 'explore', depth: 1, explore: 1 } },
'',
'/?stories=explore',
);
dispatchEvent(new PopStateEvent('popstate'));
await tick(30);
assert.deepEqual(share.restored, [], 'not under the stopping scene');
assert.equal(flights.length, 0, 'Explore waits for the scene');
director.running = false;
await tick(40);
assert.deepEqual(share.restored, ['v=2&lat=1']);
assert.deepEqual(share.restored, [original]);
assert.equal(location.hash, '', 'the address names only Stories');
assert.equal(flights.length, 1, 'then Explore frames the globe');
// A later Back to Explore with no scene in between restores nothing more.
@@ -579,9 +681,16 @@ test('Explore tours from story to story once idle, stops on a pick or a touch, a
test('a new Explore link keeps the address free of the share hash GEV writes as Stories opens', async () => {
setActivePinia(createPinia());
history.replaceState(null, '', '/?stories=explore');
history.replaceState(
null,
'',
'/?stories=explore&selectedStory=gev%3Aorbital-watch',
);
const store = useStoriesStore();
const director = fakeDirector();
const playbackCalls = [];
director.startScene = () => playbackCalls.push('start');
director.seekScene = () => playbackCalls.push('seek');
// Holding writes first writes the view change still waiting.
director.styleManager.shareLinkManager.holdWrites = (held) => {
if (held) history.replaceState(history.state, '', `${location.search}#v=2`);
@@ -595,7 +704,13 @@ test('a new Explore link keeps the address free of the share hash GEV writes as
await tick(400);
assert.equal(store.mode, 'explore');
assert.equal(location.hash, '');
assert.equal(location.search, '?stories=explore');
assert.equal(
location.search,
'?stories=explore&selectedStory=gev%3Aorbital-watch',
);
assert.equal(store.picked, 'gev:orbital-watch');
assert.equal(store.storyId, null);
assert.deepEqual(playbackCalls, []);
controller.destroy();
});
@@ -690,13 +805,12 @@ test('Close after Back to Explore still puts AUTHORING’s selection back', asyn
controller.destroy();
});
test('the person’s unsaved scenes list as drafts, which cannot be shared', () => {
test('unsaved scenes appear as local drafts and disappear when deleted', () => {
setActivePinia(createPinia());
history.replaceState(null, '', '/');
const store = useStoriesStore();
const director = fakeDirector();
director.subscribe = () => () => {};
director.draftScenes = () => [
const drafts = [
{
id: 'sketch',
title: 'Sketch',
@@ -706,6 +820,9 @@ test('the person’s unsaved scenes list as drafts, which cannot be shared', ()
tags: [],
},
];
const channel = createStateChannel(() => ({ scenes: drafts }));
director.subscribe = channel.subscribe;
director.draftScenes = () => drafts;
const controller = createStoriesController({
viewer: fakeViewer(),
director,
@@ -715,6 +832,10 @@ test('the person’s unsaved scenes list as drafts, which cannot be shared', ()
const draft = store.byId('gev:sketch');
assert.equal(draft?.draft, true);
assert.equal(draft.mine, true, 'local only: no Share');
drafts.length = 0;
channel.publish({ type: 'scene-deleted' });
controller.open();
assert.equal(store.byId('gev:sketch'), null);
controller.destroy();
});
@@ -755,20 +876,31 @@ test('the tour waits again after a wheel turn or a cancelled press, and a list c
assert.equal(flights.length, before + 1, `${type}: then tours`);
}
canvas.dispatchEvent(new Event('pointerdown'));
listener({ type: 'stories-changed' });
listener({ change: { type: 'stories-changed' } });
const held = flights.length;
t.mock.timers.tick(30000);
assert.equal(flights.length, held, 'nothing flies under the drag');
again.destroy();
});
test('teardown mid-close lets the share link write again', () => {
const { controller, director } = setup();
test('teardown mid-close releases share writes and cancels a queued reopen', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const { controller, director, store, viewer } = setup();
recordGo(t);
const held = [];
director.styleManager.shareLinkManager.holdWrites = (on) => held.push(on);
let flights = 0;
viewer.camera.flyTo = () => flights++;
controller.open();
await controller.close();
controller.open(); // queued until the Close traversal lands
controller.destroy();
const before = flights;
assert.equal(held.at(-1), false);
t.mock.timers.tick(3000);
assert.equal(store.mode, 'closed');
assert.equal(held.at(-1), false, 'no late reopen holds writes again');
assert.equal(flights, before, 'no late flight after teardown');
});
test('place names name only a shipped scene’s untitled shots', () => {
@@ -785,3 +917,76 @@ test('place names name only a shipped scene’s untitled shots', () => {
);
controller.destroy();
});
test('new navigation cancels a delayed initial story link', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
for (const action of ['back', 'open', 'close']) {
setActivePinia(createPinia());
history.replaceState(null, '', '/?story=gev%3Aflights-radar');
const store = useStoriesStore();
const controller = createStoriesController({
viewer: fakeViewer(),
director: fakeDirector(),
store,
});
store.connect(controller, listStories());
try {
if (action === 'back') land();
else if (action === 'open') controller.open();
else controller.close();
const url = location.href;
t.mock.timers.tick(400);
assert.equal(
store.mode,
action === 'open' ? 'explore' : 'closed',
action,
);
assert.equal(store.storyId, null, action);
assert.equal(
location.href,
url,
'the old link does not rewrite navigation',
);
} finally {
controller.destroy();
}
}
});
test('an earlier Back timeout cannot release a later traversal', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const { controller, store } = setup();
const steps = recordGo(t);
const landExplore = () => {
history.replaceState(
{ gevStories: { stories: 'explore', depth: 1, explore: 1 } },
'',
'/?stories=explore',
);
dispatchEvent(new PopStateEvent('popstate'));
};
try {
controller.open();
controller.openStory('gev:flights-radar');
controller.backToExplore();
landExplore();
t.mock.timers.tick(1500);
controller.openStory('gev:orbital-watch');
controller.backToExplore();
controller.close();
t.mock.timers.tick(600);
assert.equal(store.mode, 'story', 'Close still waits for the second Back');
assert.deepEqual(steps, [-1, -1]);
landExplore();
// Drain the director's already-idle promise without advancing timers.
for (let i = 0; i < 12; i++) await Promise.resolve();
assert.equal(store.mode, 'closed');
assert.deepEqual(
steps,
[-1, -1, -1],
'queued Close runs once after landing',
);
} finally {
controller.destroy();
}
});
+6 -9
View File
@@ -44,7 +44,7 @@ export function createGevPlayer(director, story, hooks = {}) {
let shotIndex = 0;
let destroyed = false;
// Each Play, Pause, Seek and Destroy takes a new number; a Play that waited
// for a teardown or a start goes ahead only if nothing came after it.
// for a teardown or a run to finish acts only if nothing came after it.
let request = 0;
const state = () => ({
@@ -65,7 +65,8 @@ export function createGevPlayer(director, story, hooks = {}) {
wallMs = (snap.sceneElapsedSec ?? 0) * 1000;
shotIndex = snap.shotIndex ?? shotIndex;
playing = !!snap.running && !snap.seeking;
if (!playing && wallMs >= durationMs - 250) wallMs = durationMs;
if (!playing && !snap.seeking && wallMs >= durationMs - 250)
wallMs = durationMs;
tick();
});
@@ -100,13 +101,9 @@ export function createGevPlayer(director, story, hooks = {}) {
afterShotId,
preview: true,
});
if (destroyed) return;
if (mine !== request) {
// Paused or scrubbed while the run was starting: that wins.
if (result?.started)
director.seekScene(sceneId, durationMs ? wallMs / durationMs : 0);
return;
}
// startScene resolves after the run finishes, not when it starts.
// A newer pause, seek or play already owns the director by then.
if (destroyed || mine !== request) return;
if (!result?.started) {
playing = false;
tick();
+31 -8
View File
@@ -1,5 +1,5 @@
// A GEV scene's Play waits for the last run to tear down and for the new run
// to start; a Pause, Seek or Destroy meanwhile must win.
// to finish; a Pause, Seek or Destroy meanwhile must win.
import test from 'node:test';
import assert from 'node:assert/strict';
import { createGevPlayer } from './gevPlayer.js';
@@ -10,7 +10,7 @@ const story = {
chapters: [{ wallMs: 0 }, { wallMs: 30000 }],
};
/** A director whose start resolves when the test says so. */
/** A director whose run starts immediately and ends when the test says so. */
function deferredDirector({ running = false } = {}) {
const calls = [];
let release = null;
@@ -30,14 +30,15 @@ function deferredDirector({ running = false } = {}) {
seekScene: (id, f) => calls.push(`seek ${f}`),
startScene: () => {
calls.push('start');
director.running = true;
return new Promise((resolve) => {
release = () => {
director.running = true;
director.running = false;
resolve({ started: true });
};
});
},
finishStart: () => release?.(),
finishRun: () => release?.(),
};
return director;
}
@@ -70,17 +71,39 @@ test('a Seek during the last run’s teardown cancels the waiting Play', async (
assert.equal(player.state.wallMs, 30000);
});
test('a Pause while the run is starting is applied once it has started', async () => {
test('a stopped run’s completion does not repeat a newer seek', async () => {
const director = deferredDirector();
const player = createGevPlayer(director, story);
const playing = player.play();
await turns();
assert.deepEqual(director.calls, ['start']);
assert.equal(director.running, true);
player.pause();
director.finishStart();
player.seek(30000);
director.finishRun();
await playing;
// Pause seeks once when pressed, and again after the late start.
assert.equal(director.calls.filter((c) => c.startsWith('seek')).length, 2);
assert.deepEqual(director.calls, ['start', 'seek 0', 'seek 0.5']);
assert.equal(player.state.playing, false);
assert.equal(player.state.wallMs, 30000);
});
test('a paused seek keeps its exact position near the end of a story', () => {
const director = deferredDirector();
let publishClock;
director.subscribeSceneClock = (listener) => {
publishClock = listener;
return () => {};
};
const player = createGevPlayer(director, story);
player.seek(59800);
publishClock({
sceneId: story.sceneId,
sceneElapsedSec: 59.8,
sceneDurationSec: 60,
running: false,
seeking: true,
});
assert.equal(player.state.wallMs, 59800);
assert.equal(player.state.playing, false);
});