diff --git a/src/react/hooks/events.ts b/src/react/hooks/events.ts index 19b4519..49aae3d 100644 --- a/src/react/hooks/events.ts +++ b/src/react/hooks/events.ts @@ -2,11 +2,29 @@ import type { ClientEvents, CommonEvents, Event, + EventEmitter, EventHandler, } from '@dreamlab.gg/core/events' import { useEffect } from 'https://esm.sh/v136/react@18.2.0' import { useGame } from './useGame.ts' +export const useEventListener = < + EventTypes extends EventEmitter.ValidEventTypes, + T extends EventEmitter.EventNames, +>( + emitter: EventEmitter, + event: T, + handler: EventEmitter.EventListener, +): void => { + useEffect(() => { + emitter.addListener(event, handler) + + return () => { + emitter.removeListener(event, handler) + } + }, [emitter, event, handler]) +} + export const useClientEventListener = >( event: T, handler: EventHandler,