Stories: a teardown that times out cannot undo the restored view

directorSettled now tells a timeout from settlement, and on a timeout
revokes the scene's layer cleanup still in flight before Close puts the
view back.
This commit is contained in:
Sameh Khamis committed 2026-11-16 13:31:51 -08:00
1 parent 0a39b05cdb
commit 4fa80fb18c
5 files changed
+105 -8

No files matched your search

+10 -3
View File
@@ -7,21 +7,28 @@ export const SETTLE_LIMIT_MS = 15000;
* Resolves once the director is idle: no run, and every LOAD, seek and
* interaction settled (SceneDirector.whenIdle). A teardown that never
* finishes is given SETTLE_LIMIT_MS, so a stuck scene cannot leave Stories
* unable to close; that case is logged.
* unable to close; that case is logged, and the scene's layer cleanup still
* in flight loses its say over layers (SceneDirector.revokeSceneRelease), so
* it cannot turn off a layer the caller puts back next.
* @param {import('../scenes/director.js').SceneDirector} director
* @returns {Promise<boolean>} True once idle, false when the limit ran out.
*/
export function directorSettled(director) {
let timer = 0;
const limit = new Promise((resolve) => {
timer = setTimeout(() => {
console.warn('Stories: the scene did not finish tearing down; going on');
resolve();
director.revokeSceneRelease?.();
resolve(false);
}, SETTLE_LIMIT_MS);
});
// A failure to wait counts as settled: the view still comes back.
const idle = Promise.resolve()
.then(() => director.whenIdle())
.catch(() => {});
.then(
() => true,
() => true,
);
return Promise.race([idle, limit]).finally(() => clearTimeout(timer));
}
+36 -1
View File
@@ -2,7 +2,11 @@
// 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';
import {
createGevPlayer,
directorSettled,
SETTLE_LIMIT_MS,
} from './gevPlayer.js';
const story = {
sceneId: 'scene-1',
@@ -281,3 +285,34 @@ test('Play from a paused link goes on from the link’s time', async () => {
director.finishRun();
await playing;
});
test('a teardown that outlasts the limit is told apart, and loses its say over layers', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const warn = console.warn;
console.warn = () => {};
let revoked = 0;
const director = {
whenIdle: () => new Promise(() => {}),
revokeSceneRelease: () => revoked++,
};
try {
const settled = directorSettled(director);
await turns();
assert.equal(revoked, 0, 'nothing is revoked while waiting');
t.mock.timers.tick(SETTLE_LIMIT_MS);
assert.equal(await settled, false, 'a timeout is not settlement');
assert.equal(revoked, 1, 'the stale cleanup is revoked before going on');
} finally {
console.warn = warn;
}
});
test('a teardown that finishes settles without revoking anything', async () => {
let revoked = 0;
const director = {
whenIdle: async () => {},
revokeSceneRelease: () => revoked++,
};
assert.equal(await directorSettled(director), true);
assert.equal(revoked, 0);
});