-
Notifications
You must be signed in to change notification settings - Fork 0
/
usePartyGuest.ts
35 lines (33 loc) · 1.06 KB
/
usePartyGuest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import useAck from './useAck'
import useSync from './useSync'
import useMoves from './useMoves'
import useConnection from './useConnection'
import { useIdStore, useStateStore, useLogSizeStore } from './stores'
import { UsePartyGuestArgs, UsePartyGuestReturn } from '..'
/**
* A React hook that, provided the game rules and a shared room identifier,
* returns the guest's state representation and an object with moves
* to call on the guest's user action.
*/
function usePartyGuest({
roomId,
game
}: UsePartyGuestArgs): UsePartyGuestReturn {
const { id } = useIdStore(roomId)
const { cache, setState } = useStateStore(roomId)
const { logSize, setLogSize } = useLogSizeStore(roomId)
const { connected, conn, data, clearData } = useConnection({ id, roomId })
useAck({ connected, conn, logSize })
useSync({ data, clearData, game, roomId, setState, setLogSize })
const { moves } = useMoves({
connected,
conn,
id,
setState,
roomId,
game,
logSize
})
return { connectionId: id, connected, state: cache, moves }
}
export default usePartyGuest