-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (75 loc) · 1.74 KB
/
index.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
import rosmaro from 'rosmaro';
const graph = {
"main": {
"type": "graph",
"nodes": {
"Prince": "Prince",
"Frog": "Frog"
},
"arrows": {
"Prince": {
"ate a pizza": {
"target": "Frog",
"entryPoint": "start"
}
}
},
"entryPoints": {
"start": {
"target": "Prince",
"entryPoint": "start"
}
}
},
"Prince": {
"type": "leaf"
},
"Frog": {
"type": "leaf"
}
};
const main = ({children, action}) => Object.values(children)[0]({action});
const Frog = ({action, context}) => {
switch (action.type) {
case 'INTRODUCE_YOURSELF':
return {result: "Ribbit! Ribbit!", arrows: [], context};
default:
return {result: undefined, arrows: [], context};
}
};
const Prince = ({action, context, node}) => {
switch (action.type) {
case 'INTRODUCE_YOURSELF':
return {result: "I am The Prince of Rosmaro!", arrows: [], context};
case 'EAT':
const arrows = action.dish === 'pizza'
? [[[node.id, 'ate a pizza']]]
: [];
return {result: undefined, arrows, context};
default:
return {result: undefined, arrows: [], context};
}
};
const bindings = {
'main': {handler: main},
'main:Prince': {handler: Prince},
'main:Frog': {handler: Frog},
};
const model = rosmaro({graph, bindings});
let state;
[
{type: 'INTRODUCE_YOURSELF'},
{type: 'EAT', dish: 'yakisoba'},
{type: 'INTRODUCE_YOURSELF'},
{type: 'EAT', dish: 'pizza'},
{type: 'INTRODUCE_YOURSELF'}
].forEach(action => {
const {state: newState, result} = model({state, action});
state = newState;
console.log(result);
});
// I am The Prince of Rosmaro!
// undefined
// I am The Prince of Rosmaro!
// undefined
// Ribbit! Ribbit!