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) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-30 22:45:54 -04:00
parent acfa9d97b3
commit 647bc61491
3 changed files with 28 additions and 32 deletions

View File

@@ -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";

View File

@@ -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<boolean>,
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<boolean>): 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<boolean>): 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 <html> (next to the theme classes) so

View File

@@ -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();