From 647bc614919b60b149fdc86f2e3b9ff3876ccd86 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Wed, 30 Sep 2026 22:45:54 -0400 Subject: [PATCH] refactor(streaming): simplify pass on the exit chord move Share one flag-mirror helper between useStageActive and usePlayingWhile, add a deferred() test helper for Desktop's hand-answered calls, and note that players opt into the pad exit by listening for gamepad:exitchord. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/v2/composables/useGamepad/index.ts | 4 +-- .../v2/composables/useStageActive/index.ts | 23 +++++++------ frontend/src/v2/views/Player/Desktop.test.ts | 33 +++++++++---------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/frontend/src/v2/composables/useGamepad/index.ts b/frontend/src/v2/composables/useGamepad/index.ts index cce865917..d36f2325a 100644 --- a/frontend/src/v2/composables/useGamepad/index.ts +++ b/frontend/src/v2/composables/useGamepad/index.ts @@ -27,8 +27,8 @@ // * Game running (storePlaying.playing): the emulator reads the pad // itself, so all translation is off. Otherwise B (shared by Circle / // Nintendo-A in the standard mapping) would quit the game. Holding -// Select+Start instead fires `gamepad:exitchord` for the player to -// open its exit dialog. +// Select+Start instead fires `gamepad:exitchord`; a player opts into +// a pad exit by listening for it. // * Controller-test screen (ACTIONS_DISABLED_PATHS): built-in actions // are muted so every button can be pressed and inspected in place. import { onBeforeUnmount } from "vue"; diff --git a/frontend/src/v2/composables/useStageActive/index.ts b/frontend/src/v2/composables/useStageActive/index.ts index fa2e7d363..40e210ffb 100644 --- a/frontend/src/v2/composables/useStageActive/index.ts +++ b/frontend/src/v2/composables/useStageActive/index.ts @@ -1,30 +1,29 @@ import { onScopeDispose, toValue, watch, type MaybeRefOrGetter } from "vue"; import storePlaying from "@/stores/playing"; +// Mirrors a source into one of the playing store's flags, cleared on dispose. +function mirrorFlag( + source: MaybeRefOrGetter, + set: (on: boolean) => void, +): void { + watch(() => toValue(source), set, { immediate: true }); + onScopeDispose(() => set(false)); +} + /** Mirrors a player's running state into the global stage flag that * unmounts the app chrome and zeroes the nav-height tokens. */ export function useStageActive(running: MaybeRefOrGetter): void { const playingStore = storePlaying(); // Deliberately not the `playing` flag: that one also spans pre-stage // phases (Stream sets it while loading behind its config screen). - watch( - () => toValue(running), - (active) => playingStore.setStageActive(active), - { immediate: true }, - ); - onScopeDispose(() => playingStore.setStageActive(false)); + mirrorFlag(running, (on) => playingStore.setStageActive(on)); } /** Mirrors a session, launch or pending claim included, into the global * playing flag, which hands the controller from useGamepad to the session. */ export function usePlayingWhile(active: MaybeRefOrGetter): void { const playingStore = storePlaying(); - watch( - () => toValue(active), - (on) => playingStore.setPlaying(on), - { immediate: true }, - ); - onScopeDispose(() => playingStore.setPlaying(false)); + mirrorFlag(active, (on) => playingStore.setPlaying(on)); } /** Mirrors the flag onto (next to the theme classes) so diff --git a/frontend/src/v2/views/Player/Desktop.test.ts b/frontend/src/v2/views/Player/Desktop.test.ts index bdc5d6f11..893986feb 100644 --- a/frontend/src/v2/views/Player/Desktop.test.ts +++ b/frontend/src/v2/views/Player/Desktop.test.ts @@ -98,6 +98,15 @@ const CLAIM = { }, }; +// A pending call the test answers by hand. +function deferred() { + let answer = (_: unknown) => {}; + const promise = new Promise((resolve) => { + answer = resolve; + }); + return { promise, answer }; +} + // Unstamped, a release reaches whichever session took the container. function expectReleasedOwnClaim(): void { expect(mocks.releaseSession).toHaveBeenCalledWith( @@ -246,12 +255,8 @@ describe("Desktop heartbeats", () => { it("keeps the exit a release made while a beat was in flight", async () => { const wrapper = await openDesktop(); mocks.releaseSession.mockResolvedValue({}); - let answer = (_: unknown) => {}; - mocks.heartbeatSession.mockReturnValue( - new Promise((resolve) => { - answer = resolve; - }), - ); + const { promise, answer } = deferred(); + mocks.heartbeatSession.mockReturnValue(promise); const beat = mocks.heartbeatTick?.(); expect(await mocks.routeLeave?.()).toBe(true); @@ -422,12 +427,8 @@ describe("Desktop controller ownership", () => { describe("Desktop claims that outlive the view", () => { it("hands back a claim that answers after the view is gone", async () => { - let answer = (_: unknown) => {}; - mocks.claimDesktop.mockReturnValue( - new Promise((resolve) => { - answer = resolve; - }), - ); + const { promise, answer } = deferred(); + mocks.claimDesktop.mockReturnValue(promise); mocks.releaseSession.mockResolvedValue({}); const wrapper = await mountDesktop(); @@ -440,12 +441,8 @@ describe("Desktop claims that outlive the view", () => { }); it("hands back a claim that answers while the leave is still resolving", async () => { - let answer = (_: unknown) => {}; - mocks.claimDesktop.mockReturnValue( - new Promise((resolve) => { - answer = resolve; - }), - ); + const { promise, answer } = deferred(); + mocks.claimDesktop.mockReturnValue(promise); mocks.releaseSession.mockResolvedValue({}); const wrapper = await mountDesktop();