-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.ts
132 lines (114 loc) · 3.01 KB
/
index.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
import { useState, useRef, useEffect } from 'react';
import {
interpret,
EventObject,
StateMachine,
State,
Interpreter,
InterpreterOptions,
MachineOptions
} from 'xstate';
interface UseMachineOptions<TContext> {
/**
* If provided, will be merged with machine's context.
*/
context?: Partial<TContext>;
/**
* If `true`, service will start immediately (before mount).
*/
immediate: boolean;
}
const defaultOptions = {
immediate: false
};
export function useMachine<TContext, TEvent extends EventObject>(
machine: StateMachine<TContext, any, TEvent>,
options: Partial<InterpreterOptions> &
Partial<UseMachineOptions<TContext>> &
Partial<MachineOptions<TContext, TEvent>> = defaultOptions
): [
State<TContext, TEvent>,
Interpreter<TContext, any, TEvent>['send'],
Interpreter<TContext, any, TEvent>
] {
const {
context,
guards,
actions,
activities,
services,
delays,
immediate,
...interpreterOptions
} = options;
const customMachine = machine.withConfig(
{
guards,
actions,
activities,
services,
delays
},
{
...machine.context,
...context
} as TContext
);
// Reference the service
const serviceRef = useRef<Interpreter<TContext, any, TEvent> | null>(null);
// Create the service only once
// See https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
if (serviceRef.current === null) {
serviceRef.current = interpret(
customMachine,
interpreterOptions
).onTransition(state => {
// Update the current machine state when a transition occurs
if (state.changed) {
setCurrent(state);
}
});
}
const service = serviceRef.current;
// Start service immediately (before mount) if specified in options
if (immediate) {
service.start();
}
// Keep track of the current machine state
const [current, setCurrent] = useState(service.initialState);
useEffect(() => {
// Start the service when the component mounts.
// Note: the service will start only if it hasn't started already.
service.start();
return () => {
// Stop the service when the component unmounts
service.stop();
};
}, []);
return [current, service.send, service];
}
export function useService<TContext, TEvent extends EventObject>(
service: Interpreter<TContext, any, TEvent>
): [
State<TContext, TEvent>,
Interpreter<TContext, any, TEvent>['send'],
Interpreter<TContext, any, TEvent>
] {
const [current, setCurrent] = useState(service.state);
useEffect(() => {
// Set to current service state as there is a possibility
// of a transition occurring between the initial useState()
// initialization and useEffect() commit.
setCurrent(service.state);
const listener = state => {
if (state.changed) {
setCurrent(state);
}
};
service.onTransition(listener);
return () => {
service.off(listener);
};
}, [service]);
return [current, service.send, service];
}