@statewalker/fsm 0.25.0 → 0.26.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 CHANGED
@@ -117,4 +117,9 @@ 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
- 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, newProcess, preparePrinter, setPrinter, setProcessPrinter, setProcessTracer, setStateTracer };
120
+ declare function newFsmProcess(config: FsmStateConfig, handler: (state: string, event: undefined | string) => void | Promise<void> | ((event: undefined | string) => void | Promise<void>) | Promise<(event: undefined | string) => void | Promise<void>>): [
121
+ dispatch: (event: string) => Promise<boolean>,
122
+ shutdown: () => Promise<void>
123
+ ];
124
+
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 };
package/dist/index.js CHANGED
@@ -408,6 +408,31 @@ function getTransitionsFromDescriptor(descriptor, prevStateKey, index = {}) {
408
408
  }
409
409
  return result;
410
410
  }
411
+
412
+ // src/newFsmProcess.ts
413
+ function newFsmProcess(config, handler) {
414
+ let started = false;
415
+ let terminated = false;
416
+ const process = new FsmProcess(config);
417
+ process.onStateCreate((state) => {
418
+ started = true;
419
+ let cleanup;
420
+ state.onEnter(async () => {
421
+ cleanup = await handler(state.key, process.event);
422
+ });
423
+ state.onExit(async () => {
424
+ cleanup?.(process.event);
425
+ });
426
+ });
427
+ async function dispatch(event) {
428
+ return terminated || event !== void 0 ? process.dispatch(event) : false;
429
+ }
430
+ async function shutdown() {
431
+ await process.shutdown();
432
+ terminated = true;
433
+ }
434
+ return [dispatch, shutdown];
435
+ }
411
436
  export {
412
437
  EVENT_ANY,
413
438
  EVENT_EMPTY,
@@ -430,6 +455,7 @@ export {
430
455
  getProcessPrinter,
431
456
  getStateTransitions,
432
457
  isStateTransitionEnabled,
458
+ newFsmProcess,
433
459
  newProcess,
434
460
  preparePrinter,
435
461
  setPrinter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/fsm",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "HFSM Implementation",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/statewalker/statewalker-fsm",
package/src/index.ts CHANGED
@@ -3,3 +3,4 @@ export * from "./FsmState.ts";
3
3
  export * from "./FsmStateConfig.ts";
4
4
  export * from "./FsmStateDescriptor.ts";
5
5
  export * from "./utils/index.ts";
6
+ export * from "./newFsmProcess.js";
@@ -0,0 +1,45 @@
1
+ import { FsmProcess } from "./FsmProcess.js";
2
+ import type { FsmStateConfig } from "./FsmStateConfig.js";
3
+ import { isStateTransitionEnabled } from "./utils/transitions.js";
4
+
5
+ export function newFsmProcess(
6
+ config: FsmStateConfig,
7
+ handler: (
8
+ state: string,
9
+ event: undefined | string,
10
+ ) =>
11
+ | void
12
+ | Promise<void>
13
+ | ((event: undefined | string) => void | Promise<void>)
14
+ | Promise<(event: undefined | string) => void | Promise<void>>,
15
+ ): [
16
+ dispatch: (event: string) => Promise<boolean>,
17
+ shutdown: () => Promise<void>,
18
+ ] {
19
+ let started = false;
20
+ let terminated = false;
21
+ const process = new FsmProcess(config);
22
+ process.onStateCreate((state) => {
23
+ started = true;
24
+ let cleanup: Awaited<ReturnType<typeof handler>> | undefined;
25
+ state.onEnter(async () => {
26
+ cleanup = await handler(state.key, process.event);
27
+ });
28
+ state.onExit(async () => {
29
+ cleanup?.(process.event);
30
+ });
31
+ });
32
+ async function dispatch(event: string): Promise<boolean> {
33
+ return terminated ||
34
+ (event !== undefined
35
+ // && (!started || isStateTransitionEnabled(process, event))
36
+ )
37
+ ? process.dispatch(event)
38
+ : false;
39
+ }
40
+ async function shutdown(): Promise<void> {
41
+ await process.shutdown();
42
+ terminated = true;
43
+ }
44
+ return [dispatch, shutdown] as const;
45
+ }