Fix Stories history and paused playback

This commit is contained in:
Manjunath committed 2026-11-16 21:38:14 +05:30
1 parent 472cc35c47
commit 2edcd5bc69
7 files changed
+144 -28

No files matched your search

+4 -2
View File
@@ -673,8 +673,10 @@ controller are separate chunks fetched together on first open, on a
fetch is reported and the next attempt tries again. Stories keeps its place
in the query, not the share-state hash, with one history entry per screen;
Close steps back over those entries, so Back afterwards does not reopen it,
and share links carry only the Stories place (`stories` / `story`), not the
sender's camera, layers or other settings. GEV's share link keeps
and share links carry the Stories screen and place. Explore links use
`stories=explore` and optionally `selectedStory`. Player links use `story`
and optionally `storyTime` in milliseconds, opening paused at that time.
GEV's share link keeps
`history.state` when it rewrites the hash, so Stories' entries survive.
Every story is a GEV scene, played on the person's own globe through
+12 -8
View File
@@ -1813,15 +1813,17 @@ export class SceneDirector {
return true;
}
/** Seek the complete authored scene state to an exact clock position. */
seekScene(sceneId, progress) {
/** Seek to an exact position. Reopening a scene can reload its authored look. */
seekScene(sceneId, progress, { reloadShot = false } = {}) {
if (this._destroyed) return Promise.resolve(false);
this.stopScene(msg('Seeking scene clock'));
const generation = this._bumpSceneSeekGeneration();
return this._trackWork(this._seekScene(sceneId, progress, generation));
return this._trackWork(
this._seekScene(sceneId, progress, generation, reloadShot),
);
}
async _seekScene(sceneId, progress, generation) {
async _seekScene(sceneId, progress, generation, reloadShot) {
const scene = this._project.scenes.find(({ id }) => id === sceneId);
if (!scene?.shots?.length) return false;
if (this._running) {
@@ -1831,10 +1833,12 @@ export class SceneDirector {
return false;
const seekState = this._sceneSeekState(scene, progress);
if (!seekState) return false;
const seeked = await this._seekLoadedShot(scene, seekState, generation);
if (this._destroyed || generation !== this._sceneSeekGeneration)
return false;
if (seeked !== null) return seeked;
if (!reloadShot) {
const seeked = await this._seekLoadedShot(scene, seekState, generation);
if (this._destroyed || generation !== this._sceneSeekGeneration)
return false;
if (seeked !== null) return seeked;
}
const result = await this._loadShot(scene.id, seekState.shot.id, {
sceneSeek: seekState,
});
+12 -2
View File
@@ -505,10 +505,13 @@ test('refused same-shot seek rollback keeps a manual toggle still in flight', as
}
});
test('same-shot seeking keeps the shortcut when declared visibility is unchanged', async () => {
const { director, dataManager, styleManager, restore } = makeDirector();
test('reopening the same shot reapplies its look while ordinary seeks reuse it', async () => {
const project = structuredClone(PROJECT_FIXTURE);
project.scenes[0].shots[0].visual.style = 'retro';
const { director, dataManager, styleManager, restore } = makeDirector({ project });
try {
await director.loadShot('scene-1', 'shot-a', { flyDuration: 0 });
await styleManager.applyVisualState({ style: 'normal' });
await dataManager.setEnabled('satellites', true);
dataManager.setEnabledCalls.length = 0;
const visualCalls = styleManager.visualCalls.length;
@@ -517,6 +520,13 @@ test('same-shot seeking keeps the shortcut when declared visibility is unchanged
assert.deepEqual(dataManager.setEnabledCalls, []);
assert.equal(styleManager.visualCalls.length, visualCalls);
assert.equal(director.getPlaybackTimingState().snapshot.sceneProgress, 0.25);
assert.equal(await director.seekScene('scene-1', 0.25, { reloadShot: true }), true);
assert.equal(styleManager.visualCalls.length, visualCalls + 1);
assert.equal(styleManager.visualStates.at(-1).style, 'retro');
const { sceneProgress, seeking } = director.getPlaybackTimingState().snapshot;
assert.equal(sceneProgress, 0.25);
assert.equal(seeking, true);
} finally {
await director.destroy();
restore();
+25 -4
View File
@@ -81,6 +81,7 @@ export function createStoriesController({
let destroyed = false;
// Close steps back over Stories' own entries; the landing popstate is ours.
let closingHistory = false;
let keepLandingView = false;
// A Back to Explore traversal is on its way.
let steppingBack = false;
let backTimer = 0;
@@ -189,14 +190,16 @@ export function createStoriesController({
let landingTimer = 0;
function finishLanding() {
clearTimeout(landingTimer);
keepLandingView = false;
const then = landed;
landed = null;
then?.();
}
function leaveHistory(then = null) {
function leaveHistory(then, keepView) {
const steps = depth();
if (steps > 0) {
closingHistory = true;
keepLandingView = keepView;
landed = then;
clearTimeout(landingTimer);
landingTimer = setTimeout(() => {
@@ -213,6 +216,20 @@ export function createStoriesController({
then?.();
}
}
// Open in GEV deliberately keeps the scene view. Clear the earlier
// entry's hash before the share manager sees popstate or hashchange;
// releasing its write hold will put the current view at this address.
const onLandingCapture = () => {
if (!closingHistory || !keepLandingView || !location.hash) return;
const url = new URL(location.href);
if (
url.searchParams.has(PARAM_STORY) ||
url.searchParams.has(PARAM_STORIES)
)
return;
url.hash = '';
history.replaceState(history.state, '', url);
};
const onPopState = () => {
if (destroyed) return;
cancelInitialLink();
@@ -259,6 +276,7 @@ export function createStoriesController({
}
}
addEventListener('popstate', onPopState);
addEventListener('popstate', onLandingCapture, true);
// ── The person's own view, kept for when Stories closes ──
function saveView() {
@@ -915,7 +933,7 @@ export function createStoriesController({
document.body.classList.remove('stories-mode');
};
if (fromHistory) afterLanding();
else leaveHistory(afterLanding);
else leaveHistory(afterLanding, keepCamera || navigated);
};
return directorSettled(director).then(finish);
},
@@ -971,8 +989,9 @@ export function createStoriesController({
// Related stories show their card images too, after a link.
drawThumbs();
startCameraReadout();
// Opened paused, a scene frames its first shot (or the moment asked).
if (atMs || !autoplay) player.seek(atMs);
// Explore may have restored a different look while the director still
// has this shot selected. Reopening restores the authored look too.
if (atMs || !autoplay) player.seek(atMs, { reloadShot: true });
if (autoplay) player.play();
if (!fromHistory)
navigate(
@@ -1090,6 +1109,7 @@ export function createStoriesController({
clearTimeout(backTimer);
clearTimeout(landingTimer);
steppingBack = closingHistory = false;
keepLandingView = false;
afterTraversal = landed = null;
dropPlayer();
stopPins();
@@ -1106,6 +1126,7 @@ export function createStoriesController({
useSavedScenes(null);
useInstalledScenes(null);
removeEventListener('popstate', onPopState);
removeEventListener('popstate', onLandingCapture, true);
canvas.removeEventListener('pointerdown', onDown);
canvas.removeEventListener('pointerdown', onExploreInput);
canvas.removeEventListener('pointerup', onExploreInput);
+24 -3
View File
@@ -8,6 +8,7 @@ import { createPinia, setActivePinia } from 'pinia';
import { createStateChannel } from '../app/stateChannel.js';
import { bindRenderer } from '../render/rendererOf.js';
import { getRenderGovernorDiagnostics } from '../renderGovernor.js';
import { ShareLinkManager } from '../sharelink.js';
const holds = () => getRenderGovernorDiagnostics().holds;
registerHooks({
@@ -533,16 +534,36 @@ test('Close after a GEV scene puts the share state back once the step back lands
});
test('Open in GEV steps back over Stories without putting the old view back', async (t) => {
const share = new ShareLinkManager(
{},
{
camera: {
on: () => () => {},
pose: () => ({ lat: 7, lon: 8, height: 1000 }),
},
},
);
t.after(() => share.destroy());
const restored = [];
// The share listener starts before Stories lazy-loads.
share.watchNavigation((state) => restored.push(state.lat));
const { controller, director, camera } = setup();
t.after(() => controller.destroy());
director.styleManager.shareLinkManager = share;
const steps = recordGo(t);
controller.open();
controller.openStory('gev:bhote-koshi-nepal-scene');
// Forward into a Stories entry has already let the share listener see
// its empty hash.
dispatchEvent(new HashChangeEvent('hashchange'));
await controller.openInGev();
assert.deepEqual(steps, [-2], 'Back never reopens Stories');
land();
assert.deepEqual(director.styleManager.shareLinkManager.restored, []);
land('#v=2&lat=1&lon=2');
dispatchEvent(new HashChangeEvent('hashchange'));
assert.deepEqual(restored, []);
assert.equal(camera.views.length, 0);
controller.destroy();
land('#v=2&lat=3&lon=4');
assert.deepEqual(restored, [3], 'ordinary navigation still restores');
});
test('a drag on the globe while a scene tears down keeps the camera where it went', async () => {
+37 -9
View File
@@ -43,6 +43,7 @@ export function createGevPlayer(director, story, hooks = {}) {
let playing = false;
let shotIndex = 0;
let destroyed = false;
let pendingSeek = null;
// Each Play, Pause, Seek and Destroy takes a new number; a Play that waited
// for a teardown or a run to finish acts only if nothing came after it.
let request = 0;
@@ -59,8 +60,7 @@ export function createGevPlayer(director, story, hooks = {}) {
if (!destroyed) hooks.onTick?.(state());
};
const unsubscribe = director.subscribeSceneClock((snap) => {
if (destroyed || !snap || snap.sceneId !== sceneId) return;
function applyClock(snap) {
if (snap.sceneDurationSec) durationMs = snap.sceneDurationSec * 1000;
wallMs = (snap.sceneElapsedSec ?? 0) * 1000;
shotIndex = snap.shotIndex ?? shotIndex;
@@ -68,8 +68,38 @@ export function createGevPlayer(director, story, hooks = {}) {
if (!playing && !snap.seeking && wallMs >= durationMs - 250)
wallMs = durationMs;
tick();
}
const unsubscribe = director.subscribeSceneClock((snap) => {
if (destroyed || !snap || snap.sceneId !== sceneId) return;
if (pendingSeek) {
if (snap.seeking) pendingSeek.snapshot = snap;
return;
}
applyClock(snap);
});
function seekToCurrentTime(options) {
playing = false;
const seek = { snapshot: null };
// seekScene stops the old run synchronously. Keep its last clock
// snapshots from overwriting this position while the seek settles.
pendingSeek = seek;
const settle = (applied) => {
if (pendingSeek !== seek) return;
pendingSeek = null;
if (applied === true && seek.snapshot) applyClock(seek.snapshot);
};
Promise.resolve(
director.seekScene(
sceneId,
durationMs ? wallMs / durationMs : 0,
options,
),
).then(settle, () => settle(false));
tick();
}
// A run from the previous story may still be releasing the camera.
async function idle() {
if (!director.running) return;
@@ -87,6 +117,7 @@ export function createGevPlayer(director, story, hooks = {}) {
const mine = ++request;
await idle();
if (destroyed || mine !== request || director.running) return;
pendingSeek = null;
// Natural completion is rounded up by the clock subscriber; a
// paused shared time inside the last 250 ms is not completion.
if (wallMs >= durationMs) {
@@ -113,17 +144,13 @@ export function createGevPlayer(director, story, hooks = {}) {
},
pause() {
request += 1;
playing = false;
director.seekScene(sceneId, durationMs ? wallMs / durationMs : 0);
tick();
seekToCurrentTime();
},
seek(ms) {
seek(ms, options) {
request += 1;
wallMs = Math.max(0, Math.min(durationMs, ms));
shotIndex = chapterAt(story, wallMs);
playing = false;
director.seekScene(sceneId, durationMs ? wallMs / durationMs : 0);
tick();
seekToCurrentTime(options);
},
resume() {
return this.play();
@@ -135,6 +162,7 @@ export function createGevPlayer(director, story, hooks = {}) {
destroy() {
request += 1;
destroyed = true;
pendingSeek = null;
unsubscribe?.();
director.stopScene();
},
+30
View File
@@ -84,10 +84,40 @@ test('a stopped run’s completion does not repeat a newer seek', async () => {
await turns();
assert.deepEqual(director.calls, ['start']);
assert.equal(director.running, true);
const oldClock = {
sceneElapsedSec: 0,
running: false,
};
const recordSeek = director.seekScene;
let settleSeek;
director.seekScene = (id, progress) => {
recordSeek(id, progress);
director.publish({ ...oldClock, stopped: true });
return new Promise((resolve) => {
if (progress === 0.5) settleSeek = resolve;
else resolve(false);
});
};
player.pause();
player.seek(30000);
assert.equal(
player.state.wallMs,
30000,
'Share can read the new time immediately',
);
await turns();
director.publish(oldClock);
director.publish({ ...oldClock, seeking: true });
assert.equal(
player.state.wallMs,
30000,
'old teardown cannot move the timeline',
);
director.finishRun();
await playing;
director.publish({ running: false, seeking: true, sceneElapsedSec: 30 });
settleSeek(true);
await turns();
assert.deepEqual(director.calls, ['start', 'seek 0', 'seek 0.5']);
assert.equal(player.state.playing, false);
assert.equal(player.state.wallMs, 30000);