-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
useInterpret.ts
150 lines (134 loc) · 4.08 KB
/
useInterpret.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
import { useEffect, useState } from 'react';
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
import {
AnyInterpreter,
AnyStateMachine,
AreAllImplementationsAssumedToBeProvided,
InternalMachineOptions,
interpret,
InterpreterFrom,
InterpreterOptions,
InterpreterStatus,
MachineOptions,
Observer,
State,
StateFrom,
toObserver
} from 'xstate';
import { MaybeLazy } from './types';
import useConstant from './useConstant';
import { UseMachineOptions } from './useMachine';
export function useIdleInterpreter(
getMachine: MaybeLazy<AnyStateMachine>,
options: Partial<InterpreterOptions> &
Partial<UseMachineOptions<unknown, never>> &
Partial<MachineOptions<unknown, never>>
): AnyInterpreter {
const machine = useConstant(() => {
return typeof getMachine === 'function' ? getMachine() : getMachine;
});
if (
process.env.NODE_ENV !== 'production' &&
typeof getMachine !== 'function'
) {
const [initialMachine] = useState(machine);
if (getMachine !== initialMachine) {
console.warn(
'Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
'Please make sure that you pass the same Machine as argument each time.'
);
}
}
const {
context,
guards,
actions,
activities,
services,
delays,
state: rehydratedState,
...interpreterOptions
} = options;
const service = useConstant(() => {
const machineConfig = {
context,
guards,
actions,
activities,
services,
delays
};
const machineWithConfig = machine.withConfig(machineConfig as any, () => ({
...machine.context,
...context
}));
return interpret(machineWithConfig, interpreterOptions);
});
// Make sure options are kept updated when they change.
// This mutation assignment is safe because the service instance is only used
// in one place -- this hook's caller.
useIsomorphicLayoutEffect(() => {
Object.assign(service.machine.options.actions!, actions);
Object.assign(service.machine.options.guards!, guards);
Object.assign(service.machine.options.activities!, activities);
Object.assign(service.machine.options.services!, services);
Object.assign(service.machine.options.delays!, delays);
}, [actions, guards, activities, services, delays]);
return service as any;
}
type RestParams<
TMachine extends AnyStateMachine
> = AreAllImplementationsAssumedToBeProvided<
TMachine['__TResolvedTypesMeta']
> extends false
? [
options: InterpreterOptions &
UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> &
InternalMachineOptions<
TMachine['__TContext'],
TMachine['__TEvent'],
TMachine['__TResolvedTypesMeta'],
true
>,
observerOrListener?:
| Observer<StateFrom<TMachine>>
| ((value: StateFrom<TMachine>) => void)
]
: [
options?: InterpreterOptions &
UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> &
InternalMachineOptions<
TMachine['__TContext'],
TMachine['__TEvent'],
TMachine['__TResolvedTypesMeta']
>,
observerOrListener?:
| Observer<StateFrom<TMachine>>
| ((value: StateFrom<TMachine>) => void)
];
export function useInterpret<TMachine extends AnyStateMachine>(
getMachine: MaybeLazy<TMachine>,
...[options = {}, observerOrListener]: RestParams<TMachine>
): InterpreterFrom<TMachine> {
const service = useIdleInterpreter(getMachine, options as any);
useEffect(() => {
if (!observerOrListener) {
return;
}
let sub = service.subscribe(toObserver(observerOrListener));
return () => {
sub.unsubscribe();
};
}, [observerOrListener]);
useEffect(() => {
const rehydratedState = options.state;
service.start(
rehydratedState ? (State.create(rehydratedState) as any) : undefined
);
return () => {
service.stop();
service.status = InterpreterStatus.NotStarted;
};
}, []);
return service as any;
}