@statewalker/fsm 0.26.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 +3 -2
- package/dist/index.js +9 -7
- package/package.json +1 -1
- package/src/newFsmProcess.ts +23 -14
package/dist/index.d.ts
CHANGED
|
@@ -117,9 +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>[]>): [
|
|
121
122
|
dispatch: (event: string) => Promise<boolean>,
|
|
122
123
|
shutdown: () => Promise<void>
|
|
123
124
|
];
|
|
124
125
|
|
|
125
|
-
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 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 };
|
|
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
|
@@ -410,22 +410,24 @@ function getTransitionsFromDescriptor(descriptor, prevStateKey, index = {}) {
|
|
|
410
410
|
}
|
|
411
411
|
|
|
412
412
|
// src/newFsmProcess.ts
|
|
413
|
-
function newFsmProcess(config,
|
|
413
|
+
function newFsmProcess(context, config, load) {
|
|
414
414
|
let started = false;
|
|
415
415
|
let terminated = false;
|
|
416
416
|
const process = new FsmProcess(config);
|
|
417
417
|
process.onStateCreate((state) => {
|
|
418
418
|
started = true;
|
|
419
|
-
let cleanup;
|
|
420
419
|
state.onEnter(async () => {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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
|
+
}
|
|
425
427
|
});
|
|
426
428
|
});
|
|
427
429
|
async function dispatch(event) {
|
|
428
|
-
return terminated || event !== void 0 ? process.dispatch(event) : false;
|
|
430
|
+
return terminated || event !== void 0 && (!started || isStateTransitionEnabled(process, event)) ? process.dispatch(event) : false;
|
|
429
431
|
}
|
|
430
432
|
async function shutdown() {
|
|
431
433
|
await process.shutdown();
|
package/package.json
CHANGED
package/src/newFsmProcess.ts
CHANGED
|
@@ -2,16 +2,24 @@ import { FsmProcess } from "./FsmProcess.js";
|
|
|
2
2
|
import type { FsmStateConfig } from "./FsmStateConfig.js";
|
|
3
3
|
import { isStateTransitionEnabled } from "./utils/transitions.js";
|
|
4
4
|
|
|
5
|
-
export
|
|
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,
|
|
6
14
|
config: FsmStateConfig,
|
|
7
|
-
|
|
15
|
+
load: (
|
|
8
16
|
state: string,
|
|
9
17
|
event: undefined | string,
|
|
10
18
|
) =>
|
|
11
|
-
|
|
|
12
|
-
|
|
|
13
|
-
|
|
|
14
|
-
| Promise<
|
|
19
|
+
| undefined
|
|
20
|
+
| Module<C>
|
|
21
|
+
| Module<C>[]
|
|
22
|
+
| Promise<undefined | Module<C> | Module<C>[]>,
|
|
15
23
|
): [
|
|
16
24
|
dispatch: (event: string) => Promise<boolean>,
|
|
17
25
|
shutdown: () => Promise<void>,
|
|
@@ -21,19 +29,20 @@ export function newFsmProcess(
|
|
|
21
29
|
const process = new FsmProcess(config);
|
|
22
30
|
process.onStateCreate((state) => {
|
|
23
31
|
started = true;
|
|
24
|
-
let cleanup: Awaited<ReturnType<typeof handler>> | undefined;
|
|
25
32
|
state.onEnter(async () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
+
}
|
|
30
40
|
});
|
|
31
41
|
});
|
|
32
42
|
async function dispatch(event: string): Promise<boolean> {
|
|
33
43
|
return terminated ||
|
|
34
|
-
(event !== undefined
|
|
35
|
-
|
|
36
|
-
)
|
|
44
|
+
(event !== undefined &&
|
|
45
|
+
(!started || isStateTransitionEnabled(process, event)))
|
|
37
46
|
? process.dispatch(event)
|
|
38
47
|
: false;
|
|
39
48
|
}
|