Skip to content

Commit

Permalink
hook to list registered spawnable entities
Browse files Browse the repository at this point in the history
  • Loading branch information
luludotdev committed Nov 9, 2023
1 parent d961a15 commit 086eba0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/react/hooks/useRegistered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { EventHandler } from '@dreamlab.gg/core/events'
import { useCallback, useEffect } from 'https://esm.sh/react@18.2.0'
import { useForceUpdate } from './useForceUpdate.ts'
import { useGame } from './useGame.ts'

export const useRegistered = () => {
const game = useGame()
const forceUpdate = useForceUpdate()

type Handler = EventHandler<'onRegister'>
const onRegister = useCallback<Handler>(() => forceUpdate(), [forceUpdate])

useEffect(() => {
game.events.common.addListener('onRegister', onRegister)

return () => {
game.events.common.removeListener('onRegister', onRegister)
}
}, [game.events.common, onRegister])

return game.registered
}

0 comments on commit 086eba0

Please sign in to comment.