From 4c34855c32bf974c39103da366afe2a910ea2c27 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Mon, 25 Aug 2025 16:50:11 -0400 Subject: [PATCH] cleanup composables --- .../src/console/composables/useInputScope.ts | 35 ++++++++++++++----- .../src/console/composables/useSpatialNav.ts | 1 + frontend/src/console/views/Game.vue | 4 +-- frontend/src/console/views/GamesList.vue | 4 +-- frontend/src/console/views/Home.vue | 4 +-- frontend/src/console/views/Play.vue | 4 +-- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/frontend/src/console/composables/useInputScope.ts b/frontend/src/console/composables/useInputScope.ts index 70f19a609..ff4131d5b 100644 --- a/frontend/src/console/composables/useInputScope.ts +++ b/frontend/src/console/composables/useInputScope.ts @@ -3,16 +3,33 @@ import { InputBus, InputBusSymbol } from "@/console/input/bus"; import type { InputListener } from "@/console/input/actions"; export function useInputScope() { - const bus = inject(InputBusSymbol)!; - let pop: (() => void) | null = null; + const bus = inject(InputBusSymbol); + + if (!bus) { + throw new Error( + "useInputScope must be used within a component that provides InputBus", + ); + } + + let unsubscribe: (() => void) | null = null; + onMounted(() => { - pop = bus.pushScope(); - }); - onUnmounted(() => { - pop?.(); - pop = null; + unsubscribe = bus.pushScope(); }); - const on = (listener: InputListener) => bus.subscribe(listener); - return { bus, on }; + onUnmounted(() => { + if (unsubscribe) { + unsubscribe(); + unsubscribe = null; + } + }); + + const subscribe = (listener: InputListener) => { + return bus.subscribe(listener); + }; + + return { + bus, + subscribe, + }; } diff --git a/frontend/src/console/composables/useSpatialNav.ts b/frontend/src/console/composables/useSpatialNav.ts index e9f2ba8a1..8687a7ba8 100644 --- a/frontend/src/console/composables/useSpatialNav.ts +++ b/frontend/src/console/composables/useSpatialNav.ts @@ -24,5 +24,6 @@ export function useSpatialNav( const ni = selectedIndex.value + cols; if (ni < getCount()) selectedIndex.value = ni; }; + return { moveLeft, moveRight, moveUp, moveDown }; } diff --git a/frontend/src/console/views/Game.vue b/frontend/src/console/views/Game.vue index dcef7adaf..5aaa1f308 100644 --- a/frontend/src/console/views/Game.vue +++ b/frontend/src/console/views/Game.vue @@ -518,7 +518,7 @@ function goBackToPlatform() { } } -const { on } = useInputScope(); +const { subscribe } = useInputScope(); function handleAction(action: InputAction): boolean { // Lightbox handling if (showLightbox.value) { @@ -865,7 +865,7 @@ onMounted(async () => { loading.value = false; } selectedZone.value = "play"; - off = on(handleAction); + off = subscribe(handleAction); }); // Focus overlays when opened so Esc works even if window handlers exist watch(showDescription, (v) => { diff --git a/frontend/src/console/views/GamesList.vue b/frontend/src/console/views/GamesList.vue index 8cc93e519..e11680e55 100644 --- a/frontend/src/console/views/GamesList.vue +++ b/frontend/src/console/views/GamesList.vue @@ -103,7 +103,7 @@ useRovingDom(selectedIndex, (i) => cardElementAt(i), { inline: "nearest", }); -const { on } = useInputScope(); +const { subscribe } = useInputScope(); const { moveLeft, moveRight, @@ -276,7 +276,7 @@ onMounted(async () => { } catch { /* ignore */ } - off = on(handleAction); + off = subscribe(handleAction); }); onUnmounted(() => { diff --git a/frontend/src/console/views/Home.vue b/frontend/src/console/views/Home.vue index 75454cd3c..1d716ed20 100644 --- a/frontend/src/console/views/Home.vue +++ b/frontend/src/console/views/Home.vue @@ -28,7 +28,7 @@ const collectionsStore = storeCollections(); const storeConsole = consoleStore(); const { navigationMode } = storeToRefs(storeConsole); const { toggleFavorite: toggleFavoriteComposable } = useFavoriteToggle(); -const { on } = useInputScope(); +const { subscribe } = useInputScope(); const platforms = ref([]); const recent = ref([]); @@ -367,7 +367,7 @@ onMounted(async () => { collectionElementAt(collectionsIndex.value), ); - off = on(handleAction); + off = subscribe(handleAction); }); let off: (() => void) | null = null; diff --git a/frontend/src/console/views/Play.vue b/frontend/src/console/views/Play.vue index 38956d6de..e43ea4ccc 100644 --- a/frontend/src/console/views/Play.vue +++ b/frontend/src/console/views/Play.vue @@ -40,7 +40,7 @@ const exitOptions = [ }, { id: "cancel", label: "Cancel", desc: "Return to the game" }, ]; -const { on: onInputScope } = useInputScope(); +const { subscribe } = useInputScope(); let exitScopeOff: (() => void) | null = null; let romCache: DetailedRomSchema | null = null; let rafId: number | null = 0; @@ -71,7 +71,7 @@ function showPrompt() { nextTick(() => { exitScopeOff?.(); - exitScopeOff = onInputScope(handleExitAction); + exitScopeOff = subscribe(handleExitAction); }); }