@statewalker/fsm 0.25.0 → 0.27.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/dist/index.d.ts +7 -1
- package/dist/index.js +28 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/newFsmProcess.ts +54 -0
package/dist/index.d.ts
CHANGED
|
@@ -117,4 +117,10 @@ declare function setStateTracer(state: FsmState, print?: Printer): void;
|
|
|
117
117
|
declare function isStateTransitionEnabled(process: FsmProcess, event: string): boolean;
|
|
118
118
|
declare function getStateTransitions(state?: FsmState): [from: string, event: string, to: string][];
|
|
119
119
|
|
|
120
|
-
|
|
120
|
+
type Module<C = unknown> = (context: C) => void | (() => void | Promise<void>) | Promise<void | (() => void | Promise<void>)>;
|
|
121
|
+
declare function newFsmProcess<C>(context: C, config: FsmStateConfig, load: (state: string, event: undefined | string) => undefined | Module<C> | Module<C>[] | Promise<undefined | Module<C> | Module<C>[]>): [
|
|
122
|
+
dispatch: (event: string) => Promise<boolean>,
|
|
123
|
+
shutdown: () => Promise<void>
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
export { EVENT_ANY, EVENT_EMPTY, type FsmEventKey, FsmProcess, type FsmProcessDump, type FsmProcessDumpHandler, type FsmProcessHandler, FsmState, type FsmStateConfig, FsmStateDescriptor, type FsmStateDump, type FsmStateDumpHandler, type FsmStateErrorHandler, type FsmStateHandler, type FsmStateKey, KEY_PRINTER, type Module, type Printer, type PrinterConfig, STATE_ANY, STATE_FINAL, STATE_INITIAL, STATUS_ENTER, STATUS_EXIT, STATUS_FINISHED, STATUS_FIRST, STATUS_LAST, STATUS_LEAF, STATUS_NEXT, STATUS_NONE, getPrinter, getProcessPrinter, getStateTransitions, isStateTransitionEnabled, newFsmProcess, newProcess, preparePrinter, setPrinter, setProcessPrinter, setProcessTracer, setStateTracer };
|
package/dist/index.js
CHANGED
|
@@ -408,6 +408,33 @@ function getTransitionsFromDescriptor(descriptor, prevStateKey, index = {}) {
|
|
|
408
408
|
}
|
|
409
409
|
return result;
|
|
410
410
|
}
|
|
411
|
+
|
|
412
|
+
// src/newFsmProcess.ts
|
|
413
|
+
function newFsmProcess(context, config, load) {
|
|
414
|
+
let started = false;
|
|
415
|
+
let terminated = false;
|
|
416
|
+
const process = new FsmProcess(config);
|
|
417
|
+
process.onStateCreate((state) => {
|
|
418
|
+
started = true;
|
|
419
|
+
state.onEnter(async () => {
|
|
420
|
+
const modules = await load(state.key, process.event) ?? [];
|
|
421
|
+
for (const module of Array.isArray(modules) ? modules : [modules]) {
|
|
422
|
+
const handler = await module(context);
|
|
423
|
+
if (typeof handler === "function") {
|
|
424
|
+
state.onExit(handler);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
async function dispatch(event) {
|
|
430
|
+
return terminated || event !== void 0 && (!started || isStateTransitionEnabled(process, event)) ? process.dispatch(event) : false;
|
|
431
|
+
}
|
|
432
|
+
async function shutdown() {
|
|
433
|
+
await process.shutdown();
|
|
434
|
+
terminated = true;
|
|
435
|
+
}
|
|
436
|
+
return [dispatch, shutdown];
|
|
437
|
+
}
|
|
411
438
|
export {
|
|
412
439
|
EVENT_ANY,
|
|
413
440
|
EVENT_EMPTY,
|
|
@@ -430,6 +457,7 @@ export {
|
|
|
430
457
|
getProcessPrinter,
|
|
431
458
|
getStateTransitions,
|
|
432
459
|
isStateTransitionEnabled,
|
|
460
|
+
newFsmProcess,
|
|
433
461
|
newProcess,
|
|
434
462
|
preparePrinter,
|
|
435
463
|
setPrinter,
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { FsmProcess } from "./FsmProcess.js";
|
|
2
|
+
import type { FsmStateConfig } from "./FsmStateConfig.js";
|
|
3
|
+
import { isStateTransitionEnabled } from "./utils/transitions.js";
|
|
4
|
+
|
|
5
|
+
export type Module<C = unknown> = (
|
|
6
|
+
context: C,
|
|
7
|
+
) =>
|
|
8
|
+
| void
|
|
9
|
+
| (() => void | Promise<void>)
|
|
10
|
+
| Promise<void | (() => void | Promise<void>)>;
|
|
11
|
+
|
|
12
|
+
export function newFsmProcess<C>(
|
|
13
|
+
context: C,
|
|
14
|
+
config: FsmStateConfig,
|
|
15
|
+
load: (
|
|
16
|
+
state: string,
|
|
17
|
+
event: undefined | string,
|
|
18
|
+
) =>
|
|
19
|
+
| undefined
|
|
20
|
+
| Module<C>
|
|
21
|
+
| Module<C>[]
|
|
22
|
+
| Promise<undefined | Module<C> | Module<C>[]>,
|
|
23
|
+
): [
|
|
24
|
+
dispatch: (event: string) => Promise<boolean>,
|
|
25
|
+
shutdown: () => Promise<void>,
|
|
26
|
+
] {
|
|
27
|
+
let started = false;
|
|
28
|
+
let terminated = false;
|
|
29
|
+
const process = new FsmProcess(config);
|
|
30
|
+
process.onStateCreate((state) => {
|
|
31
|
+
started = true;
|
|
32
|
+
state.onEnter(async () => {
|
|
33
|
+
const modules = (await load(state.key, process.event)) ?? [];
|
|
34
|
+
for (const module of Array.isArray(modules) ? modules : [modules]) {
|
|
35
|
+
const handler = await module(context);
|
|
36
|
+
if (typeof handler === "function") {
|
|
37
|
+
state.onExit(handler);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
async function dispatch(event: string): Promise<boolean> {
|
|
43
|
+
return terminated ||
|
|
44
|
+
(event !== undefined &&
|
|
45
|
+
(!started || isStateTransitionEnabled(process, event)))
|
|
46
|
+
? process.dispatch(event)
|
|
47
|
+
: false;
|
|
48
|
+
}
|
|
49
|
+
async function shutdown(): Promise<void> {
|
|
50
|
+
await process.shutdown();
|
|
51
|
+
terminated = true;
|
|
52
|
+
}
|
|
53
|
+
return [dispatch, shutdown] as const;
|
|
54
|
+
}
|