mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
36 lines
739 B
TypeScript
36 lines
739 B
TypeScript
import type { InputListener } from "@/console/input/actions";
|
|
import { InputBus, InputBusSymbol } from "@/console/input/bus";
|
|
import { inject, onMounted, onUnmounted } from "vue";
|
|
|
|
export function useInputScope() {
|
|
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(() => {
|
|
unsubscribe = bus.pushScope();
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
if (unsubscribe) {
|
|
unsubscribe();
|
|
unsubscribe = null;
|
|
}
|
|
});
|
|
|
|
const subscribe = (listener: InputListener) => {
|
|
return bus.subscribe(listener);
|
|
};
|
|
|
|
return {
|
|
bus,
|
|
subscribe,
|
|
};
|
|
}
|