@statewalker/fsm 0.36.0 → 0.38.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.
@@ -1,48 +0,0 @@
1
- import type { FsmProcess } from "../core/fsm-process.ts";
2
- import type { FsmState } from "../core/fsm-state.ts";
3
- export type Printer = (...args: unknown[]) => void;
4
- export type PrinterConfig = {
5
- prefix?: string;
6
- print?: (...args: unknown[]) => void;
7
- lineNumbers?: boolean;
8
- };
9
-
10
- export const KEY_PRINTER = "printer";
11
-
12
- export function preparePrinter(
13
- process: FsmProcess,
14
- { prefix = "", print = console.log, lineNumbers = false }: PrinterConfig,
15
- ): Printer {
16
- let lineCounter = 0;
17
- const shift = () => {
18
- let prefix = "";
19
- for (let s = process.state?.parent; s; s = s.parent) {
20
- prefix += " ";
21
- }
22
- return prefix;
23
- };
24
- const getPrefix = lineNumbers ? () => `[${++lineCounter}]${shift()}` : shift;
25
- const printer = (...args: unknown[]) => print(prefix, getPrefix(), ...args);
26
- return printer;
27
- }
28
-
29
- export function setPrinter(state: FsmState, config: PrinterConfig = {}) {
30
- const printer = preparePrinter(state.process, config);
31
- state.setData(KEY_PRINTER, printer);
32
- }
33
-
34
- export function setProcessPrinter(
35
- process: FsmProcess,
36
- config: PrinterConfig = {},
37
- ) {
38
- const printer = preparePrinter(process, config);
39
- process.setData(KEY_PRINTER, printer);
40
- }
41
-
42
- export function getProcessPrinter(process: FsmProcess): Printer {
43
- return process.getData(KEY_PRINTER) || console.log;
44
- }
45
-
46
- export function getPrinter(state: FsmState): Printer {
47
- return state.getData(KEY_PRINTER, true) || getProcessPrinter(state.process);
48
- }
@@ -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
- }