@real-router/fsm 0.2.3 → 0.2.4
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/package.json +4 -4
- package/src/fsm.ts +148 -0
- package/src/index.ts +3 -0
- package/src/types.ts +26 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/fsm",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "Universal synchronous FSM engine for Real-Router",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
"types": "./dist/esm/index.d.mts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"development": "./src/index.ts",
|
|
12
11
|
"types": {
|
|
13
12
|
"import": "./dist/esm/index.d.mts",
|
|
14
13
|
"require": "./dist/cjs/index.d.ts"
|
|
@@ -18,7 +17,8 @@
|
|
|
18
17
|
}
|
|
19
18
|
},
|
|
20
19
|
"files": [
|
|
21
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
22
|
],
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"build": "tsdown --config-loader unrun",
|
|
48
48
|
"type-check": "tsc --noEmit",
|
|
49
49
|
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0",
|
|
50
|
-
"lint:package": "
|
|
50
|
+
"lint:package": "publint",
|
|
51
51
|
"lint:types": "attw --pack .",
|
|
52
52
|
"bench": "NODE_OPTIONS='--expose-gc --max-old-space-size=4096' npx tsx tests/benchmarks/index.ts",
|
|
53
53
|
"build:dist-only": "tsdown --config-loader unrun"
|
package/src/fsm.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { FSMConfig, TransitionInfo, TransitionListener } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Synchronous finite state machine engine.
|
|
5
|
+
*
|
|
6
|
+
* Reentrancy: `send()` inside `onTransition` listener is allowed but unbounded —
|
|
7
|
+
* callers are responsible for preventing infinite loops.
|
|
8
|
+
*
|
|
9
|
+
* Exceptions: if a listener throws, the exception propagates to the caller.
|
|
10
|
+
* State is already updated before listeners fire, so `getState()` reflects the
|
|
11
|
+
* new state even if the exception escapes `send()`.
|
|
12
|
+
*/
|
|
13
|
+
export class FSM<
|
|
14
|
+
TStates extends string,
|
|
15
|
+
TEvents extends string,
|
|
16
|
+
TContext,
|
|
17
|
+
TPayloadMap extends Partial<Record<TEvents, unknown>> = Record<never, never>,
|
|
18
|
+
> {
|
|
19
|
+
#state: TStates;
|
|
20
|
+
#currentTransitions: Partial<Record<TEvents, TStates>>;
|
|
21
|
+
#listenerCount = 0;
|
|
22
|
+
#actions: Map<TStates, Map<TEvents, (payload: unknown) => void>> | null =
|
|
23
|
+
null;
|
|
24
|
+
readonly #context: TContext;
|
|
25
|
+
readonly #transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;
|
|
26
|
+
readonly #listeners: (TransitionListener<
|
|
27
|
+
TStates,
|
|
28
|
+
TEvents,
|
|
29
|
+
TPayloadMap
|
|
30
|
+
> | null)[] = [];
|
|
31
|
+
|
|
32
|
+
constructor(config: FSMConfig<TStates, TEvents, TContext>) {
|
|
33
|
+
this.#state = config.initial;
|
|
34
|
+
this.#context = config.context;
|
|
35
|
+
this.#transitions = config.transitions;
|
|
36
|
+
this.#currentTransitions = config.transitions[config.initial];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
send(event: TEvents, payload?: TPayloadMap[TEvents]): TStates {
|
|
40
|
+
const nextState = this.#currentTransitions[event];
|
|
41
|
+
|
|
42
|
+
if (nextState === undefined) {
|
|
43
|
+
return this.#state;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const from = this.#state;
|
|
47
|
+
|
|
48
|
+
this.#state = nextState;
|
|
49
|
+
this.#currentTransitions = this.#transitions[nextState];
|
|
50
|
+
|
|
51
|
+
if (this.#actions !== null) {
|
|
52
|
+
const action = this.#actions.get(from)?.get(event);
|
|
53
|
+
|
|
54
|
+
if (action !== undefined) {
|
|
55
|
+
action(payload);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (this.#listenerCount > 0) {
|
|
60
|
+
const info: TransitionInfo<TStates, TEvents, TPayloadMap> = {
|
|
61
|
+
from,
|
|
62
|
+
to: nextState,
|
|
63
|
+
event,
|
|
64
|
+
payload: payload,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
for (const listener of this.#listeners) {
|
|
68
|
+
if (listener !== null) {
|
|
69
|
+
listener(info);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return this.#state;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
canSend(event: TEvents): boolean {
|
|
78
|
+
return this.#currentTransitions[event] !== undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Directly sets FSM state without triggering actions or listeners.
|
|
83
|
+
* Use for hot-path optimizations where the caller handles side effects.
|
|
84
|
+
*/
|
|
85
|
+
forceState(state: TStates): void {
|
|
86
|
+
this.#state = state;
|
|
87
|
+
this.#currentTransitions = this.#transitions[state];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getState(): TStates {
|
|
91
|
+
return this.#state;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getContext(): TContext {
|
|
95
|
+
return this.#context;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
on<E extends TEvents>(
|
|
99
|
+
from: TStates,
|
|
100
|
+
event: E,
|
|
101
|
+
action: E extends keyof TPayloadMap
|
|
102
|
+
? (payload: TPayloadMap[E]) => void
|
|
103
|
+
: () => void,
|
|
104
|
+
): () => void {
|
|
105
|
+
this.#actions ??= new Map();
|
|
106
|
+
|
|
107
|
+
let stateActions = this.#actions.get(from);
|
|
108
|
+
|
|
109
|
+
if (!stateActions) {
|
|
110
|
+
stateActions = new Map();
|
|
111
|
+
this.#actions.set(from, stateActions);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
stateActions.set(event, action as (payload: unknown) => void);
|
|
115
|
+
|
|
116
|
+
return () => {
|
|
117
|
+
this.#actions?.get(from)?.delete(event);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
onTransition(
|
|
122
|
+
listener: (info: TransitionInfo<TStates, TEvents, TPayloadMap>) => void,
|
|
123
|
+
): () => void {
|
|
124
|
+
const nullIndex = this.#listeners.indexOf(null);
|
|
125
|
+
let index: number;
|
|
126
|
+
|
|
127
|
+
if (nullIndex === -1) {
|
|
128
|
+
index = this.#listeners.length;
|
|
129
|
+
this.#listeners.push(listener);
|
|
130
|
+
} else {
|
|
131
|
+
this.#listeners[nullIndex] = listener;
|
|
132
|
+
index = nullIndex;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
this.#listenerCount++;
|
|
136
|
+
let subscribed = true;
|
|
137
|
+
|
|
138
|
+
return () => {
|
|
139
|
+
if (!subscribed) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
subscribed = false;
|
|
144
|
+
this.#listeners[index] = null;
|
|
145
|
+
this.#listenerCount--;
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface FSMConfig<
|
|
2
|
+
TStates extends string,
|
|
3
|
+
TEvents extends string,
|
|
4
|
+
TContext,
|
|
5
|
+
> {
|
|
6
|
+
initial: TStates;
|
|
7
|
+
context: TContext;
|
|
8
|
+
transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface TransitionInfo<
|
|
12
|
+
TStates extends string,
|
|
13
|
+
TEvents extends string,
|
|
14
|
+
TPayloadMap extends Partial<Record<TEvents, unknown>>,
|
|
15
|
+
> {
|
|
16
|
+
from: TStates;
|
|
17
|
+
to: TStates;
|
|
18
|
+
event: TEvents;
|
|
19
|
+
payload: TPayloadMap[TEvents] | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type TransitionListener<
|
|
23
|
+
TStates extends string,
|
|
24
|
+
TEvents extends string,
|
|
25
|
+
TPayloadMap extends Partial<Record<TEvents, unknown>>,
|
|
26
|
+
> = (info: TransitionInfo<TStates, TEvents, TPayloadMap>) => void;
|