@statewalker/fsm 0.36.0 → 0.37.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.
Files changed (41) hide show
  1. package/README.md +112 -0
  2. package/bin/cli.js +1 -1
  3. package/dist/bin/node-runner-UJp8T_oP.d.ts +16 -0
  4. package/dist/bin/node-runner-UJp8T_oP.d.ts.map +1 -0
  5. package/dist/bin/node-runner.js +40 -0
  6. package/dist/bin/node-runner.js.map +1 -0
  7. package/dist/index-vTjnkbGW.d.ts +129 -0
  8. package/dist/index-vTjnkbGW.d.ts.map +1 -0
  9. package/dist/index.js +38 -779
  10. package/dist/index.js.map +1 -1
  11. package/dist/launcher-6VUDviTj.d.ts +48 -0
  12. package/dist/launcher-6VUDviTj.d.ts.map +1 -0
  13. package/dist/launcher-CASrMAOa.js +443 -0
  14. package/dist/launcher-CASrMAOa.js.map +1 -0
  15. package/package.json +8 -8
  16. package/src/bin/node-runner.ts +57 -0
  17. package/src/core/fsm-base-class.ts +1 -26
  18. package/src/core/fsm-process.ts +14 -11
  19. package/src/core/fsm-state.ts +1 -21
  20. package/src/core/index.ts +1 -1
  21. package/src/index.ts +0 -3
  22. package/src/orchestrator/handler-registry.ts +108 -0
  23. package/src/orchestrator/index.ts +15 -9
  24. package/src/orchestrator/launcher.ts +42 -141
  25. package/src/orchestrator/start-process.ts +141 -26
  26. package/src/utils/index.ts +0 -2
  27. package/src/utils/printer.ts +5 -11
  28. package/src/utils/tracer.ts +1 -1
  29. package/CHANGELOG.md +0 -59
  30. package/dist/index.d.ts +0 -335
  31. package/src/core/new-fsm-process.ts +0 -83
  32. package/src/orchestrator/constants.ts +0 -130
  33. package/src/orchestrator/is-generator.ts +0 -12
  34. package/src/orchestrator/process-config-manager.ts +0 -70
  35. package/src/orchestrator/resolve-module-refs.ts +0 -52
  36. package/src/orchestrator/start-node-processes.ts +0 -19
  37. package/src/orchestrator/start-processes.ts +0 -32
  38. package/src/orchestrator/types.ts +0 -34
  39. package/src/utils/handlers.ts +0 -35
  40. package/src/utils/process.ts +0 -26
  41. package/src/utils/transitions.ts +0 -56
