cleanup composables

This commit is contained in:
Georges-Antoine Assi
2025-08-25 16:50:11 -04:00
parent a4650900e8
commit 4c34855c32
6 changed files with 35 additions and 17 deletions

View File

@@ -3,16 +3,33 @@ import { InputBus, InputBusSymbol } from "@/console/input/bus";
import type { InputListener } from "@/console/input/actions";
export function useInputScope() {
const bus = inject<InputBus>(InputBusSymbol)!;
let pop: (() => void) | null = null;
const bus = inject<InputBus>(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,
};
}

View File

@@ -24,5 +24,6 @@ export function useSpatialNav(
const ni = selectedIndex.value + cols;
if (ni < getCount()) selectedIndex.value = ni;
};
return { moveLeft, moveRight, moveUp, moveDown };
}

View File

@@ -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) => {

View File

@@ -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(() => {

View File

@@ -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<PlatformSchema[]>([]);
const recent = ref<SimpleRomSchema[]>([]);
@@ -367,7 +367,7 @@ onMounted(async () => {
collectionElementAt(collectionsIndex.value),
);
off = on(handleAction);
off = subscribe(handleAction);
});
let off: (() => void) | null = null;

View File

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