generated from adamrybinski/haunted-snow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
138 lines (129 loc) · 3.58 KB
/
server.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
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
import { interpret } from './deps.js';
import { createInspectMachine } from './inspectMachine.js';
import { stringify, toEventObject, toSCXMLEvent } from './utils.js';
const services = new Set();
const serviceMap = new Map();
const serviceListeners = new Set();
function createDevTools() {
globalThis.__xstate__ = {
services,
register: service => {
services.add(service);
serviceMap.set(service.sessionId, service);
serviceListeners.forEach(listener => listener(service));
service.onStop(() => {
services.delete(service);
serviceMap.delete(service.sessionId);
});
},
onRegister: listener => {
serviceListeners.add(listener);
services.forEach(service => listener(service));
return {
unsubscribe: () => {
serviceListeners.delete(listener);
}
};
}
};
}
export function inspect(options) {
const {
server
} = options;
createDevTools();
const inspectService = interpret(createInspectMachine(globalThis.__xstate__)).start();
let client;
server.on('connection', function connection(wss) {
client = {
id: '@@xstate/ws-client',
send: event => {
server.clients.forEach(ws => {
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify(event));
}
});
},
subscribe: () => {
return {
unsubscribe: () => void 0
};
},
getSnapshot: () => undefined
};
wss.on('message', function incoming(message) {
if (typeof message !== 'string') {
return;
}
const jsonMessage = JSON.parse(message);
inspectService.send({ ...jsonMessage,
client
});
});
});
globalThis.__xstate__.onRegister(service => {
inspectService.send({
type: 'service.register',
machine: JSON.stringify(service.machine),
state: JSON.stringify(service.state || service.initialState),
id: service.id,
sessionId: service.sessionId
});
inspectService.send({
type: 'service.event',
event: stringify((service.state || service.initialState)._event),
sessionId: service.sessionId
}); // monkey-patch service.send so that we know when an event was sent
// to a service *before* it is processed, since other events might occur
// while the sent one is being processed, which throws the order off
const originalSend = service.send.bind(service);
service.send = function inspectSend(event, payload) {
inspectService.send({
type: 'service.event',
event: stringify(toSCXMLEvent(toEventObject(event, payload))),
sessionId: service.sessionId
});
return originalSend(event, payload);
};
service.subscribe(state => {
inspectService.send({
type: 'service.state',
state: stringify(state),
sessionId: service.sessionId
});
});
service.onStop(() => {
inspectService.send({
type: 'service.stop',
sessionId: service.sessionId
});
});
service.subscribe(state => {
inspectService.send({
type: 'service.state',
state: JSON.stringify(state),
sessionId: service.sessionId
});
});
});
const inspector = {
id: '@@xstate/inspector',
send: event => {
inspectService.send(event);
},
subscribe: () => {
return {
unsubscribe: () => void 0
};
},
disconnect: () => {
server.close();
inspectService.stop();
},
getSnapshot: () => undefined
};
server.on('close', () => {
inspectService.stop();
});
return inspector;
}