-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hook to list registered spawnable entities
- Loading branch information
1 parent
d961a15
commit 086eba0
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |