From e3044bca1b5967afd9898f2de6fa1e92278d4412 Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Mon, 30 Nov 2026 15:21:04 -0500 Subject: [PATCH 1/2] Do a keystroke's explorer work once, not three times Each change to the explorer wrote the address with navigate(), and on its way the router rewrites the entry it is leaving. The explorer heard that as a move back to the old cut, re-ran the whole change to undo it (the search box briefly showing the old words), then ran it again when the new address landed: three full changes per keystroke. The address is now followed again only once the router has settled. Typing also no longer reads every card's place from storage (only the playing episode's moves), sets hidden only on cards whose state changes, and keys that land before a frame make one change. Timers and frames the explorer still had coming are cleared when the page swaps out. Co-Authored-By: Claude Opus 5.5 --- .../src/components/explorer/explorer.ts | 105 ++++++++++++++---- 1 file changed, 84 insertions(+), 21 deletions(-) diff --git a/packages/starpod/src/components/explorer/explorer.ts b/packages/starpod/src/components/explorer/explorer.ts index fe46d787..c09acd8b 100644 --- a/packages/starpod/src/components/explorer/explorer.ts +++ b/packages/starpod/src/components/explorer/explorer.ts @@ -762,6 +762,23 @@ export function startExplorer() { /* ---------- Motion ---------- */ const flying = new Map(); + // Frames and timers still to come when the page swaps out. + const frames = new Set(); + const timers = new Set(); + function nextFrame(fn: () => void) { + const id = requestAnimationFrame(() => { + frames.delete(id); + fn(); + }); + frames.add(id); + } + function later(fn: () => void, ms: number) { + const id = window.setTimeout(() => { + timers.delete(id); + fn(); + }, ms); + timers.add(id); + } const refitting = new WeakMap(); /** * Copies of text blocks as they were (in `view`, at their old boxes), @@ -1145,12 +1162,15 @@ export function startExplorer() { { keep, place, - anchor + anchor, + now }: { keep?: Card | null; place?: () => void; /** The card the window must hold when `place` places the reader. */ anchor?: Card | null; + /** Called from a frame, before it paints. */ + now?: boolean; } = {} ) { // On screen now: in the viewport (read in one pass over the cards the @@ -1273,9 +1293,7 @@ export function startExplorer() { settleAway(320 * scale); } // Let the browser's anchoring forget the old layout before it resumes. - requestAnimationFrame(() => - requestAnimationFrame(() => delete list.dataset.moving) - ); + nextFrame(() => nextFrame(() => delete list.dataset.moving)); if (reducedMotion()) { return; @@ -1283,8 +1301,8 @@ export function startExplorer() { // Cards that were shown but far away and now slide into view rise in. // Found at the start of the next frame (still before it paints), where // the layout this read needs is computed once, with the frame's own, - // rather than forced a second time inside the handler. Where cards on - // screen are leaving, they rise once those have cleared their text + // rather than forced a second time inside the handler (a change made in + // a frame finds them at once). Where cards on screen are leaving, they rise once those have cleared their text // (explorer.css), so the two never read over each other. const s = scale; const clearing = leaving.some( @@ -1293,13 +1311,13 @@ export function startExplorer() { ) ? LEAVE_MS : 0; - requestAnimationFrame(() => + const rise = () => onScreen() .filter((card) => !before.has(card) && shownBefore.has(card)) .forEach((card, i) => { // (Arriving, its picture fades in once loaded: explorer.css.) card.li.dataset.join = ''; - window.setTimeout(() => delete card.li.dataset.join, 700 * s); + later(() => delete card.li.dataset.join, 700 * s); card.li.animate( [ { opacity: 0, translate: '0 32px' }, @@ -1312,8 +1330,12 @@ export function startExplorer() { fill: 'backwards' } ); - }) - ); + }); + if (now) { + rise(); + } else { + nextFrame(rise); + } } /* ---------- State ---------- */ @@ -1333,8 +1355,11 @@ export function startExplorer() { const joined: Array = []; for (const card of cards) { const was = card.li.hidden; - card.li.hidden = !visible.has(card); - if (!card.li.hidden) { + const hide = !visible.has(card); + if (hide !== was) { + card.li.hidden = hide; + } + if (!hide) { card.li.removeAttribute('data-exit'); if (was) { card.li.dataset.join = ''; @@ -1343,7 +1368,7 @@ export function startExplorer() { } } if (joined.length) { - window.setTimeout( + later( () => joined.forEach((li) => delete li.dataset.join), 700 * slomo() ); @@ -1449,6 +1474,7 @@ export function startExplorer() { } let lastParam = ''; + let writing = 0; function remember(next: ExplorerState, param: string) { const hash = toHash(next) || ALL; if (location.hash === hash || (!location.hash && hash === ALL)) { @@ -1457,9 +1483,17 @@ export function startExplorer() { // To the router, a change of hash alone is a move within the page. // Consecutive changes to one control (typing, arrowing through a menu) // are one step in history; switching controls starts a new one. - void navigate(location.pathname + location.search + hash, { + // On its way to the new address the router rewrites the entry it is + // leaving, which reads as a move back to the old cut: the address is + // followed again only once the router has settled. + writing++; + const settled = () => { + writing--; + fromAddress(); + }; + navigate(location.pathname + location.search + hash, { history: param === lastParam ? 'replace' : 'push' - }); + }).then(settled, settled); lastParam = param; } @@ -1493,16 +1527,37 @@ export function startExplorer() { String(started); } - function commit(next: ExplorerState, param: string | null) { + // Typing is one change a frame, however many keys land before it. + let typing = 0; + function commit(next: ExplorerState, param: string | null, now = false) { + if (typing) { + // A control changed before the typing's frame keeps what was typed; + // a reset or an address replaces it. + cancelAnimationFrame(typing); + typing = 0; + if (param && param !== 'reset') { + next = { ...next, q: search.value.trim() }; + } + } if (toHash(next) === toHash(state)) { return; } - readProgress(next); + if (param === 'q') { + // Only the loaded episode's place moves while it plays; every other + // card's is current (a pause updates it). + if (live) { + showProgress(live, readPosition(live.li.dataset.id!)); + } + countProgress(next); + } else { + readProgress(next); + } if (open) { swap(open); } change(() => render(next), { - keep: next.sort === state.sort ? undefined : null + keep: next.sort === state.sort ? undefined : null, + now }); state = next; syncControls(next); @@ -2056,8 +2111,11 @@ export function startExplorer() { form.addEventListener( 'input', (event) => { - if (event.target === search) { - commit({ ...state, q: search.value.trim() }, 'q'); + if (event.target === search && !typing) { + typing = requestAnimationFrame(() => { + typing = 0; + commit({ ...state, q: search.value.trim() }, 'q', true); + }); } }, { signal } @@ -2365,7 +2423,7 @@ export function startExplorer() { // with the Navigation API, and with hashchange for a step through history. const path = location.pathname; function fromAddress() { - if (location.pathname !== path || !isStateHash(location.hash)) { + if (writing || location.pathname !== path || !isStateHash(location.hash)) { return; } const next = known(parseState(location.hash)); @@ -2420,6 +2478,11 @@ export function startExplorer() { clocked.forEach((el) => delete el.dataset.propsFor); unregister(); swapping.forEach((timer) => clearTimeout(timer)); + flying.forEach((timer) => clearTimeout(timer)); + timers.forEach((timer) => clearTimeout(timer)); + frames.forEach((frame) => cancelAnimationFrame(frame)); + cancelAnimationFrame(typing); + clearTimeout(announce); clearTimeout(folded); clearTimeout(settling); cancelAnimationFrame(following); From ac04a4b9aef1c8cec9a4210b2c8f3fe692ffc02c Mon Sep 17 00:00:00 2001 From: Robert Wagner Date: Tue, 1 Dec 2026 06:53:49 -0500 Subject: [PATCH 2/2] Test that typing keeps its words in the explorer's search box One test logs each time the page sets the search box while `javascript` is typed, and expects none of those writes to change what the box holds. Without the `writing` guard in `fromAddress()`, the router's rewrite of the entry it leaves rolls the box back to the previous words on each key (`ja` to `j`) before restoring them. The other sets the search and picks Video in one task, before typing's frame runs, and expects the address and the box to keep the words. Co-Authored-By: Claude Opus 5.5 --- tests/e2e/explorer.spec.ts | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/e2e/explorer.spec.ts b/tests/e2e/explorer.spec.ts index c37476d4..80ad632c 100644 --- a/tests/e2e/explorer.spec.ts +++ b/tests/e2e/explorer.spec.ts @@ -64,6 +64,64 @@ test('searches titles and descriptions as you type', async ({ page }) => { } }); +test('leaves the search box holding what was typed', async ({ page }) => { + // Every time the page itself sets the box, with what the box held then. + await page.evaluate(() => { + const { get, set } = Object.getOwnPropertyDescriptor( + HTMLInputElement.prototype, + 'value' + )!; + const writes: Array<{ from: string; to: string }> = []; + (window as unknown as { writes: typeof writes }).writes = writes; + Object.defineProperty(HTMLInputElement.prototype, 'value', { + configurable: true, + get, + set(this: HTMLInputElement, value: string) { + if (this.name === 'q' && this.closest('[data-xp-bar]')) { + writes.push({ from: get!.call(this), to: value }); + } + set!.call(this, value); + } + }); + }); + const writes = () => + page.evaluate( + () => + (window as unknown as { writes: Array<{ from: string; to: string }> }) + .writes + ); + const box = page.getByRole('searchbox', { name: 'Search episodes' }); + await box.pressSequentially('javascript'); + await expect(page).toHaveURL(/#q=javascript$/); + await expect(box).toHaveValue('javascript'); + expect((await writes()).filter(({ from, to }) => from !== to)).toEqual([]); + // The log does see the page set the box: a reset empties it. + await page.getByRole('button', { name: 'Reset' }).click(); + await expect(box).toHaveValue(''); + expect(await writes()).toContainEqual({ from: 'javascript', to: '' }); +}); + +test('keeps the typed words when a filter changes before they apply', async ({ + page +}) => { + // In one task, so the filter changes before the search's frame runs. + await page.evaluate(() => { + const bar = document.querySelector('[data-xp-bar]')!; + const search = bar.querySelector('input[name=q]')!; + search.value = 'css'; + search.dispatchEvent(new Event('input', { bubbles: true })); + bar + .querySelector('input[name=kind][value=video]')! + .click(); + }); + await expect(page).toHaveURL(/#q=css&kind=video$/); + await expect( + page.getByRole('searchbox', { name: 'Search episodes' }) + ).toHaveValue('css'); + const results = await shown(page); + expect(results.every((card) => card.video)).toBe(true); +}); + test('filters by video and audio', async ({ page }) => { await page.locator('label:has(input[name=kind][value=video])').click(); await expect(page).toHaveURL(/#kind=video$/);