-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.js
202 lines (177 loc) · 6.39 KB
/
templates.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const { resolve, formatControlState } = require('./helpers');
const { INIT_STATE, INIT_EVENT, DEEP, SHALLOW, ACTION_IDENTITY } = require('kingly');
const implDoStr = `
function chain(arrFns, actions) {
return function chain_(s, ed, stg) {
return (
arrFns.reduce(function(acc, fn) {
var r = actions[fn](s, ed, stg);
return {
updates: acc.updates.concat(r.updates),
outputs: acc.outputs.concat(r.outputs),
};
}, { updates: [], outputs: [] })
);
};
}
`;
const templateIntro = ({usesHistoryStates, hasAutomaticEvents, nextEventMap, hasChainedActions}) => ([[
hasAutomaticEvents && `var nextEventMap = ${JSON.stringify(nextEventMap)}` || '',
`\n`,
].join(''),
hasChainedActions && implDoStr || "",
usesHistoryStates && `
function updateHistoryState(history, getAncestors, state_from_name) {
if (state_from_name === ${JSON.stringify(INIT_STATE)}) {
return history
}
else {
var ancestors = getAncestors(state_from_name) || [];
ancestors.reduce((oldAncestor, newAncestor) => {
// set the exited state in the history of all ancestors
history[${JSON.stringify(DEEP)}][newAncestor] = state_from_name;
history[${JSON.stringify(SHALLOW)}][newAncestor] = oldAncestor;
return newAncestor
}, state_from_name);
return history
}
}
`.trim() || ``,
``]).join('\n');
const transitionWithoutGuard = (action, to, usesHistoryStates, stateIndexList) => {
// We remove the actions with empty names
// Those means don't do anything
const actionNames = action.map(a => a.slice(3, -3)).filter(Boolean);
// console.warn(`transitionWithoutGuard > actionNames`, actionNames)
let computedStr = '';
let isActionIdentity = false;
if (actionNames.length === 0) {
isActionIdentity = true;
}
else if (actionNames.length === 1) {
computedStr = `var computed = actions["${actionNames[0]}"](s, ed, stg);`;
}
else {
computedStr = `var computed = chain(${JSON.stringify(actionNames)}, actions)(s, ed, stg);`;
}
const toIndex = resolve(to, stateIndexList);
// console.warn(`toIndex `, stateIndexList, to, resolve(to), toIndex)
return [
`function (s, ed, stg){`,
isActionIdentity
? `
// Transition to ${to};
cs = ${toIndex}; // No action, only cs changes!
`.trim()
: `
${computedStr}
// Transition to ${to};
cs = ${toIndex};
es = updateState(s, computed.updates);
`.trim(),
usesHistoryStates && `hs = updateHistoryState(hs, getAncestors, cs); \n` || ``,
isActionIdentity ? `return ${JSON.stringify(ACTION_IDENTITY())}` : `return computed`,
`},`,
].join('\n');
};
function transitionWithGuards(guards, usesHistoryStates, stateIndexList) {
let predicateStr = "";
let actionStr = "";
return [
`function (s, ed, stg){`,
`let computed = null;`,
guards.map(({ predicate: _predicateList, to, action: _actionList }, index) => {
const predicateList = _predicateList.map(x => x.slice(3, -3));
// We remove the actions entered in yed with empty strings (edge case)
const actionList= _actionList.map(x => x.slice(3, -3)).filter(Boolean);
// console.warn(`predicateList `, predicateList);
// console.warn(`actionList`, actionList);
if (predicateList.length === 1) {
predicateStr = `guards["${predicateList[0]}"](s, ed, stg)`
}
else {
// We have a guard if we arrive here => predicateList.length > 1
predicateStr = predicateList.map(p => `guards["${p}"](s, ed, stg)`).join(`&&`);
// predicateStr = `${JSON.stringify(predicateList)}.every(p => guards[p](s, ed, stg))`
}
if (actionList.length === 0) {
actionStr = `computed = ${JSON.stringify(ACTION_IDENTITY())};`
}
else if (actionList.length === 1) {
actionStr = `computed = actions["${actionList[0]}"](s, ed, stg);`
}
else {
// predicateList.length > 1
actionStr = `computed = chain(${JSON.stringify(actionList)}, actions)(s, ed, stg);`
}
return `${index ? 'else if' : 'if'} (${predicateStr}) {
${actionStr}
// Transition to ${formatControlState(to)};
cs = ${resolve(to, stateIndexList)};}`;
}).join('\n'),
`if (computed !== null) {
es = updateState(s, computed.updates);`,
usesHistoryStates && `hs = updateHistoryState(hs, getAncestors, cs);` || '',
` }
return computed
`,
`},`,
].join('\n');
}
const isGraphWithoutCompoundStates = stateAncestors => Object.keys(stateAncestors).length === 0;
const mainLoop = ({ nextEventMap, hasAutomaticEvents, stateAncestors }) => (`
function process(event){
var eventLabel = Object.keys(event)[0];
var eventData = event[eventLabel];
${isGraphWithoutCompoundStates(stateAncestors)
? `
var controlStateHandlingEvent = (eventHandlers[cs] || {})[eventLabel] && cs;
`.trim()
: `
var controlStateHandlingEvent = [cs].concat(getAncestors(cs)||[]).find(function(controlState){
return Boolean(eventHandlers[controlState] && eventHandlers[controlState][eventLabel]);
});
// console.warn('controlStateHandlingEvent', controlStateHandlingEvent);
`.trim()
}
if (controlStateHandlingEvent != null) {
// Run the handler
var computed = eventHandlers[controlStateHandlingEvent][eventLabel](es, eventData, stg);
// cs, es, hs have been updated in place by the handler
${hasAutomaticEvents ? `
return computed === null
// If transition, but no guards fulfilled => null, else
? [null]
: nextEventMap[cs] === -1
? computed.outputs
// Run automatic transition if any
: computed.outputs.concat(process({[nextEventMap[cs]]: eventData}))
`.trim() : `
// If transition, but no guards fulfilled => null, else => computed outputs
return computed === null ? [null] : computed.outputs;
`.trim()}
}
// Event is not accepted by the machine
else return null
}
// Start the machine
process({[${JSON.stringify(INIT_EVENT)}]: initialExtendedState});
return process
`).trim();
const esmExports = `
export { createStateMachine }
`.trim();
const cjsExports = `
module.exports = {
createStateMachine
}
`.trim();
module.exports = {
templateIntro,
transitionWithoutGuard,
transitionWithGuards,
mainLoop,
ImplDoStr: implDoStr,
esmExports,
cjsExports,
};