@statewalker/fsm 0.15.2 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +337 -260
- package/package.json +24 -21
- package/src/FsmBaseClass.ts +52 -0
- package/src/FsmProcess.ts +184 -0
- package/src/FsmState.ts +87 -0
- package/src/FsmStateConfig.ts +13 -0
- package/src/FsmStateDescriptor.ts +47 -0
- package/src/context/handlers.ts +30 -0
- package/src/context/index.ts +3 -0
- package/src/context/printer.ts +48 -0
- package/src/context/tracer.ts +19 -0
- package/src/index.ts +5 -0
- package/src/utils/bindMethods.ts +7 -0
- package/dist/index-umd.js +0 -311
- package/dist/index-umd.min.js +0 -2
- package/dist/index.min.js +0 -2
- package/index.js +0 -1
- package/src/KEYS.js +0 -7
- package/src/buildStateDescriptor.js +0 -34
- package/src/checkProcessDescriptor.js +0 -54
- package/src/getAllStateKeys.js +0 -27
- package/src/getAllTransitions.js +0 -33
- package/src/getStateDescriptor.js +0 -8
- package/src/getTargetStateKey.js +0 -19
- package/src/index.js +0 -10
- package/src/newAsyncProcess.js +0 -47
- package/src/newProcessInstance.js +0 -44
- package/src/newSyncProcess.js +0 -27
package/dist/index-umd.js
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
// @statewalker/fsm v0.15.2 https://github.com/statewalker/statewalker-fsm Copyright 2022 Mikhail Kotelnikov
|
|
2
|
-
(function (global, factory) {
|
|
3
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@statewalker/tree')) :
|
|
4
|
-
typeof define === 'function' && define.amd ? define(['exports', '@statewalker/tree'], factory) :
|
|
5
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.statewalker = global.statewalker || {}, global.statewalker.fsm = global.statewalker.fsm || {}), global.statewalker.tree));
|
|
6
|
-
})(this, (function (exports, tree) { 'use strict';
|
|
7
|
-
|
|
8
|
-
function buildStateDescriptor(config = {}) {
|
|
9
|
-
const { key, transitions = [], states = {}, options, ...params } = config;
|
|
10
|
-
const result = {
|
|
11
|
-
key,
|
|
12
|
-
transitions: {},
|
|
13
|
-
states: {},
|
|
14
|
-
options: Object.assign(options || {}, params)
|
|
15
|
-
};
|
|
16
|
-
for (const t of transitions) {
|
|
17
|
-
let stateKey, eventKey, targetStateKey;
|
|
18
|
-
if (Array.isArray(t)) {
|
|
19
|
-
stateKey = t[0];
|
|
20
|
-
eventKey = t[1];
|
|
21
|
-
targetStateKey = t[2];
|
|
22
|
-
} else {
|
|
23
|
-
const { from, event, to } = t;
|
|
24
|
-
stateKey = from;
|
|
25
|
-
eventKey = event;
|
|
26
|
-
targetStateKey = to;
|
|
27
|
-
}
|
|
28
|
-
const index = result.transitions[stateKey] = result.transitions[stateKey] || {};
|
|
29
|
-
index[eventKey] = targetStateKey;
|
|
30
|
-
}
|
|
31
|
-
if (Array.isArray(states)) {
|
|
32
|
-
for (const s of states) {
|
|
33
|
-
result.states[s.key] = buildStateDescriptor(s);
|
|
34
|
-
}
|
|
35
|
-
} else if (states) {
|
|
36
|
-
for (const key of Object.keys(states)) {
|
|
37
|
-
result.states[key] = buildStateDescriptor(states[key]);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return result;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
var KEYS = {
|
|
44
|
-
STATE_ANY : "*",
|
|
45
|
-
STATE_INITIAL : "",
|
|
46
|
-
STATE_FINAL : "",
|
|
47
|
-
EVENT_ANY : "*",
|
|
48
|
-
EVENT_EMPTY : "",
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
function checkProcessDescriptor(descriptor) {
|
|
52
|
-
return checkStateDescriptor(descriptor);
|
|
53
|
-
|
|
54
|
-
function checkStateDescriptor(descriptor, report = [], stack = []) {
|
|
55
|
-
stack = [...stack, descriptor.key];
|
|
56
|
-
const result = Object.assign({ key: descriptor.key }, checkStateTransitions(descriptor));
|
|
57
|
-
if (result.deadendStates.length || result.unreachableStates.length) {
|
|
58
|
-
const info = { path: stack };
|
|
59
|
-
report.push(info);
|
|
60
|
-
if (result.deadendStates.length) info.deadendStates = result.deadendStates;
|
|
61
|
-
if (result.unreachableStates.length) info.unreachableStates = result.unreachableStates;
|
|
62
|
-
}
|
|
63
|
-
for (const subDescriptor of Object.values(descriptor.states)) {
|
|
64
|
-
checkStateDescriptor(subDescriptor, report, stack);
|
|
65
|
-
}
|
|
66
|
-
return report;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function checkStateTransitions(descriptor) {
|
|
70
|
-
const sourceStates = {};
|
|
71
|
-
const targetStates = {};
|
|
72
|
-
for (const [from, destinations] of Object.entries(descriptor.transitions)) {
|
|
73
|
-
sourceStates[from] = (sourceStates[from] || 0) + 1;
|
|
74
|
-
for (const targetKey of Object.values(destinations)) {
|
|
75
|
-
targetStates[targetKey] = (targetStates[targetKey] || 0) + 1;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
// Unreachable states - no transitions leading to these states
|
|
79
|
-
const unreachableStates = [];
|
|
80
|
-
// Deadend states - no transitions from these states
|
|
81
|
-
const deadendStates = [];
|
|
82
|
-
|
|
83
|
-
// Unreachable states - no transitions leading to these states
|
|
84
|
-
for (const sourceKey of Object.keys(sourceStates)) {
|
|
85
|
-
if (sourceKey !== KEYS.STATE_ANY &&
|
|
86
|
-
sourceKey !== KEYS.STATE_FINAL &&
|
|
87
|
-
sourceKey !== KEYS.STATE_INITIAL &&
|
|
88
|
-
!(sourceKey in targetStates)) {
|
|
89
|
-
unreachableStates.push(sourceKey);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Deadend states - no transitions from these states
|
|
94
|
-
for (const targetKey of Object.keys(targetStates)) {
|
|
95
|
-
if (targetKey !== KEYS.STATE_FINAL && !(targetKey in sourceStates)) {
|
|
96
|
-
deadendStates.push(targetKey);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return { unreachableStates, deadendStates }
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function getStateDescriptor(process, stateKey) {
|
|
105
|
-
let descriptor;
|
|
106
|
-
for (let i = process.stack.length - 1; !descriptor && i >= 0; i--) {
|
|
107
|
-
const states = process.stack[i].descriptor.states || {};
|
|
108
|
-
descriptor = states[stateKey];
|
|
109
|
-
}
|
|
110
|
-
return descriptor || { transitions: {}, states: {} };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* This method returns a list of all states used in
|
|
115
|
-
* the specified state descriptor.
|
|
116
|
-
*
|
|
117
|
-
* @param {object} descriptor compiled state descriptor
|
|
118
|
-
* defining transitions between sub-states and sub-states
|
|
119
|
-
* @returns a sorted list of all states
|
|
120
|
-
*/
|
|
121
|
-
function getAllStateKeys(descriptor) {
|
|
122
|
-
const index = {};
|
|
123
|
-
const add = (key) => index[key] = (index[key] || 0) + 1;
|
|
124
|
-
visit(descriptor);
|
|
125
|
-
return Object.keys(index).sort();
|
|
126
|
-
function visit(descriptor) {
|
|
127
|
-
add(descriptor.key);
|
|
128
|
-
for (const [from, destinations] of Object.entries(descriptor.transitions)) {
|
|
129
|
-
add(from);
|
|
130
|
-
for (const targetKey of Object.values(destinations)) {
|
|
131
|
-
add(targetKey);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
for (const [stateKey, stateDescriptor] of Object.entries(descriptor.states)) {
|
|
135
|
-
add(stateKey);
|
|
136
|
-
visit(stateDescriptor);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function getTargetStateKey(descriptor, stateKey, eventKey) {
|
|
142
|
-
const pairs = [
|
|
143
|
-
[stateKey, eventKey],
|
|
144
|
-
[KEYS.STATE_ANY, eventKey],
|
|
145
|
-
[stateKey, KEYS.EVENT_ANY],
|
|
146
|
-
[KEYS.STATE_ANY, KEYS.EVENT_ANY]
|
|
147
|
-
];
|
|
148
|
-
let targetKey;
|
|
149
|
-
for (let i = 0, len = pairs.length; (targetKey === undefined) && i < len; i++) {
|
|
150
|
-
const [stateKey, eventKey] = pairs[i];
|
|
151
|
-
const stateTransitions = descriptor.transitions[stateKey];
|
|
152
|
-
if (!stateTransitions) continue;
|
|
153
|
-
targetKey = stateTransitions[eventKey];
|
|
154
|
-
if (targetKey === undefined) targetKey = stateTransitions[KEYS.EVENT_ANY];
|
|
155
|
-
}
|
|
156
|
-
return (targetKey !== undefined) ? targetKey : KEYS.STATE_FINAL;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function newProcessInstance({
|
|
160
|
-
config,
|
|
161
|
-
descriptor,
|
|
162
|
-
newState = (obj) => obj,
|
|
163
|
-
newProcess = (descriptor) => ({
|
|
164
|
-
descriptor,
|
|
165
|
-
status: 0,
|
|
166
|
-
stack: [],
|
|
167
|
-
event: undefined,
|
|
168
|
-
current: undefined
|
|
169
|
-
})
|
|
170
|
-
}) {
|
|
171
|
-
descriptor = descriptor || buildStateDescriptor(config);
|
|
172
|
-
const process = newProcess(descriptor);
|
|
173
|
-
const loadNextState = () => {
|
|
174
|
-
let key, descriptor;
|
|
175
|
-
if (!process.status) {
|
|
176
|
-
descriptor = process.descriptor;
|
|
177
|
-
key = descriptor.key;
|
|
178
|
-
} else {
|
|
179
|
-
if (!process.stack.length) {
|
|
180
|
-
key = KEYS.STATE_FINAL;
|
|
181
|
-
} else {
|
|
182
|
-
const eventKey = process.event.key || KEYS.EVENT_EMPTY;
|
|
183
|
-
const parentDescriptor = process.stack[process.stack.length - 1].descriptor;
|
|
184
|
-
const stateKey = process.status & tree.MODE.ENTER
|
|
185
|
-
? KEYS.STATE_INITIAL
|
|
186
|
-
: process.current ? process.current.key : KEYS.STATE_FINAL;
|
|
187
|
-
key = getTargetStateKey(parentDescriptor, stateKey, eventKey);
|
|
188
|
-
if (key) {
|
|
189
|
-
descriptor = getStateDescriptor(process, key);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return key ? newState({ process, key, descriptor }) : null;
|
|
194
|
-
};
|
|
195
|
-
return [process, loadNextState];
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function newAsyncProcess({
|
|
199
|
-
config,
|
|
200
|
-
descriptor,
|
|
201
|
-
before, after,
|
|
202
|
-
newProcess,
|
|
203
|
-
newState,
|
|
204
|
-
handleError = console.error
|
|
205
|
-
}) {
|
|
206
|
-
const [process, loadNextState] = newProcessInstance({
|
|
207
|
-
config,
|
|
208
|
-
descriptor,
|
|
209
|
-
newProcess,
|
|
210
|
-
newState
|
|
211
|
-
});
|
|
212
|
-
const shift = tree.newAsyncTreeWalker(before, after, process);
|
|
213
|
-
process.finished = false;
|
|
214
|
-
process.next = (event) => {
|
|
215
|
-
process.nextEvent = event;
|
|
216
|
-
return process.running = process.running || Promise
|
|
217
|
-
.resolve()
|
|
218
|
-
.then(async () => {
|
|
219
|
-
try {
|
|
220
|
-
process.event = process.nextEvent;
|
|
221
|
-
process.nextEvent = undefined;
|
|
222
|
-
while (!process.finished && process.event) {
|
|
223
|
-
process.finished = !await shift(loadNextState);
|
|
224
|
-
if ((process.status & tree.MODE.EXIT) && process.nextEvent) {
|
|
225
|
-
// Consume the next event if it exists.
|
|
226
|
-
process.event = process.nextEvent;
|
|
227
|
-
process.nextEvent = undefined;
|
|
228
|
-
} else if (process.status & tree.MODE.LEAF) {
|
|
229
|
-
// Returns control when the process is in a "leaf" state (a state without children)
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
} catch (error) {
|
|
234
|
-
handleError(error);
|
|
235
|
-
}
|
|
236
|
-
})
|
|
237
|
-
.finally(() => process.running = undefined)
|
|
238
|
-
.then(() => !process.finished);
|
|
239
|
-
};
|
|
240
|
-
return process;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Returns all possible transitions from the current state of the process.
|
|
245
|
-
* @param {Object} options - method parameters
|
|
246
|
-
* @param {Process} options.process - the current process for which
|
|
247
|
-
* all transitions should be returned
|
|
248
|
-
* @returns {Array<Object>} - an array of all transitions;
|
|
249
|
-
* each entry in this returned array contains the following keys:
|
|
250
|
-
* 1) parentStateKey - key of the state where the transition can be activated
|
|
251
|
-
* 2) sourceStateKey - the initial transition state
|
|
252
|
-
* 3) eventKey - key of the event activating transition
|
|
253
|
-
* 4) targetStateKey - the resulting state for this transition
|
|
254
|
-
*
|
|
255
|
-
*/
|
|
256
|
-
function getAllTransitions(process) {
|
|
257
|
-
const list = [];
|
|
258
|
-
let stateKey = process.current ? process.current.key : KEYS.STATE_INITIAL;
|
|
259
|
-
for (let i = process.stack.length - 1; i >= 0; i--) {
|
|
260
|
-
const parentState = process.stack[i];
|
|
261
|
-
const parentStateKey = parentState.key;
|
|
262
|
-
const descriptor = parentState.descriptor;
|
|
263
|
-
for (const sKey of [stateKey, KEYS.STATE_ANY]) {
|
|
264
|
-
const stateTransitions = descriptor.transitions[sKey];
|
|
265
|
-
if (!stateTransitions) continue;
|
|
266
|
-
for (const[eventKey, targetKey] of Object.entries(stateTransitions)) {
|
|
267
|
-
list.push([parentStateKey, sKey, eventKey, targetKey]);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
stateKey = parentStateKey;
|
|
271
|
-
}
|
|
272
|
-
return list;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
function newSyncProcess({
|
|
276
|
-
config,
|
|
277
|
-
descriptor,
|
|
278
|
-
before, after,
|
|
279
|
-
newProcess,
|
|
280
|
-
newState,
|
|
281
|
-
}) {
|
|
282
|
-
const [process, loadNextState] = newProcessInstance({
|
|
283
|
-
config,
|
|
284
|
-
descriptor,
|
|
285
|
-
newProcess,
|
|
286
|
-
newState,
|
|
287
|
-
});
|
|
288
|
-
const shift = tree.newTreeWalker(before, after, process);
|
|
289
|
-
process.dispatch = process.next = (event) => {
|
|
290
|
-
if (typeof event === 'string') event = { key : event };
|
|
291
|
-
process.event = event;
|
|
292
|
-
while (process.event) {
|
|
293
|
-
if (!shift(loadNextState)) return false;
|
|
294
|
-
if (process.status & tree.MODE.LEAF) return true;
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
return process;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
exports.KEYS = KEYS;
|
|
301
|
-
exports.buildStateDescriptor = buildStateDescriptor;
|
|
302
|
-
exports.checkProcessDescriptor = checkProcessDescriptor;
|
|
303
|
-
exports.getAllStateKeys = getAllStateKeys;
|
|
304
|
-
exports.getAllTransitions = getAllTransitions;
|
|
305
|
-
exports.getStateDescriptor = getStateDescriptor;
|
|
306
|
-
exports.getTargetStateKey = getTargetStateKey;
|
|
307
|
-
exports.newAsyncProcess = newAsyncProcess;
|
|
308
|
-
exports.newProcessInstance = newProcessInstance;
|
|
309
|
-
exports.newSyncProcess = newSyncProcess;
|
|
310
|
-
|
|
311
|
-
}));
|
package/dist/index-umd.min.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
// @statewalker/fsm v0.15.2 https://github.com/statewalker/statewalker-fsm Copyright 2022 Mikhail Kotelnikov
|
|
2
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@statewalker/tree")):"function"==typeof define&&define.amd?define(["exports","@statewalker/tree"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).statewalker=e.statewalker||{},e.statewalker.fsm=e.statewalker.fsm||{}),e.statewalker.tree)}(this,(function(e,t){"use strict";function n(e={}){const{key:t,transitions:s=[],states:r={},options:o,...c}=e,i={key:t,transitions:{},states:{},options:Object.assign(o||{},c)};for(const e of s){let t,n,s;if(Array.isArray(e))t=e[0],n=e[1],s=e[2];else{const{from:r,event:o,to:c}=e;t=r,n=o,s=c}(i.transitions[t]=i.transitions[t]||{})[n]=s}if(Array.isArray(r))for(const e of r)i.states[e.key]=n(e);else if(r)for(const e of Object.keys(r))i.states[e]=n(r[e]);return i}var s={STATE_ANY:"*",STATE_INITIAL:"",STATE_FINAL:"",EVENT_ANY:"*",EVENT_EMPTY:""};function r(e,t){let n;for(let s=e.stack.length-1;!n&&s>=0;s--){n=(e.stack[s].descriptor.states||{})[t]}return n||{transitions:{},states:{}}}function o(e,t,n){const r=[[t,n],[s.STATE_ANY,n],[t,s.EVENT_ANY],[s.STATE_ANY,s.EVENT_ANY]];let o;for(let t=0,n=r.length;void 0===o&&t<n;t++){const[n,c]=r[t],i=e.transitions[n];i&&(o=i[c],void 0===o&&(o=i[s.EVENT_ANY]))}return void 0!==o?o:s.STATE_FINAL}function c({config:e,descriptor:c,newState:i=(e=>e),newProcess:a=(e=>({descriptor:e,status:0,stack:[],event:void 0,current:void 0}))}){const f=a(c=c||n(e));return[f,()=>{let e,n;if(f.status)if(f.stack.length){const c=f.event.key||s.EVENT_EMPTY;e=o(f.stack[f.stack.length-1].descriptor,f.status&t.MODE.ENTER?s.STATE_INITIAL:f.current?f.current.key:s.STATE_FINAL,c),e&&(n=r(f,e))}else e=s.STATE_FINAL;else n=f.descriptor,e=n.key;return e?i({process:f,key:e,descriptor:n}):null}]}e.KEYS=s,e.buildStateDescriptor=n,e.checkProcessDescriptor=function(e){return function e(t,n=[],r=[]){r=[...r,t.key];const o=Object.assign({key:t.key},function(e){const t={},n={};for(const[s,r]of Object.entries(e.transitions)){t[s]=(t[s]||0)+1;for(const e of Object.values(r))n[e]=(n[e]||0)+1}const r=[],o=[];for(const e of Object.keys(t))e===s.STATE_ANY||e===s.STATE_FINAL||e===s.STATE_INITIAL||e in n||r.push(e);for(const e of Object.keys(n))e===s.STATE_FINAL||e in t||o.push(e);return{unreachableStates:r,deadendStates:o}}(t));if(o.deadendStates.length||o.unreachableStates.length){const e={path:r};n.push(e),o.deadendStates.length&&(e.deadendStates=o.deadendStates),o.unreachableStates.length&&(e.unreachableStates=o.unreachableStates)}for(const s of Object.values(t.states))e(s,n,r);return n}(e)},e.getAllStateKeys=function(e){const t={},n=e=>t[e]=(t[e]||0)+1;return function e(t){n(t.key);for(const[e,s]of Object.entries(t.transitions)){n(e);for(const e of Object.values(s))n(e)}for(const[s,r]of Object.entries(t.states))n(s),e(r)}(e),Object.keys(t).sort()},e.getAllTransitions=function(e){const t=[];let n=e.current?e.current.key:s.STATE_INITIAL;for(let r=e.stack.length-1;r>=0;r--){const o=e.stack[r],c=o.key,i=o.descriptor;for(const e of[n,s.STATE_ANY]){const n=i.transitions[e];if(n)for(const[s,r]of Object.entries(n))t.push([c,e,s,r])}n=c}return t},e.getStateDescriptor=r,e.getTargetStateKey=o,e.newAsyncProcess=function({config:e,descriptor:n,before:s,after:r,newProcess:o,newState:i,handleError:a=console.error}){const[f,u]=c({config:e,descriptor:n,newProcess:o,newState:i}),l=t.newAsyncTreeWalker(s,r,f);return f.finished=!1,f.next=e=>(f.nextEvent=e,f.running=f.running||Promise.resolve().then((async()=>{try{for(f.event=f.nextEvent,f.nextEvent=void 0;!f.finished&&f.event;)if(f.finished=!await l(u),f.status&t.MODE.EXIT&&f.nextEvent)f.event=f.nextEvent,f.nextEvent=void 0;else if(f.status&t.MODE.LEAF)break}catch(e){a(e)}})).finally((()=>f.running=void 0)).then((()=>!f.finished))),f},e.newProcessInstance=c,e.newSyncProcess=function({config:e,descriptor:n,before:s,after:r,newProcess:o,newState:i}){const[a,f]=c({config:e,descriptor:n,newProcess:o,newState:i}),u=t.newTreeWalker(s,r,a);return a.dispatch=a.next=e=>{for("string"==typeof e&&(e={key:e}),a.event=e;a.event;){if(!u(f))return!1;if(a.status&t.MODE.LEAF)return!0}},a}}));
|
package/dist/index.min.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
// @statewalker/fsm v0.15.2 https://github.com/statewalker/statewalker-fsm Copyright 2022 Mikhail Kotelnikov
|
|
2
|
-
import{MODE as t,newAsyncTreeWalker as e,newTreeWalker as n}from"@statewalker/tree";function s(t={}){const{key:e,transitions:n=[],states:r={},options:o,...c}=t,i={key:e,transitions:{},states:{},options:Object.assign(o||{},c)};for(const t of n){let e,n,s;if(Array.isArray(t))e=t[0],n=t[1],s=t[2];else{const{from:r,event:o,to:c}=t;e=r,n=o,s=c}(i.transitions[e]=i.transitions[e]||{})[n]=s}if(Array.isArray(r))for(const t of r)i.states[t.key]=s(t);else if(r)for(const t of Object.keys(r))i.states[t]=s(r[t]);return i}var r={STATE_ANY:"*",STATE_INITIAL:"",STATE_FINAL:"",EVENT_ANY:"*",EVENT_EMPTY:""};function o(t){return function t(e,n=[],s=[]){s=[...s,e.key];const o=Object.assign({key:e.key},function(t){const e={},n={};for(const[s,r]of Object.entries(t.transitions)){e[s]=(e[s]||0)+1;for(const t of Object.values(r))n[t]=(n[t]||0)+1}const s=[],o=[];for(const t of Object.keys(e))t===r.STATE_ANY||t===r.STATE_FINAL||t===r.STATE_INITIAL||t in n||s.push(t);for(const t of Object.keys(n))t===r.STATE_FINAL||t in e||o.push(t);return{unreachableStates:s,deadendStates:o}}(e));if(o.deadendStates.length||o.unreachableStates.length){const t={path:s};n.push(t),o.deadendStates.length&&(t.deadendStates=o.deadendStates),o.unreachableStates.length&&(t.unreachableStates=o.unreachableStates)}for(const r of Object.values(e.states))t(r,n,s);return n}(t)}function c(t,e){let n;for(let s=t.stack.length-1;!n&&s>=0;s--){n=(t.stack[s].descriptor.states||{})[e]}return n||{transitions:{},states:{}}}function i(t){const e={},n=t=>e[t]=(e[t]||0)+1;return function t(e){n(e.key);for(const[t,s]of Object.entries(e.transitions)){n(t);for(const t of Object.values(s))n(t)}for(const[s,r]of Object.entries(e.states))n(s),t(r)}(t),Object.keys(e).sort()}function a(t,e,n){const s=[[e,n],[r.STATE_ANY,n],[e,r.EVENT_ANY],[r.STATE_ANY,r.EVENT_ANY]];let o;for(let e=0,n=s.length;void 0===o&&e<n;e++){const[n,c]=s[e],i=t.transitions[n];i&&(o=i[c],void 0===o&&(o=i[r.EVENT_ANY]))}return void 0!==o?o:r.STATE_FINAL}function f({config:e,descriptor:n,newState:o=(t=>t),newProcess:i=(t=>({descriptor:t,status:0,stack:[],event:void 0,current:void 0}))}){const f=i(n=n||s(e));return[f,()=>{let e,n;if(f.status)if(f.stack.length){const s=f.event.key||r.EVENT_EMPTY;e=a(f.stack[f.stack.length-1].descriptor,f.status&t.ENTER?r.STATE_INITIAL:f.current?f.current.key:r.STATE_FINAL,s),e&&(n=c(f,e))}else e=r.STATE_FINAL;else n=f.descriptor,e=n.key;return e?o({process:f,key:e,descriptor:n}):null}]}function u({config:n,descriptor:s,before:r,after:o,newProcess:c,newState:i,handleError:a=console.error}){const[u,T]=f({config:n,descriptor:s,newProcess:c,newState:i}),E=e(r,o,u);return u.finished=!1,u.next=e=>(u.nextEvent=e,u.running=u.running||Promise.resolve().then((async()=>{try{for(u.event=u.nextEvent,u.nextEvent=void 0;!u.finished&&u.event;)if(u.finished=!await E(T),u.status&t.EXIT&&u.nextEvent)u.event=u.nextEvent,u.nextEvent=void 0;else if(u.status&t.LEAF)break}catch(t){a(t)}})).finally((()=>u.running=void 0)).then((()=>!u.finished))),u}function T(t){const e=[];let n=t.current?t.current.key:r.STATE_INITIAL;for(let s=t.stack.length-1;s>=0;s--){const o=t.stack[s],c=o.key,i=o.descriptor;for(const t of[n,r.STATE_ANY]){const n=i.transitions[t];if(n)for(const[s,r]of Object.entries(n))e.push([c,t,s,r])}n=c}return e}function E({config:e,descriptor:s,before:r,after:o,newProcess:c,newState:i}){const[a,u]=f({config:e,descriptor:s,newProcess:c,newState:i}),T=n(r,o,a);return a.dispatch=a.next=e=>{for("string"==typeof e&&(e={key:e}),a.event=e;a.event;){if(!T(u))return!1;if(a.status&t.LEAF)return!0}},a}export{r as KEYS,s as buildStateDescriptor,o as checkProcessDescriptor,i as getAllStateKeys,T as getAllTransitions,c as getStateDescriptor,a as getTargetStateKey,u as newAsyncProcess,f as newProcessInstance,E as newSyncProcess};
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './src/index.js';
|
package/src/KEYS.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export default function buildStateDescriptor(config = {}) {
|
|
2
|
-
const { key, transitions = [], states = {}, options, ...params } = config;
|
|
3
|
-
const result = {
|
|
4
|
-
key,
|
|
5
|
-
transitions: {},
|
|
6
|
-
states: {},
|
|
7
|
-
options: Object.assign(options || {}, params)
|
|
8
|
-
};
|
|
9
|
-
for (const t of transitions) {
|
|
10
|
-
let stateKey, eventKey, targetStateKey;
|
|
11
|
-
if (Array.isArray(t)) {
|
|
12
|
-
stateKey = t[0];
|
|
13
|
-
eventKey = t[1];
|
|
14
|
-
targetStateKey = t[2];
|
|
15
|
-
} else {
|
|
16
|
-
const { from, event, to } = t;
|
|
17
|
-
stateKey = from;
|
|
18
|
-
eventKey = event;
|
|
19
|
-
targetStateKey = to;
|
|
20
|
-
}
|
|
21
|
-
const index = result.transitions[stateKey] = result.transitions[stateKey] || {};
|
|
22
|
-
index[eventKey] = targetStateKey;
|
|
23
|
-
}
|
|
24
|
-
if (Array.isArray(states)) {
|
|
25
|
-
for (const s of states) {
|
|
26
|
-
result.states[s.key] = buildStateDescriptor(s);
|
|
27
|
-
}
|
|
28
|
-
} else if (states) {
|
|
29
|
-
for (const key of Object.keys(states)) {
|
|
30
|
-
result.states[key] = buildStateDescriptor(states[key]);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return result;
|
|
34
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import KEYS from "./KEYS.js";
|
|
2
|
-
|
|
3
|
-
export default function checkProcessDescriptor(descriptor) {
|
|
4
|
-
return checkStateDescriptor(descriptor);
|
|
5
|
-
|
|
6
|
-
function checkStateDescriptor(descriptor, report = [], stack = []) {
|
|
7
|
-
stack = [...stack, descriptor.key];
|
|
8
|
-
const result = Object.assign({ key: descriptor.key }, checkStateTransitions(descriptor));
|
|
9
|
-
if (result.deadendStates.length || result.unreachableStates.length) {
|
|
10
|
-
const info = { path: stack };
|
|
11
|
-
report.push(info);
|
|
12
|
-
if (result.deadendStates.length) info.deadendStates = result.deadendStates;
|
|
13
|
-
if (result.unreachableStates.length) info.unreachableStates = result.unreachableStates;
|
|
14
|
-
}
|
|
15
|
-
for (const subDescriptor of Object.values(descriptor.states)) {
|
|
16
|
-
checkStateDescriptor(subDescriptor, report, stack);
|
|
17
|
-
}
|
|
18
|
-
return report;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function checkStateTransitions(descriptor) {
|
|
22
|
-
const sourceStates = {};
|
|
23
|
-
const targetStates = {};
|
|
24
|
-
for (const [from, destinations] of Object.entries(descriptor.transitions)) {
|
|
25
|
-
sourceStates[from] = (sourceStates[from] || 0) + 1;
|
|
26
|
-
for (const targetKey of Object.values(destinations)) {
|
|
27
|
-
targetStates[targetKey] = (targetStates[targetKey] || 0) + 1;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
// Unreachable states - no transitions leading to these states
|
|
31
|
-
const unreachableStates = [];
|
|
32
|
-
// Deadend states - no transitions from these states
|
|
33
|
-
const deadendStates = [];
|
|
34
|
-
|
|
35
|
-
// Unreachable states - no transitions leading to these states
|
|
36
|
-
for (const sourceKey of Object.keys(sourceStates)) {
|
|
37
|
-
if (sourceKey !== KEYS.STATE_ANY &&
|
|
38
|
-
sourceKey !== KEYS.STATE_FINAL &&
|
|
39
|
-
sourceKey !== KEYS.STATE_INITIAL &&
|
|
40
|
-
!(sourceKey in targetStates)) {
|
|
41
|
-
unreachableStates.push(sourceKey);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Deadend states - no transitions from these states
|
|
46
|
-
for (const targetKey of Object.keys(targetStates)) {
|
|
47
|
-
if (targetKey !== KEYS.STATE_FINAL && !(targetKey in sourceStates)) {
|
|
48
|
-
deadendStates.push(targetKey);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return { unreachableStates, deadendStates }
|
|
53
|
-
}
|
|
54
|
-
}
|
package/src/getAllStateKeys.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This method returns a list of all states used in
|
|
3
|
-
* the specified state descriptor.
|
|
4
|
-
*
|
|
5
|
-
* @param {object} descriptor compiled state descriptor
|
|
6
|
-
* defining transitions between sub-states and sub-states
|
|
7
|
-
* @returns a sorted list of all states
|
|
8
|
-
*/
|
|
9
|
-
export default function getAllStateKeys(descriptor) {
|
|
10
|
-
const index = {};
|
|
11
|
-
const add = (key) => index[key] = (index[key] || 0) + 1;
|
|
12
|
-
visit(descriptor);
|
|
13
|
-
return Object.keys(index).sort();
|
|
14
|
-
function visit(descriptor) {
|
|
15
|
-
add(descriptor.key);
|
|
16
|
-
for (const [from, destinations] of Object.entries(descriptor.transitions)) {
|
|
17
|
-
add(from);
|
|
18
|
-
for (const targetKey of Object.values(destinations)) {
|
|
19
|
-
add(targetKey);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
for (const [stateKey, stateDescriptor] of Object.entries(descriptor.states)) {
|
|
23
|
-
add(stateKey);
|
|
24
|
-
visit(stateDescriptor);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
package/src/getAllTransitions.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import KEYS from "./KEYS.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns all possible transitions from the current state of the process.
|
|
5
|
-
* @param {Object} options - method parameters
|
|
6
|
-
* @param {Process} options.process - the current process for which
|
|
7
|
-
* all transitions should be returned
|
|
8
|
-
* @returns {Array<Object>} - an array of all transitions;
|
|
9
|
-
* each entry in this returned array contains the following keys:
|
|
10
|
-
* 1) parentStateKey - key of the state where the transition can be activated
|
|
11
|
-
* 2) sourceStateKey - the initial transition state
|
|
12
|
-
* 3) eventKey - key of the event activating transition
|
|
13
|
-
* 4) targetStateKey - the resulting state for this transition
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export default function getAllTransitions(process) {
|
|
17
|
-
const list = [];
|
|
18
|
-
let stateKey = process.current ? process.current.key : KEYS.STATE_INITIAL;
|
|
19
|
-
for (let i = process.stack.length - 1; i >= 0; i--) {
|
|
20
|
-
const parentState = process.stack[i];
|
|
21
|
-
const parentStateKey = parentState.key;
|
|
22
|
-
const descriptor = parentState.descriptor;
|
|
23
|
-
for (const sKey of [stateKey, KEYS.STATE_ANY]) {
|
|
24
|
-
const stateTransitions = descriptor.transitions[sKey];
|
|
25
|
-
if (!stateTransitions) continue;
|
|
26
|
-
for (const[eventKey, targetKey] of Object.entries(stateTransitions)) {
|
|
27
|
-
list.push([parentStateKey, sKey, eventKey, targetKey]);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
stateKey = parentStateKey;
|
|
31
|
-
}
|
|
32
|
-
return list;
|
|
33
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export default function getStateDescriptor(process, stateKey) {
|
|
2
|
-
let descriptor;
|
|
3
|
-
for (let i = process.stack.length - 1; !descriptor && i >= 0; i--) {
|
|
4
|
-
const states = process.stack[i].descriptor.states || {};
|
|
5
|
-
descriptor = states[stateKey];
|
|
6
|
-
}
|
|
7
|
-
return descriptor || { transitions: {}, states: {} };
|
|
8
|
-
}
|
package/src/getTargetStateKey.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import KEYS from "./KEYS.js";
|
|
2
|
-
|
|
3
|
-
export default function getTargetStateKey(descriptor, stateKey, eventKey) {
|
|
4
|
-
const pairs = [
|
|
5
|
-
[stateKey, eventKey],
|
|
6
|
-
[KEYS.STATE_ANY, eventKey],
|
|
7
|
-
[stateKey, KEYS.EVENT_ANY],
|
|
8
|
-
[KEYS.STATE_ANY, KEYS.EVENT_ANY]
|
|
9
|
-
];
|
|
10
|
-
let targetKey;
|
|
11
|
-
for (let i = 0, len = pairs.length; (targetKey === undefined) && i < len; i++) {
|
|
12
|
-
const [stateKey, eventKey] = pairs[i];
|
|
13
|
-
const stateTransitions = descriptor.transitions[stateKey];
|
|
14
|
-
if (!stateTransitions) continue;
|
|
15
|
-
targetKey = stateTransitions[eventKey];
|
|
16
|
-
if (targetKey === undefined) targetKey = stateTransitions[KEYS.EVENT_ANY];
|
|
17
|
-
}
|
|
18
|
-
return (targetKey !== undefined) ? targetKey : KEYS.STATE_FINAL;
|
|
19
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { default as buildStateDescriptor } from "./buildStateDescriptor.js";
|
|
2
|
-
export { default as checkProcessDescriptor } from "./checkProcessDescriptor.js";
|
|
3
|
-
export { default as getStateDescriptor } from "./getStateDescriptor.js";
|
|
4
|
-
export { default as getAllStateKeys } from "./getAllStateKeys.js";
|
|
5
|
-
export { default as getTargetStateKey } from "./getTargetStateKey.js";
|
|
6
|
-
export { default as KEYS } from "./KEYS.js";
|
|
7
|
-
export { default as newAsyncProcess } from "./newAsyncProcess.js";
|
|
8
|
-
export { default as getAllTransitions } from "./getAllTransitions.js";
|
|
9
|
-
export { default as newProcessInstance } from "./newProcessInstance.js";
|
|
10
|
-
export { default as newSyncProcess } from "./newSyncProcess.js";
|
package/src/newAsyncProcess.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { MODE, newAsyncTreeWalker } from "@statewalker/tree";
|
|
2
|
-
import newProcessInstance from "./newProcessInstance.js";
|
|
3
|
-
|
|
4
|
-
export default function newAsyncProcess({
|
|
5
|
-
config,
|
|
6
|
-
descriptor,
|
|
7
|
-
before, after,
|
|
8
|
-
newProcess,
|
|
9
|
-
newState,
|
|
10
|
-
handleError = console.error
|
|
11
|
-
}) {
|
|
12
|
-
const [process, loadNextState] = newProcessInstance({
|
|
13
|
-
config,
|
|
14
|
-
descriptor,
|
|
15
|
-
newProcess,
|
|
16
|
-
newState
|
|
17
|
-
});
|
|
18
|
-
const shift = newAsyncTreeWalker(before, after, process);
|
|
19
|
-
process.finished = false;
|
|
20
|
-
process.next = (event) => {
|
|
21
|
-
process.nextEvent = event;
|
|
22
|
-
return process.running = process.running || Promise
|
|
23
|
-
.resolve()
|
|
24
|
-
.then(async () => {
|
|
25
|
-
try {
|
|
26
|
-
process.event = process.nextEvent;
|
|
27
|
-
process.nextEvent = undefined;
|
|
28
|
-
while (!process.finished && process.event) {
|
|
29
|
-
process.finished = !await shift(loadNextState);
|
|
30
|
-
if ((process.status & MODE.EXIT) && process.nextEvent) {
|
|
31
|
-
// Consume the next event if it exists.
|
|
32
|
-
process.event = process.nextEvent;
|
|
33
|
-
process.nextEvent = undefined;
|
|
34
|
-
} else if (process.status & MODE.LEAF) {
|
|
35
|
-
// Returns control when the process is in a "leaf" state (a state without children)
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
} catch (error) {
|
|
40
|
-
handleError(error);
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
.finally(() => process.running = undefined)
|
|
44
|
-
.then(() => !process.finished);
|
|
45
|
-
}
|
|
46
|
-
return process;
|
|
47
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { MODE } from "@statewalker/tree";
|
|
2
|
-
import buildStateDescriptor from "./buildStateDescriptor.js";
|
|
3
|
-
import getTargetStateKey from "./getTargetStateKey.js";
|
|
4
|
-
import getStateDescriptor from "./getStateDescriptor.js";
|
|
5
|
-
import KEYS from "./KEYS.js";
|
|
6
|
-
|
|
7
|
-
export default function newProcessInstance({
|
|
8
|
-
config,
|
|
9
|
-
descriptor,
|
|
10
|
-
newState = (obj) => obj,
|
|
11
|
-
newProcess = (descriptor) => ({
|
|
12
|
-
descriptor,
|
|
13
|
-
status: 0,
|
|
14
|
-
stack: [],
|
|
15
|
-
event: undefined,
|
|
16
|
-
current: undefined
|
|
17
|
-
})
|
|
18
|
-
}) {
|
|
19
|
-
descriptor = descriptor || buildStateDescriptor(config);
|
|
20
|
-
const process = newProcess(descriptor);
|
|
21
|
-
const loadNextState = () => {
|
|
22
|
-
let key, descriptor;
|
|
23
|
-
if (!process.status) {
|
|
24
|
-
descriptor = process.descriptor;
|
|
25
|
-
key = descriptor.key;
|
|
26
|
-
} else {
|
|
27
|
-
if (!process.stack.length) {
|
|
28
|
-
key = KEYS.STATE_FINAL;
|
|
29
|
-
} else {
|
|
30
|
-
const eventKey = process.event.key || KEYS.EVENT_EMPTY;
|
|
31
|
-
const parentDescriptor = process.stack[process.stack.length - 1].descriptor;
|
|
32
|
-
const stateKey = process.status & MODE.ENTER
|
|
33
|
-
? KEYS.STATE_INITIAL
|
|
34
|
-
: process.current ? process.current.key : KEYS.STATE_FINAL;
|
|
35
|
-
key = getTargetStateKey(parentDescriptor, stateKey, eventKey);
|
|
36
|
-
if (key) {
|
|
37
|
-
descriptor = getStateDescriptor(process, key);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return key ? newState({ process, key, descriptor }) : null;
|
|
42
|
-
};
|
|
43
|
-
return [process, loadNextState];
|
|
44
|
-
}
|
package/src/newSyncProcess.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { MODE, newTreeWalker } from "@statewalker/tree";
|
|
2
|
-
import newProcessInstance from "./newProcessInstance.js";
|
|
3
|
-
|
|
4
|
-
export default function newSyncProcess({
|
|
5
|
-
config,
|
|
6
|
-
descriptor,
|
|
7
|
-
before, after,
|
|
8
|
-
newProcess,
|
|
9
|
-
newState,
|
|
10
|
-
}) {
|
|
11
|
-
const [process, loadNextState] = newProcessInstance({
|
|
12
|
-
config,
|
|
13
|
-
descriptor,
|
|
14
|
-
newProcess,
|
|
15
|
-
newState,
|
|
16
|
-
});
|
|
17
|
-
const shift = newTreeWalker(before, after, process);
|
|
18
|
-
process.dispatch = process.next = (event) => {
|
|
19
|
-
if (typeof event === 'string') event = { key : event };
|
|
20
|
-
process.event = event;
|
|
21
|
-
while (process.event) {
|
|
22
|
-
if (!shift(loadNextState)) return false;
|
|
23
|
-
if (process.status & MODE.LEAF) return true;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return process;
|
|
27
|
-
}
|