-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.ts
151 lines (148 loc) · 4.98 KB
/
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
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
import * as actions from './actions.js';
import * as guards from './guards.js';
import type { Context, Event, State } from './types';
import { createMachine } from 'xstate';
export const machine = createMachine<Context, Event, State>(
{
context: {
data: null,
edits: null,
errors: [],
failure: null,
},
entry: ['validate'],
id: 'nucleon',
initial: 'init',
on: {
DELETE: { target: '#nucleon.busy.deleting' },
FETCH: { actions: ['clearData', 'clearEdits', 'clearErrors'], target: '#nucleon.busy.fetching' },
REFRESH: { target: '#nucleon.busy.fetching' },
SET_DATA: { actions: ['setData', 'clearEdits', 'clearErrors'], target: '#nucleon.init' },
},
states: {
busy: {
states: {
creating: {
invoke: {
onDone: { actions: ['setData', 'clearEdits', 'validate'], target: '#nucleon.idle.snapshot' },
onError: [
{ actions: 'setErrors', cond: 'isV8nErrorEvent', target: '#nucleon.idle' },
{ actions: 'setFailure', target: '#nucleon.fail' },
],
src: 'sendPost',
},
},
deleting: {
invoke: {
onDone: { actions: ['clearData', 'clearEdits', 'validate'], target: '#nucleon.idle.template' },
onError: [
{ actions: 'setErrors', cond: 'isV8nErrorEvent', target: '#nucleon.idle' },
{ actions: 'setFailure', target: '#nucleon.fail' },
],
src: 'sendDelete',
},
},
fetching: {
invoke: {
onDone: { actions: ['setData', 'clearErrors', 'validate'], target: '#nucleon.idle.snapshot' },
onError: [
{ actions: 'setErrors', cond: 'isV8nErrorEvent', target: '#nucleon.idle' },
{ actions: 'setFailure', target: '#nucleon.fail' },
],
src: 'sendGet',
},
},
updating: {
invoke: {
onDone: { actions: ['setData', 'clearEdits', 'validate'], target: '#nucleon.idle.snapshot' },
onError: [
{ actions: 'setErrors', cond: 'isV8nErrorEvent', target: '#nucleon.idle' },
{ actions: 'setFailure', target: '#nucleon.fail' },
],
src: 'sendPatch',
},
},
},
},
fail: {
exit: 'clearFailure',
},
idle: {
initial: 'unknown',
on: { EDIT: { actions: ['applyEdit', 'validate'], target: '.unknown' } },
states: {
snapshot: {
initial: 'unknown',
states: {
clean: {
initial: 'unknown',
states: {
invalid: {},
unknown: { always: [{ cond: 'hasErrors', target: 'invalid' }, { target: 'valid' }] },
valid: {},
},
},
dirty: {
initial: 'unknown',
on: {
UNDO: {
actions: ['clearEdits', 'clearErrors', 'validate'],
target: '#nucleon.idle.snapshot.clean',
},
},
states: {
invalid: {},
unknown: { always: [{ cond: 'hasErrors', target: 'invalid' }, { target: 'valid' }] },
valid: { on: { SUBMIT: { target: '#nucleon.busy.updating' } } },
},
},
unknown: {
always: [{ cond: 'hasEdits', target: 'dirty' }, { target: 'clean' }],
},
},
},
template: {
initial: 'unknown',
states: {
clean: {
initial: 'unknown',
states: {
invalid: {},
unknown: { always: [{ cond: 'hasErrors', target: 'invalid' }, { target: 'valid' }] },
valid: {},
},
},
dirty: {
initial: 'unknown',
on: {
UNDO: {
actions: ['clearEdits', 'clearErrors', 'validate'],
target: '#nucleon.idle.template.clean',
},
},
states: {
invalid: {},
unknown: { always: [{ cond: 'hasErrors', target: 'invalid' }, { target: 'valid' }] },
valid: { on: { SUBMIT: { target: '#nucleon.busy.creating' } } },
},
},
unknown: {
always: [{ cond: 'hasEdits', target: 'dirty' }, { target: 'clean' }],
},
},
},
unknown: {
always: [{ cond: 'hasData', target: 'snapshot' }, { target: 'template' }],
},
},
},
init: {
always: [{ cond: 'hasData', target: 'idle.snapshot' }, { target: 'idle.template' }],
},
},
},
{
actions,
guards,
}
);