From 4b86b32527aec99c7da2ff5c6d0d9e581d9573c1 Mon Sep 17 00:00:00 2001 From: byrongamatos Date: Sun, 12 Jul 2026 15:54:40 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20stop=20monkey-patching=20window.showScre?= =?UTF-8?q?en=20=E2=80=94=20listen=20to=20the=20screen=20events=20instead?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This wrapped window.showScreen to tear down the stems graph when leaving the player. It was the THIRD wrapper on that one global, and it shipped a bug. Core publishes window.showScreen; the v3 shell wrapped it (carrying the legacy home -> v3-songs mapping); this wrapped it again. Each captured whatever happened to be there AT THE TIME — and plugins load ASYNCHRONOUSLY, so the chain linked up in whatever order the race settled. When this wrapper won, the shell's mapping was silently dropped and THE LIBRARY OPENED ON THE DEAD LEGACY SCREEN. Testers reported it as "randomly, the library shows the old interface" (got-feedback/feedback#923, #924). Core already emits screen events for exactly this. Listening costs nothing, cannot clobber another plugin, and cannot be clobbered by one. ━━━ THREE THINGS CODEX CAUGHT, EACH A SILENT NO-OP ━━━ 1. TIMING. My first cut listened to screen:changed — which fires at the END of showScreen, after core awaits library and provider loads. The old wrapper ran BEFORE navigation began. Late teardown means the stems graph keeps playing on a non-player screen, and a slow or failing fetch skips it entirely. Core now emits screen:changing before it does anything, and that is the event this wants. 2. BOTH EVENTS, because of cross-repo ordering. screen:changing is NEW — today's released host does not emit it. Listening only to the new one would mean teardown() NEVER runs on the current host. Listening only to the old one reintroduces the late-teardown problem once the host updates. So: both. It is safe by construction — the wrapper this replaces called teardown() on EVERY non-player navigation, so it is already idempotent. 3. EITHER BUS. window.slopsmith is the legacy alias of the SAME object (core: `window.slopsmith = window.feedBack`), and the rest of this file reads it. Reading only window.feedBack would attach nothing on a build that exposes just the old name. ━━━ AND A PRE-EXISTING BUG, FOUND ON THE WAY ━━━ All THREE of this file's "bus is late" fallbacks listened for `slopsmith:capabilities:ready`. Core dispatches `feedBack:capabilities:ready` (capabilities.js:1536). The slopsmith: name is the PRE-DMCA event and NOTHING has emitted it since the rename — so every one of those fallbacks has been DEAD, and wireLifecycleListeners / installCapabilityParticipant silently never wired whenever the bus was late. All three now listen for the event that is actually dispatched (keeping the old alias too, harmlessly). 47/47, Codex 0. Refs got-feedback/feedback#924 Co-Authored-By: Claude Opus 4.8 (1M context) --- src/main.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/src/main.js b/src/main.js index d0b7faf..253a31f 100644 --- a/src/main.js +++ b/src/main.js @@ -1012,6 +1012,12 @@ import { // silently whenever the bus wasn't up yet.) const wireLifecycleListeners = () => { if (!(window.slopsmith && typeof window.slopsmith.on === 'function')) { + // `feedBack:capabilities:ready` is what capabilities.js actually dispatches + // (capabilities.js:1536). The slopsmith: name is the PRE-DMCA event and nothing + // emits it any more — so this fallback has been DEAD since the rename, and the + // lifecycle listeners silently never wired when the bus was late. Keep the old + // alias too, harmlessly, for an older capabilities build. + window.addEventListener('feedBack:capabilities:ready', wireLifecycleListeners, { once: true }); window.addEventListener('slopsmith:capabilities:ready', wireLifecycleListeners, { once: true }); return; } @@ -1028,14 +1034,70 @@ import { }; wireLifecycleListeners(); - // Clean up on leaving the player - const _show = window.showScreen; - hookState.baseShowScreen = _show; - window.showScreen = function (id) { - const impl = hookState.impl; - if (id !== 'player' && impl && typeof impl.teardown === 'function') impl.teardown(); - return hookState.baseShowScreen.call(this, id); + // Clean up on leaving the player. + // + // This USED to monkey-patch window.showScreen. It doesn't any more, and that mattered: + // THREE parties were wrapping that one global — core publishes it, the v3 shell wrapped it + // (carrying the legacy home -> v3-songs mapping), and this wrapped it again — each + // capturing whatever happened to be there at the time. Plugins load ASYNCHRONOUSLY, so the + // chain linked up in whatever order the race settled, and when this wrapper won, the + // shell's mapping was silently dropped and the LIBRARY OPENED ON THE DEAD LEGACY SCREEN. + // Testers saw it as "randomly, the library shows the old interface" + // (got-feedback/feedback#923, #924). + // + // Core already emits screen:changed for exactly this. Listening costs nothing, cannot + // clobber another plugin, and cannot be clobbered by one. + // + // RETRY IF THE BUS IS LATE. Codex [P2] on the first cut, and it was right: the old + // wrapper did not need window.feedBack to exist, but a listener does. Bailing out when + // the bus is not ready yet would silently mean teardown() NEVER runs — a leak that only + // shows up as stems still playing after you leave the player. Same shape, and the same + // fix, as wireLifecycleListeners() above. + // screen:CHANGING, not screen:changed. Codex [P2], and it matters: the old wrapper ran + // BEFORE showScreen did anything, whereas screen:changed fires at the very END — after + // core awaits library and provider loads. Listening to the late event would delay teardown + // behind a slow fetch, or skip it entirely if that fetch threw, and the stems graph would + // keep playing on a non-player screen. screen:changing fires before any of that, which is + // exactly the timing the wrapper had. + const wireScreenListener = () => { + // Either bus. `window.slopsmith` is the LEGACY ALIAS of the same object + // (core's app.js: `window.slopsmith = window.feedBack`), and the rest of this file + // reads it. Codex [P2]: on a build that only exposes the old name, reading just + // `window.feedBack` would attach nothing — and with the showScreen wrapper gone, + // teardown() would silently never run and stems would keep playing after you leave + // the player. They are the same object; take whichever is there. + const bus = window.feedBack || window.slopsmith; + if (!(bus && typeof bus.on === 'function')) { + window.addEventListener('feedBack:capabilities:ready', wireScreenListener, { once: true }); + window.addEventListener('slopsmith:capabilities:ready', wireScreenListener, { once: true }); + return; + } + // BOTH events, deliberately. Codex [P2], and it is a cross-repo ordering problem: + // + // screen:changing fires BEFORE core navigates — the timing the old wrapper had, and + // the one we want. But it is NEW: today's released host does not + // emit it yet (got-feedback/feedback#924). + // screen:changed fires at the END of showScreen. Later than ideal, but it exists + // on every host in the field TODAY. + // + // Listening to only the new one would mean teardown() NEVER runs on the current host — + // the stems graph would just keep playing after you leave the player. Listening to only + // the old one reintroduces the late-teardown problem once the host is updated. + // + // Both is safe: the wrapper this replaces called teardown() on EVERY non-player + // navigation, so it is already idempotent by construction. On a new host teardown runs + // at the early (correct) moment and the late event is a cheap no-op; on an old host the + // late event is the only one, exactly as before. + const onLeavingPlayer = (ev) => { + const id = ev && ev.detail && ev.detail.id; + if (!id || id === 'player') return; + const impl = hookState.impl; + if (impl && typeof impl.teardown === 'function') impl.teardown(); + }; + bus.on('screen:changing', onLeavingPlayer); // preferred — pre-navigation + bus.on('screen:changed', onLeavingPlayer); // fallback — hosts without the new event }; + wireScreenListener(); } function capabilityApi() { @@ -1275,6 +1337,8 @@ import { function installCapabilityParticipant() { const api = capabilityApi(); if (!api || typeof api.registerParticipant !== 'function') { + // Same dead-event bug as above — see the note at wireLifecycleListeners. + window.addEventListener('feedBack:capabilities:ready', installCapabilityParticipant, { once: true }); window.addEventListener('slopsmith:capabilities:ready', installCapabilityParticipant, { once: true }); return; }