This repository was archived by the owner on Sep 14, 2022. It is now read-only.
forked from matthewp/robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
40 lines (36 loc) · 1.25 KB
/
debug.js
File metadata and controls
40 lines (36 loc) · 1.25 KB
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
import { d, invoke } from './machine.js';
const invokePromiseType = Object.getPrototypeOf(invoke(Promise.resolve()));
function unknownState(from, state) {
throw new Error(`Cannot transition from ${from} to unknown state: ${state}`);
}
d._create = function(current, states) {
if(!(current in states)) {
throw new Error(`Initial state [${current}] is not a known state.`);
}
for(let p in states) {
let state = states[p];
for(let [, candidates] of state.transitions) {
for(let {to} of candidates) {
if(!(to in states)) {
unknownState(p, to);
}
}
}
if (invokePromiseType.isPrototypeOf(state)) {
let hasErrorFrom = false;
for(let [, candidates] of state.transitions) {
for(let {from} of candidates) {
if (from === 'error') hasErrorFrom = true;
}
}
if(!hasErrorFrom) {
console.warn(
`When using invoke [current state: ${p}] with Promise-returning function, you need to add 'error' state. Otherwise, robot will hide errors in Promise-returning function`
);
}
}
}
};
d._send = function(eventName, currentStateName) {
throw new Error(`No transitions for event ${eventName} from the current state [${currentStateName}]`);
};