-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine.ts
46 lines (43 loc) · 949 Bytes
/
machine.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
import { createMachine, interpret, actions } from 'xstate';
const wait = (ms: number) => new Promise((res) => setTimeout(res, ms));
export const countMachine = createMachine({
id: 'count',
initial: 'idle',
context: {
count: 0,
},
states: {
idle: {
on: {
COUNT: 'even',
},
},
even: {
on: {
COUNT: 'counting_even',
},
tags: ['resolve'],
},
counting_even: {
invoke: {
src: () => wait(1000),
onDone: {
target: 'odd',
actions: actions.assign({ count: (ctx) => (ctx as any).count + 1 }),
},
},
},
odd: {
invoke: {
src: () => wait(1000),
onDone: {
target: 'even',
actions: actions.assign({ count: (ctx) => (ctx as any).count + 1 }),
},
},
},
},
});
const service = interpret(countMachine).onTransition((state) => {
console.log(state.value);
});