-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.ts
25 lines (23 loc) · 919 Bytes
/
secret.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
import { Move, SecretMove, RandomSecretMove } from '.'
/**
* A higher order function to provide `contextId` and `revealASecret`, enabling the move
* to return a different state for different peers.
* ```js
* withSecret(({ contextId, hostId, revealSecret, state }) => {
* const isHostContext = hostId === contextId
* if (isHostContext) {
* return ({ ...state, deck: state.deck.slice(1) })
* } else {
* const topCardOfDeck = revealSecret(hostId, s => s.deck[0])
* return ({ ...state, revealedCard: topCardOfDeck })
* }
* })
* ```
* @param moveFn A move defition that needs to be secretive
* @return A move defintion with context awareness and access to secret state
*/
export function withSecret(moveFn: Move): SecretMove | RandomSecretMove {
moveFn._isSecret = true
return moveFn as SecretMove | RandomSecretMove
}
export const isSecretMove = (moveFn: Move) => moveFn._isSecret