generated from adamrybinski/haunted-snow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
108 lines (91 loc) · 2.15 KB
/
utils.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
import { createMachine } from './deps.js';
export function getLazy(value) {
return typeof value === 'function' ? value() : value;
}
export function stringify(value) {
return JSON.stringify(value);
}
export function isReceiverEvent(event) {
if (!event) {
return false;
}
try {
if (typeof event === 'object' && 'type' in event && event.type.startsWith('service.')) {
return true;
}
} catch (e) {
return false;
}
return false;
}
export function parseState(stateJSON) {
const state = State.create(JSON.parse(stateJSON));
delete state.history;
return state;
}
export function parseReceiverEvent(event) {
switch (event.type) {
case 'service.event':
return { ...event,
event: JSON.parse(event.event)
};
case 'service.register':
return { ...event,
machine: createMachine(JSON.parse(event.machine)),
state: parseState(event.state)
};
case 'service.state':
return { ...event,
state: parseState(event.state)
};
default:
return event;
}
}
export function partition(items, predicate) {
const [truthy, falsy] = [[], []]
for (const item of items) {
if (predicate(item)) {
truthy.push(item);
} else {
falsy.push(item);
}
}
return [truthy, falsy];
}
export function toSCXMLEvent(event, scxmlEvent) {
if (!isString(event) && '$$type' in event && event.$$type === 'scxml') {
return event;
}
const eventObject = toEventObject(event);
return {
name: eventObject.type,
data: eventObject,
$$type: 'scxml',
type: 'external',
...scxmlEvent
};
}
export function toEventObject(event, payload) {
if (isString(event) || typeof event === 'number') {
return {
type: event,
...payload
};
}
return event;
}
export function toObserver(nextHandler, errorHandler, completionHandler) {
if (typeof nextHandler === 'object') {
return nextHandler;
}
const noop = () => void 0;
return {
next: nextHandler,
error: errorHandler || noop,
complete: completionHandler || noop
};
}
export function isString(value) {
return typeof value === 'string';
}