deno.land / x / jotai@v1.8.4 / src / xstate / atomWithMachine.ts

atomWithMachine.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { interpret } from 'xstate'import type { AnyInterpreter, AnyStateMachine, AreAllImplementationsAssumedToBeProvided, EventObject, InternalMachineOptions, InterpreterFrom, InterpreterOptions, Prop, StateConfig, StateFrom,} from 'xstate'import type { Atom, Getter, WritableAtom } from 'jotai'import { atom } from 'jotai'
export const RESTART = Symbol()
export interface MachineAtomOptions<TContext, TEvent extends EventObject> { /** * If provided, will be merged with machine's `context`. */ context?: Partial<TContext> /** * The state to rehydrate the machine to. The machine will * start at this state instead of its `initialState`. */ state?: StateConfig<TContext, TEvent>}
type Options<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided< TMachine['__TResolvedTypesMeta'] > extends false ? InterpreterOptions & MachineAtomOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions< TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true > : InterpreterOptions & MachineAtomOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions< TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'] >
type MaybeParam<T> = T extends (v: infer V) => unknown ? V : never
export function atomWithMachine< TMachine extends AnyStateMachine, TInterpreter = InterpreterFrom<TMachine>>( getMachine: TMachine | ((get: Getter) => TMachine), getOptions?: Options<TMachine> | ((get: Getter) => Options<TMachine>)): WritableAtom< StateFrom<TMachine>, MaybeParam<Prop<TInterpreter, 'send'>> | typeof RESTART, void> { const cachedMachineAtom = atom<{ machine: AnyStateMachine service: AnyInterpreter } | null>(null) const machineAtom = atom( (get) => { const cachedMachine = get(cachedMachineAtom) if (cachedMachine) { return cachedMachine } let initializing = true const safeGet = (a: Atom<unknown>) => { if (initializing) { return get(a) } throw new Error('get not allowed after initialization') } const machine = isGetter(getMachine) ? getMachine(safeGet) : getMachine const options = isGetter(getOptions) ? getOptions(safeGet) : getOptions initializing = false const { guards, actions, services, delays, context, ...interpreterOptions } = options || {}
const machineConfig = { ...(guards && { guards }), ...(actions && { actions }), ...(services && { services }), ...(delays && { delays }), }
const machineWithConfig = machine.withConfig( machineConfig as any, () => ({ ...machine.context, ...context, }) )
const service = interpret(machineWithConfig, interpreterOptions) return { machine: machineWithConfig, service } }, (get, set, _arg) => { set(cachedMachineAtom, get(machineAtom)) } )
machineAtom.onMount = (commit) => { commit() }
const cachedMachineStateAtom = atom<StateFrom<TMachine> | null>(null)
const machineStateAtom = atom( (get) => get(cachedMachineStateAtom) ?? (get(machineAtom).machine.initialState as StateFrom<TMachine>), (get, set, registerCleanup: (cleanup: () => void) => void) => { const { service } = get(machineAtom) service.onTransition((nextState: any) => { set(cachedMachineStateAtom, nextState) }) service.start() registerCleanup(() => { const { service } = get(machineAtom) service.stop() }) } )
machineStateAtom.onMount = (initialize) => { let unsub: (() => void) | undefined | false
initialize((cleanup) => { if (unsub === false) { cleanup() } else { unsub = cleanup } })
return () => { if (unsub) { unsub() } unsub = false } }
const machineStateWithServiceAtom = atom( (get) => get(machineStateAtom), ( get, set, event: Parameters<AnyInterpreter['send']>[0] | typeof RESTART ) => { const { service } = get(machineAtom) if (event === RESTART) { service.stop() set(cachedMachineAtom, null) set(machineAtom, null) const { service: newService } = get(machineAtom) newService.onTransition((nextState: any) => { set(cachedMachineStateAtom, nextState) }) newService.start() } else { service.send(event) } } )
return machineStateWithServiceAtom}
const isGetter = <T>(v: T | ((get: Getter) => T)): v is (get: Getter) => T => typeof v === 'function'
jotai

Version Info

Tagged at
2 years ago