@real-router/fsm 0.1.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/README.md +159 -0
- package/dist/cjs/index.d.ts +32 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/metafile-cjs.json +1 -0
- package/dist/esm/index.d.mts +32 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/metafile-esm.json +1 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# @real-router/fsm
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
5
|
+
|
|
6
|
+
Universal synchronous FSM engine for Real-Router. Zero dependencies, full TypeScript generics, O(1) transition lookup.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @real-router/fsm
|
|
12
|
+
# or
|
|
13
|
+
pnpm add @real-router/fsm
|
|
14
|
+
# or
|
|
15
|
+
yarn add @real-router/fsm
|
|
16
|
+
# or
|
|
17
|
+
bun add @real-router/fsm
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { FSM } from "@real-router/fsm";
|
|
24
|
+
|
|
25
|
+
const fsm = new FSM({
|
|
26
|
+
initial: "green",
|
|
27
|
+
context: { count: 0 },
|
|
28
|
+
transitions: {
|
|
29
|
+
green: { TIMER: "yellow" },
|
|
30
|
+
yellow: { TIMER: "red" },
|
|
31
|
+
red: { TIMER: "green", RESET: "green" },
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
fsm.send("TIMER"); // "yellow"
|
|
36
|
+
fsm.send("TIMER"); // "red"
|
|
37
|
+
fsm.send("RESET"); // "green"
|
|
38
|
+
fsm.getState(); // "green"
|
|
39
|
+
fsm.getContext(); // { count: 0 }
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### `new FSM(config: FSMConfig<TStates, TEvents, TContext>)`
|
|
47
|
+
|
|
48
|
+
Creates a new FSM instance.\
|
|
49
|
+
`config.initial: TStates` — initial state\
|
|
50
|
+
`config.context: TContext` — shared context object\
|
|
51
|
+
`config.transitions: Record<TStates, Partial<Record<TEvents, TStates>>>` — transition table
|
|
52
|
+
|
|
53
|
+
### `fsm.send(event, payload?): TStates`
|
|
54
|
+
|
|
55
|
+
Sends an event to trigger a transition. Returns the current state after processing.\
|
|
56
|
+
If no transition exists for the event in the current state, returns the current state (no-op).\
|
|
57
|
+
State is updated before listeners fire (reentrancy-safe).
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
fsm.send("TIMER"); // transitions and returns new state
|
|
61
|
+
fsm.send("RESET"); // no-op if no transition defined, returns current state
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### `fsm.getState(): TStates`
|
|
65
|
+
|
|
66
|
+
Returns the current state.
|
|
67
|
+
|
|
68
|
+
### `fsm.getContext(): TContext`
|
|
69
|
+
|
|
70
|
+
Returns the context object (same reference as provided in config).
|
|
71
|
+
|
|
72
|
+
### `fsm.onTransition(listener): () => void`
|
|
73
|
+
|
|
74
|
+
Subscribes a listener to state transitions. Returns an unsubscribe function.\
|
|
75
|
+
Listener receives `TransitionInfo` with `from`, `to`, `event`, and `payload` fields.\
|
|
76
|
+
Not called on no-op sends (when no transition exists).
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const unsub = fsm.onTransition((info) => {
|
|
80
|
+
console.log(`${info.from} -> ${info.to} via ${info.event}`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
fsm.send("TIMER"); // logs: "green -> yellow via TIMER"
|
|
84
|
+
unsub();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## Type-Safe Payloads
|
|
90
|
+
|
|
91
|
+
Use `TPayloadMap` to require payloads for specific events:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import type { FSMConfig } from "@real-router/fsm";
|
|
95
|
+
|
|
96
|
+
type State = "idle" | "loading" | "done";
|
|
97
|
+
type Event = "FETCH" | "RESOLVE";
|
|
98
|
+
|
|
99
|
+
interface PayloadMap {
|
|
100
|
+
FETCH: { url: string };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const config: FSMConfig<State, Event, null> = {
|
|
104
|
+
initial: "idle",
|
|
105
|
+
context: null,
|
|
106
|
+
transitions: {
|
|
107
|
+
idle: { FETCH: "loading" },
|
|
108
|
+
loading: { RESOLVE: "done" },
|
|
109
|
+
done: {},
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const fsm = new FSM<State, Event, null, PayloadMap>(config);
|
|
114
|
+
|
|
115
|
+
fsm.send("FETCH", { url: "/api" }); // OK — payload required
|
|
116
|
+
fsm.send("RESOLVE"); // OK — no payload needed
|
|
117
|
+
fsm.send("FETCH"); // TypeScript error — missing payload
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Types
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import type { FSMConfig, TransitionInfo } from "@real-router/fsm";
|
|
126
|
+
|
|
127
|
+
interface FSMConfig<TStates, TEvents, TContext> {
|
|
128
|
+
initial: TStates;
|
|
129
|
+
context: TContext;
|
|
130
|
+
transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
interface TransitionInfo<TStates, TEvents, TPayloadMap> {
|
|
134
|
+
from: TStates;
|
|
135
|
+
to: TStates;
|
|
136
|
+
event: TEvents;
|
|
137
|
+
payload: TPayloadMap[TEvents] | undefined;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Design
|
|
144
|
+
|
|
145
|
+
- **Synchronous** — no async, no promises, no microtasks
|
|
146
|
+
- **O(1) transitions** — cached current-state lookup, single property access per `send()`
|
|
147
|
+
- **Zero-alloc hot path** — when no listeners are registered, `send()` allocates nothing
|
|
148
|
+
- **Null-slot listener array** — `onTransition` reuses slots from unsubscribed listeners, preventing unbounded array growth
|
|
149
|
+
- **Reentrancy-safe** — `send()` inside a listener sees the updated state; callers are responsible for preventing infinite recursion
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Related Packages
|
|
154
|
+
|
|
155
|
+
- [@real-router/core](https://www.npmjs.com/package/@real-router/core) — Core router
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
MIT © [Oleg Ivanov](https://github.com/greydragon888)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface FSMConfig<TStates extends string, TEvents extends string, TContext> {
|
|
2
|
+
initial: TStates;
|
|
3
|
+
context: TContext;
|
|
4
|
+
transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;
|
|
5
|
+
}
|
|
6
|
+
interface TransitionInfo<TStates extends string, TEvents extends string, TPayloadMap extends Partial<Record<TEvents, unknown>>> {
|
|
7
|
+
from: TStates;
|
|
8
|
+
to: TStates;
|
|
9
|
+
event: TEvents;
|
|
10
|
+
payload: TPayloadMap[TEvents] | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Synchronous finite state machine engine.
|
|
15
|
+
*
|
|
16
|
+
* Reentrancy: `send()` inside `onTransition` listener is allowed but unbounded —
|
|
17
|
+
* callers are responsible for preventing infinite loops.
|
|
18
|
+
*
|
|
19
|
+
* Exceptions: if a listener throws, the exception propagates to the caller.
|
|
20
|
+
* State is already updated before listeners fire, so `getState()` reflects the
|
|
21
|
+
* new state even if the exception escapes `send()`.
|
|
22
|
+
*/
|
|
23
|
+
declare class FSM<TStates extends string, TEvents extends string, TContext, TPayloadMap extends Partial<Record<TEvents, unknown>> = Record<never, never>> {
|
|
24
|
+
#private;
|
|
25
|
+
constructor(config: FSMConfig<TStates, TEvents, TContext>);
|
|
26
|
+
send<E extends TEvents>(event: E, ...args: E extends keyof TPayloadMap ? [payload: TPayloadMap[E]] : [payload?: never]): TStates;
|
|
27
|
+
getState(): TStates;
|
|
28
|
+
getContext(): TContext;
|
|
29
|
+
onTransition(listener: (info: TransitionInfo<TStates, TEvents, TPayloadMap>) => void): () => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { FSM, type FSMConfig, type TransitionInfo };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports.FSM=class{#t;#s;#n=0;#i;#e;#r=[];constructor(t){this.#t=t.initial,this.#i=t.context,this.#e=t.transitions,this.#s=t.transitions[t.initial]}send(t,...s){const n=this.#s[t];if(void 0===n)return this.#t;const i=this.#t;if(this.#t=n,this.#s=this.#e[n],this.#n>0){const e={from:i,to:n,event:t,payload:s[0]};for(const t of this.#r)null!==t&&t(e)}return this.#t}getState(){return this.#t}getContext(){return this.#i}onTransition(t){const s=this.#r.indexOf(null);let n;-1===s?(n=this.#r.length,this.#r.push(t)):(this.#r[s]=t,n=s),this.#n++;let i=!0;return()=>{i&&(i=!1,this.#r[n]=null,this.#n--)}}};//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/fsm.ts"],"names":[],"mappings":";AAYO,IAAM,MAAN,MAKL;AAAA,EACA,MAAA;AAAA,EACA,mBAAA;AAAA,EACA,cAAA,GAAiB,CAAA;AAAA,EACR,QAAA;AAAA,EACA,YAAA;AAAA,EACA,aAIK,EAAC;AAAA,EAEf,YAAY,MAAA,EAA+C;AACzD,IAAA,IAAA,CAAK,SAAS,MAAA,CAAO,OAAA;AACrB,IAAA,IAAA,CAAK,WAAW,MAAA,CAAO,OAAA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AA,GAAQ,KAAK,UAAA,CAAW,MAAA;AACxB,MAAA,IAAA,CAAK,UAAA,CAAW,KAAK,QAAQ,CAAA;AAAA,IAC/B,CAAA,MAAO;AACL,MAAA,IAAA,CAAK,UAAA,CAAW,SAAS,CAAA,GAAI,QAAA;AAC7B,MAAA,KAAA,GAAQ,SAAA;AAAA,IACV;AAEA,IAAA,IAAA,CAAK,cAAA,EAAA;AACL,IAAA,IAAI,UAAA,GAAa,IAAA;AAEjB,IAAA,OAAO,MAAM;AACX,MAAA,IAAI,CAAC,UAAA,EAAY;AACf,QAAA;AAAA,MACF;AAEA,MAAA,UAAA,GAAa,KAAA;AACb,MAAA,IAAA,CAAK,UAAA,CAAW,KAAK,CAAA,GAAI,IAAA;AACzB,MAAA,IAAA,CAAK,cAAA,EAAA;AAAA,IACP,CAAA;AAAA,EACF;AACF","file":"index.js","sourcesContent":["import type { FSMConfig, TransitionInfo, TransitionListener } from \"./types\";\n\n/**\n * Synchronous finite state machine engine.\n *\n * Reentrancy: `send()` inside `onTransition` listener is allowed but unbounded —\n * callers are responsible for preventing infinite loops.\n *\n * Exceptions: if a listener throws, the exception propagates to the caller.\n * State is already updated before listeners fire, so `getState()` reflects the\n * new state even if the exception escapes `send()`.\n */\nexport class FSM<\n TStates extends string,\n TEvents extends string,\n TContext,\n TPayloadMap extends Partial<Record<TEvents, unknown>> = Record<never, never>,\n> {\n #state: TStates;\n #currentTransitions: Partial<Record<TEvents, TStates>>;\n #listenerCount = 0;\n readonly #context: TContext;\n readonly #transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;\n readonly #listeners: (TransitionListener<\n TStates,\n TEvents,\n TPayloadMap\n > | null)[] = [];\n\n constructor(config: FSMConfig<TStates, TEvents, TContext>) {\n this.#state = config.initial;\n this.#context = config.context;\n this.#transitions = config.transitions;\n this.#currentTransitions = config.transitions[config.initial];\n }\n\n send<E extends TEvents>(\n event: E,\n ...args: E extends keyof TPayloadMap\n ? [payload: TPayloadMap[E]]\n : [payload?: never]\n ): TStates {\n const nextState = this.#currentTransitions[event];\n\n if (nextState === undefined) {\n return this.#state;\n }\n\n const from = this.#state;\n\n this.#state = nextState;\n this.#currentTransitions = this.#transitions[nextState];\n\n if (this.#listenerCount > 0) {\n const info: TransitionInfo<TStates, TEvents, TPayloadMap> = {\n from,\n to: nextState,\n event,\n payload: args[0] as TPayloadMap[TEvents] | undefined,\n };\n\n for (const listener of this.#listeners) {\n if (listener !== null) {\n listener(info);\n }\n }\n }\n\n return this.#state;\n }\n\n getState(): TStates {\n return this.#state;\n }\n\n getContext(): TContext {\n return this.#context;\n }\n\n onTransition(\n listener: (info: TransitionInfo<TStates, TEvents, TPayloadMap>) => void,\n ): () => void {\n const nullIndex = this.#listeners.indexOf(null);\n let index: number;\n\n if (nullIndex === -1) {\n index = this.#listeners.length;\n this.#listeners.push(listener);\n } else {\n this.#listeners[nullIndex] = listener;\n index = nullIndex;\n }\n\n this.#listenerCount++;\n let subscribed = true;\n\n return () => {\n if (!subscribed) {\n return;\n }\n\n subscribed = false;\n this.#listeners[index] = null;\n this.#listenerCount--;\n };\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.6_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js":{"bytes":569,"imports":[],"format":"esm"},"src/fsm.ts":{"bytes":2731,"imports":[{"path":"/Users/olegivanov/WebstormProjects/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.6_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":88,"imports":[{"path":"src/fsm.ts","kind":"import-statement","original":"./fsm"},{"path":"/Users/olegivanov/WebstormProjects/real-router/node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.6_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js","kind":"import-statement","external":true}],"format":"esm"}},"outputs":{"dist/cjs/index.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":4022},"dist/cjs/index.js":{"imports":[],"exports":["FSM"],"entryPoint":"src/index.ts","inputs":{"src/fsm.ts":{"bytesInOutput":1500},"src/index.ts":{"bytesInOutput":0}},"bytes":1532}}}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface FSMConfig<TStates extends string, TEvents extends string, TContext> {
|
|
2
|
+
initial: TStates;
|
|
3
|
+
context: TContext;
|
|
4
|
+
transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;
|
|
5
|
+
}
|
|
6
|
+
interface TransitionInfo<TStates extends string, TEvents extends string, TPayloadMap extends Partial<Record<TEvents, unknown>>> {
|
|
7
|
+
from: TStates;
|
|
8
|
+
to: TStates;
|
|
9
|
+
event: TEvents;
|
|
10
|
+
payload: TPayloadMap[TEvents] | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Synchronous finite state machine engine.
|
|
15
|
+
*
|
|
16
|
+
* Reentrancy: `send()` inside `onTransition` listener is allowed but unbounded —
|
|
17
|
+
* callers are responsible for preventing infinite loops.
|
|
18
|
+
*
|
|
19
|
+
* Exceptions: if a listener throws, the exception propagates to the caller.
|
|
20
|
+
* State is already updated before listeners fire, so `getState()` reflects the
|
|
21
|
+
* new state even if the exception escapes `send()`.
|
|
22
|
+
*/
|
|
23
|
+
declare class FSM<TStates extends string, TEvents extends string, TContext, TPayloadMap extends Partial<Record<TEvents, unknown>> = Record<never, never>> {
|
|
24
|
+
#private;
|
|
25
|
+
constructor(config: FSMConfig<TStates, TEvents, TContext>);
|
|
26
|
+
send<E extends TEvents>(event: E, ...args: E extends keyof TPayloadMap ? [payload: TPayloadMap[E]] : [payload?: never]): TStates;
|
|
27
|
+
getState(): TStates;
|
|
28
|
+
getContext(): TContext;
|
|
29
|
+
onTransition(listener: (info: TransitionInfo<TStates, TEvents, TPayloadMap>) => void): () => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { FSM, type FSMConfig, type TransitionInfo };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t=class{#t;#s;#n=0;#i;#e;#r=[];constructor(t){this.#t=t.initial,this.#i=t.context,this.#e=t.transitions,this.#s=t.transitions[t.initial]}send(t,...s){const n=this.#s[t];if(void 0===n)return this.#t;const i=this.#t;if(this.#t=n,this.#s=this.#e[n],this.#n>0){const e={from:i,to:n,event:t,payload:s[0]};for(const t of this.#r)null!==t&&t(e)}return this.#t}getState(){return this.#t}getContext(){return this.#i}onTransition(t){const s=this.#r.indexOf(null);let n;-1===s?(n=this.#r.length,this.#r.push(t)):(this.#r[s]=t,n=s),this.#n++;let i=!0;return()=>{i&&(i=!1,this.#r[n]=null,this.#n--)}}};export{t as FSM};//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/fsm.ts"],"names":[],"mappings":";AAYO,IAAM,MAAN,MAKL;AAAA,EACA,MAAA;AAAA,EACA,mBAAA;AAAA,EACA,cAAA,GAAiB,CAAA;AAAA,EACR,QAAA;AAAA,EACA,YAAA;AAAA,EACA,aAIK,EAAC;AAAA,EAEf,YAAY,MAAA,EAA+C;AACzD,IAAA,IAAA,CAAK,SAAS,MAAA,CAAO,OAAA;AACrB,IAAA,IAAA,CAAK,WAAW,MAAA,CAAO,OAAA;AACvB,IAAA,IAAA,CAAK,eAAe,MAAA,CAAO,WAAA;AAC3B,IAAA,IAAA,CAAK,mBAAA,GAAsB,MAAA,CAAO,WAAA,CAAY,MAAA,CAAO,OAAO,CAAA;AAAA,EAC9D;AAAA,EAEA,IAAA,CACE,UACG,IAAA,EAGM;AACT,IAAA,MAAM,SAAA,GAAY,IAAA,CAAK,mBAAA,CAAoB,KAAK,CAAA;AAEhD,IAAA,IAAI,cAAc,MAAA,EAAW;AAC3B,MAAA,OAAO,IAAA,CAAK,MAAA;AAAA,IACd;AAEA,IAAA,MAAM,OAAO,I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F;AAEA,MAAA,UAAA,GAAa,KAAA;AACb,MAAA,IAAA,CAAK,UAAA,CAAW,KAAK,CAAA,GAAI,IAAA;AACzB,MAAA,IAAA,CAAK,cAAA,EAAA;AAAA,IACP,CAAA;AAAA,EACF;AACF","file":"index.mjs","sourcesContent":["import type { FSMConfig, TransitionInfo, TransitionListener } from \"./types\";\n\n/**\n * Synchronous finite state machine engine.\n *\n * Reentrancy: `send()` inside `onTransition` listener is allowed but unbounded —\n * callers are responsible for preventing infinite loops.\n *\n * Exceptions: if a listener throws, the exception propagates to the caller.\n * State is already updated before listeners fire, so `getState()` reflects the\n * new state even if the exception escapes `send()`.\n */\nexport class FSM<\n TStates extends string,\n TEvents extends string,\n TContext,\n TPayloadMap extends Partial<Record<TEvents, unknown>> = Record<never, never>,\n> {\n #state: TStates;\n #currentTransitions: Partial<Record<TEvents, TStates>>;\n #listenerCount = 0;\n readonly #context: TContext;\n readonly #transitions: Record<TStates, Partial<Record<TEvents, TStates>>>;\n readonly #listeners: (TransitionListener<\n TStates,\n TEvents,\n TPayloadMap\n > | null)[] = [];\n\n constructor(config: FSMConfig<TStates, TEvents, TContext>) {\n this.#state = config.initial;\n this.#context = config.context;\n this.#transitions = config.transitions;\n this.#currentTransitions = config.transitions[config.initial];\n }\n\n send<E extends TEvents>(\n event: E,\n ...args: E extends keyof TPayloadMap\n ? [payload: TPayloadMap[E]]\n : [payload?: never]\n ): TStates {\n const nextState = this.#currentTransitions[event];\n\n if (nextState === undefined) {\n return this.#state;\n }\n\n const from = this.#state;\n\n this.#state = nextState;\n this.#currentTransitions = this.#transitions[nextState];\n\n if (this.#listenerCount > 0) {\n const info: TransitionInfo<TStates, TEvents, TPayloadMap> = {\n from,\n to: nextState,\n event,\n payload: args[0] as TPayloadMap[TEvents] | undefined,\n };\n\n for (const listener of this.#listeners) {\n if (listener !== null) {\n listener(info);\n }\n }\n }\n\n return this.#state;\n }\n\n getState(): TStates {\n return this.#state;\n }\n\n getContext(): TContext {\n return this.#context;\n }\n\n onTransition(\n listener: (info: TransitionInfo<TStates, TEvents, TPayloadMap>) => void,\n ): () => void {\n const nullIndex = this.#listeners.indexOf(null);\n let index: number;\n\n if (nullIndex === -1) {\n index = this.#listeners.length;\n this.#listeners.push(listener);\n } else {\n this.#listeners[nullIndex] = listener;\n index = nullIndex;\n }\n\n this.#listenerCount++;\n let subscribed = true;\n\n return () => {\n if (!subscribed) {\n return;\n }\n\n subscribed = false;\n this.#listeners[index] = null;\n this.#listenerCount--;\n };\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"src/fsm.ts":{"bytes":2731,"imports":[],"format":"esm"},"src/index.ts":{"bytes":88,"imports":[{"path":"src/fsm.ts","kind":"import-statement","original":"./fsm"}],"format":"esm"}},"outputs":{"dist/esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":4022},"dist/esm/index.mjs":{"imports":[],"exports":["FSM"],"entryPoint":"src/index.ts","inputs":{"src/fsm.ts":{"bytesInOutput":1500},"src/index.ts":{"bytesInOutput":0}},"bytes":1532}}}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@real-router/fsm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"description": "Universal synchronous FSM engine for Real-Router",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.mjs",
|
|
8
|
+
"types": "./dist/esm/index.d.mts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": {
|
|
12
|
+
"import": "./dist/esm/index.d.mts",
|
|
13
|
+
"require": "./dist/cjs/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"import": "./dist/esm/index.mjs",
|
|
16
|
+
"require": "./dist/cjs/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/greydragon888/real-router.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"real-router",
|
|
28
|
+
"fsm",
|
|
29
|
+
"finite-state-machine",
|
|
30
|
+
"state-machine"
|
|
31
|
+
],
|
|
32
|
+
"author": {
|
|
33
|
+
"name": "Oleg Ivanov",
|
|
34
|
+
"email": "greydragon888@gmail.com",
|
|
35
|
+
"url": "https://github.com/greydragon888"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/greydragon888/real-router/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/greydragon888/real-router",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"test": "vitest",
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"type-check": "tsc --noEmit",
|
|
46
|
+
"lint": "eslint --cache --ext .ts src/ tests/ --fix --max-warnings 0",
|
|
47
|
+
"lint:package": "publint",
|
|
48
|
+
"lint:types": "attw --pack .",
|
|
49
|
+
"bench": "NODE_OPTIONS='--expose-gc --max-old-space-size=4096' npx tsx tests/benchmarks/index.ts"
|
|
50
|
+
},
|
|
51
|
+
"sideEffects": false,
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"mitata": "1.0.34"
|
|
54
|
+
}
|
|
55
|
+
}
|