mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
cleanup composables
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -24,5 +24,6 @@ export function useSpatialNav(
|
||||
const ni = selectedIndex.value + cols;
|
||||
if (ni < getCount()) selectedIndex.value = ni;
|
||||
};
|
||||
|
||||
return { moveLeft, moveRight, moveUp, moveDown };
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user