@@ -1,19 +0,0 @@
1
- import { resolveModulesPaths } from "./resolve-module-refs.ts";
2
- import { startProcesses } from "./start-processes.ts";
3
- export async function startNodeProcesses(modules?: string[]): Promise<void> {
4
- try {
5
- if (!modules) {
6
- modules = process.argv.slice(2);
7
- }
8
- const terminate = await startProcesses({
9
- modules: resolveModulesPaths(`file://${process.cwd()}/`, ...modules),
10
- onExit: () => process.exit(0),
11
- });
12
- process.on("SIGINT", terminate);
13
- process.on("SIGTERM", terminate);
14
- process.stdin.resume();
15
- } catch (err) {
16
- console.error("Fatal error:", err);
17
- process.exit(1);
18
- }
19
- }
@@ -1,32 +0,0 @@
1
- import { launcher } from "./launcher.ts";
2
-
3
- export async function startProcesses({
4
- onExit,
5
- modules,
6
- init,
7
- }: {
8
- onExit?: () => Promise<void> | void;
9
- modules: unknown[];
10
- init?: (
11
- context: Record<string, unknown>,
12
- ) => Promise<Record<string, unknown>> | Record<string, unknown>;
13
- }) {
14
- const modulesList: unknown[] = [];
15
- for (const module of modules) {
16
- modulesList.push(
17
- typeof module === "string" || module instanceof URL
18
- ? await import(String(module))
19
- : module,
20
- );
21
- }
22
- return await launcher({
23
- init,
24
- processes: [
25
- ...modulesList,
26
- // This function is called on exit at the end of the process chain
27
- function Exit() {
28
- return onExit;
29
- },
30
- ],
31
- });
32
- }
@@ -1,34 +0,0 @@
1
- export type StageHandler<C = Record<string, unknown>> = (
2
- context: C,
3
- ) =>
4
- | void
5
- | (() => void | Promise<void>)
6
- | Promise<void | (() => void | Promise<void>)>
7
- | AsyncGenerator<string, void, unknown>
8
- | Generator<string, void, unknown>;
9
-
10
- export function toStageHandlers<C>(
11
- value: unknown,
12
- accept: (key: string, value: unknown) => boolean,
13
- ): StageHandler<C>[] {
14
- return visit(value);
15
-
16
- function visit(val: unknown): StageHandler<C>[] {
17
- if (!val) {
18
- return [];
19
- }
20
- if (typeof val === "function") {
21
- return [val] as StageHandler<C>[];
22
- }
23
- if (typeof val !== "object") {
24
- return [];
25
- }
26
- if (Array.isArray(val)) {
27
- return val.flatMap(visit);
28
- }
29
- const modulesIndex = val as Record<string, unknown>;
30
- return Object.entries(modulesIndex).flatMap(([key, value]) => {
31
- return accept(key, value) ? visit(value) : [];
32
- });
33
- }
34
- }
@@ -1,35 +0,0 @@
1
- import type { FsmState, FsmStateHandler } from "../core/fsm-state.ts";
2
-
3
- export const KEY_HANDLERS = "handlers";
4
-
5
- export function callStateHandlers(state: FsmState) {
6
- const key = state.key;
7
- for (
8
- let parent: FsmState | undefined = state.parent;
9
- parent;
10
- parent = parent?.parent
11
- ) {
12
- const handlers =
13
- parent.getData<Record<string, FsmStateHandler[]>>(KEY_HANDLERS)?.[key];
14
- handlers?.forEach((handler) => {
15
- handler(state);
16
- });
17
- }
18
- }
19
-
20
- export function addSubstateHandlers(
21
- state: FsmState,
22
- handlers: Record<string, FsmStateHandler>,
23
- ) {
24
- const oldIndex =
25
- state.getData<Record<string, FsmStateHandler[]>>(KEY_HANDLERS);
26
- const index = oldIndex ? { ...oldIndex } : {};
27
- for (const [key, handler] of Object.entries(handlers)) {
28
- let list = index[key];
29
- if (!list) {
30
- list = index[key] = [];
31
- }
32
- list.push(handler);
33
- }
34
- state.setData(KEY_HANDLERS, index);
35
- }
@@ -1,26 +0,0 @@
1
- import { FsmProcess } from "../core/fsm-process.ts";
2
- import type { FsmStateConfig } from "../core/fsm-state-config.ts";
3
- import { setProcessPrinter } from "./printer.ts";
4
- import { setProcessTracer } from "./tracer.ts";
5
-
6
- export function newProcess(
7
- config: FsmStateConfig,
8
- {
9
- prefix,
10
- print,
11
- lineNumbers = true,
12
- }: {
13
- prefix?: string;
14
- print?: (...args: any[]) => void;
15
- lineNumbers?: boolean;
16
- },
17
- ): FsmProcess {
18
- const process = new FsmProcess(config);
19
- setProcessPrinter(process, {
20
- prefix,
21
- print,
22
- lineNumbers,
23
- });
24
- setProcessTracer(process);
25
- return process;
26
- }
@@ -1,56 +0,0 @@
1
- import type { FsmProcess } from "../core/fsm-process.ts";
2
- import type { FsmState } from "../core/fsm-state.ts";
3
- import type { FsmStateDescriptor } from "../core/fsm-state-descriptor.ts";
4
-
5
- export function isStateTransitionEnabled(process: FsmProcess, event: string) {
6
- const transitions = getStateTransitions(process.state);
7
- let active = false;
8
- for (const [from, ev, to] of transitions) {
9
- if (ev === event) {
10
- active = true;
11
- break;
12
- }
13
- }
14
- return active;
15
- }
16
-
17
- export function getStateTransitions(
18
- state?: FsmState,
19
- ): [from: string, event: string, to: string][] {
20
- const result: [from: string, event: string, to: string][] = [];
21
- const index = {};
22
- if (state) {
23
- let prevStateKey = state.key;
24
- for (let parent = state?.parent; parent; parent = parent.parent) {
25
- if (!parent.descriptor) continue;
26
- result.push(
27
- ...getTransitionsFromDescriptor(parent.descriptor, prevStateKey, index),
28
- );
29
- prevStateKey = parent.key;
30
- }
31
- }
32
- return result.reverse();
33
- }
34
-
35
- function getTransitionsFromDescriptor(
36
- descriptor: FsmStateDescriptor,
37
- prevStateKey: string,
38
- index: Record<string, boolean> = {},
39
- ) {
40
- const result: [from: string, event: string, to: string][] = [];
41
- const prevStateKeys = [prevStateKey, "*"];
42
- for (const prevKey of prevStateKeys) {
43
- const targets: undefined | Record<string, string> =
44
- descriptor.transitions[prevKey];
45
- if (targets) {
46
- for (const [event, target] of Object.entries(targets)) {
47
- if (index[event]) continue;
48
- if (target) {
49
- index[event] = true;
50
- }
51
- result.push([prevStateKey, event, target]);
52
- }
53
- }
54
- }
55
- return result;
56
- }