diff --git a/frontend/src/v2/composables/useExitChord/index.test.ts b/frontend/src/v2/composables/useExitChord/index.test.ts index a1df659a5..b544bfe82 100644 --- a/frontend/src/v2/composables/useExitChord/index.test.ts +++ b/frontend/src/v2/composables/useExitChord/index.test.ts @@ -55,6 +55,21 @@ describe("useExitChord", () => { scope.stop(); }); + it("waits for a release before a held chord can fire again", () => { + const { onChord, scope } = listen(() => true); + pads = [pad(CHORD)]; + + vi.advanceTimersByTime(5000); + expect(onChord).toHaveBeenCalledTimes(1); + + pads = [pad([])]; + vi.advanceTimersByTime(100); + pads = [pad(CHORD)]; + vi.advanceTimersByTime(1700); + expect(onChord).toHaveBeenCalledTimes(2); + scope.stop(); + }); + it("starts the hold over when either button is let go", () => { const { onChord, scope } = listen(() => true); pads = [pad(CHORD)]; diff --git a/frontend/src/v2/composables/useExitChord/index.ts b/frontend/src/v2/composables/useExitChord/index.ts index 94add7d64..d589898a8 100644 --- a/frontend/src/v2/composables/useExitChord/index.ts +++ b/frontend/src/v2/composables/useExitChord/index.ts @@ -15,6 +15,9 @@ export function useExitChord( onChord: () => void, ): void { let heldSince = 0; + // Set once a hold fires, so a chord still held after the dialog is cancelled + // waits for a release instead of reopening it. + let fired = false; function poll(): void { const pads = navigator.getGamepads ? navigator.getGamepads() : []; @@ -29,10 +32,13 @@ export function useExitChord( const now = performance.now(); if (!held) { heldSince = 0; + fired = false; + } else if (fired) { + return; } else if (!heldSince) { heldSince = now; } else if (now - heldSince >= HOLD_MS) { - heldSince = 0; + fired = true; onChord(); } } @@ -47,6 +53,7 @@ export function useExitChord( } else { pause(); heldSince = 0; + fired = false; } }, { immediate: true }, diff --git a/frontend/src/v2/views/Player/Desktop.vue b/frontend/src/v2/views/Player/Desktop.vue index 30e5dbc43..396073157 100644 --- a/frontend/src/v2/views/Player/Desktop.vue +++ b/frontend/src/v2/views/Player/Desktop.vue @@ -10,7 +10,7 @@ import { RAlert, RBtn, RSpinner } from "@v2/lib"; import { useEventListener, useIntervalFn } from "@vueuse/core"; import { isAxiosError } from "axios"; -import { computed, onBeforeUnmount, onMounted, ref } from "vue"; +import { computed, onMounted, ref } from "vue"; import { useI18n } from "vue-i18n"; import { onBeforeRouteLeave, useRoute, useRouter } from "vue-router"; import { ROUTES } from "@/plugins/router"; @@ -22,6 +22,7 @@ import SessionEndedReason from "@/v2/components/Player/SessionEndedReason.vue"; import StreamStage from "@/v2/components/Player/StreamStage.vue"; import { useConfirm } from "@/v2/composables/useConfirm"; import { useExitChord } from "@/v2/composables/useExitChord"; +import { useIsAlive } from "@/v2/composables/useIsAlive"; import { usePageTitle } from "@/v2/composables/usePageTitle"; import { useSocketEvent } from "@/v2/composables/useSocketEvent"; import { usePlayingWhile } from "@/v2/composables/useStageActive"; @@ -68,7 +69,7 @@ useExitChord(sessionActive, () => void handleExit()); // Leaving mid-claim is allowed, so a claim that answers after unmount is handed // straight back instead of standing unbeaten until it goes stale. -let disposed = false; +const alive = useIsAlive(); async function openDesktop(): Promise { if (!containerName.value) { @@ -84,7 +85,7 @@ async function openDesktop(): Promise { platform.value = data.platform; claimedAt.value = data.claimed_at; holdsClaim.value = true; - if (disposed) { + if (!alive.value) { void release(); return; } @@ -237,10 +238,6 @@ useEventListener(window, "pagehide", onPageHide); onMounted(() => { void openDesktop(); }); - -onBeforeUnmount(() => { - disposed = true; -});