generated from adamrybinski/haunted-snow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
inspectMachine.js
75 lines (74 loc) · 1.99 KB
/
inspectMachine.js
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
import { createMachine, assign } from './deps.js';
import { stringify } from './utils.js';
export function createInspectMachine(devTools = globalThis.__xstate__) {
const serviceMap = new Map();
const sub = devTools.onRegister(service => {
serviceMap.set(service.sessionId, service);
});
return createMachine({
initial: 'pendingConnection',
context: {
client: undefined
},
states: {
pendingConnection: {},
connected: {
on: {
'service.state': {
actions: (ctx, e) => ctx.client.send(e)
},
'service.event': {
actions: (ctx, e) => ctx.client.send(e)
},
'service.register': {
actions: (ctx, e) => ctx.client.send(e)
},
'service.stop': {
actions: (ctx, e) => ctx.client.send(e)
},
'xstate.event': {
actions: (_, e) => {
const {
event
} = e;
const scxmlEventObject = JSON.parse(event);
const service = serviceMap.get(scxmlEventObject.origin);
service?.send(scxmlEventObject);
}
},
unload: {
actions: ctx => {
ctx.client.send({
type: 'xstate.disconnect'
});
}
},
disconnect: 'disconnected'
}
},
disconnected: {
entry: () => {
sub.unsubscribe();
},
type: 'final'
}
},
on: {
'xstate.inspecting': {
target: '.connected',
actions: [assign({
client: (_, e) => e.client
}), ctx => {
devTools.services.forEach(service => {
ctx.client?.send({
type: 'service.register',
machine: stringify(service.machine),
state: stringify(service.state || service.initialState),
sessionId: service.sessionId
});
});
}]
}
}
});
}