mfsm 2.0.0 → 3.0.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 +103 -6
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +160 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +113 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +36 -22
- package/CHANGELOG.md +0 -86
- package/CODE_OF_CONDUCT.md +0 -46
- package/src/index.js +0 -156
package/README.md
CHANGED
|
@@ -2,12 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
No really, this is probably not for you.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
```bash
|
|
8
|
+
npm install mfsm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Requirements:**
|
|
12
|
+
- Node.js 22 or higher
|
|
13
|
+
- ESM only (no CommonJS support)
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### TypeScript
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import fsm from 'mfsm'
|
|
21
|
+
import type { FSMDefinition, FSMInstance } from 'mfsm'
|
|
22
|
+
import client from 'something'
|
|
23
|
+
|
|
24
|
+
const clientFsm: FSMInstance = fsm({
|
|
11
25
|
api: {
|
|
12
26
|
connect: function () {
|
|
13
27
|
this.handle('connect')
|
|
@@ -63,7 +77,63 @@ clientFsm.connect()
|
|
|
63
77
|
.then(() => { console.log("connection established") })
|
|
64
78
|
```
|
|
65
79
|
|
|
66
|
-
###
|
|
80
|
+
### JavaScript (ESM)
|
|
81
|
+
|
|
82
|
+
```javascript
|
|
83
|
+
import fsm from 'mfsm'
|
|
84
|
+
|
|
85
|
+
const clientFsm = fsm({
|
|
86
|
+
api: {
|
|
87
|
+
connect() {
|
|
88
|
+
this.handle('connect')
|
|
89
|
+
return this.after('connected')
|
|
90
|
+
},
|
|
91
|
+
disconnect() {
|
|
92
|
+
this.handle('disconnect')
|
|
93
|
+
return this.after('disconnected')
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
init: {
|
|
97
|
+
url: process.env.HOST_URL,
|
|
98
|
+
client: client,
|
|
99
|
+
default: 'disconnected'
|
|
100
|
+
},
|
|
101
|
+
states: {
|
|
102
|
+
connected: {
|
|
103
|
+
disconnect() {
|
|
104
|
+
this.client.disconnect()
|
|
105
|
+
.then(() => {
|
|
106
|
+
this.handle('disconnect')
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
connecting: {
|
|
111
|
+
disconnect: { deferUntil: 'connected' }
|
|
112
|
+
},
|
|
113
|
+
disconnecting: {
|
|
114
|
+
connect: { deferUntil: 'disconnected' }
|
|
115
|
+
},
|
|
116
|
+
disconnected: {
|
|
117
|
+
onEntry: { emit: 'ready', wait: 50 },
|
|
118
|
+
connect() {
|
|
119
|
+
this.client.connect(this.url)
|
|
120
|
+
.then(() => {
|
|
121
|
+
this.next('connected')
|
|
122
|
+
})
|
|
123
|
+
this.next('connecting')
|
|
124
|
+
this.once('closed', () => {
|
|
125
|
+
this.next('disconnected')
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
clientFsm.connect()
|
|
133
|
+
.then(() => { console.log("connection established") })
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Declarative Handle Properties
|
|
67
137
|
|
|
68
138
|
* `emit` - emits an event from the FSM
|
|
69
139
|
* `next` - transitions FSM to a new state
|
|
@@ -71,3 +141,30 @@ clientFsm.connect()
|
|
|
71
141
|
* `deferUntil` - delays handling the event until after a specific state has occurred
|
|
72
142
|
* `forward` - forwards the event to a new state (after + next)
|
|
73
143
|
* `wait` - amount of time (in ms) to wait before emitting events or transitioning
|
|
144
|
+
|
|
145
|
+
## Event Patterns
|
|
146
|
+
|
|
147
|
+
The FSM uses [topic-dispatch](https://github.com/arobson/topic-dispatch) for event handling, which supports AMQP-style wildcard patterns:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// Listen to all events
|
|
151
|
+
machine.on('*', (event, topic) => {
|
|
152
|
+
console.log(`Event on ${topic}:`, event)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// Pattern matching (see topic-dispatch docs for more patterns)
|
|
156
|
+
machine.on('state.*', handler) // Match state.connected, state.idle, etc.
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Version 3.0.0 Changes
|
|
160
|
+
|
|
161
|
+
This is a major rewrite with breaking changes:
|
|
162
|
+
|
|
163
|
+
- **ESM-only**: No CommonJS support. Use ESM imports (`import`) only.
|
|
164
|
+
- **TypeScript**: Full TypeScript support with complete type definitions.
|
|
165
|
+
- **Node.js 22+**: Minimum Node.js version is now 22.
|
|
166
|
+
- **Modern dependencies**: Updated to latest versions with ESM and TypeScript support.
|
|
167
|
+
- `fauxdash` ^1.8.6 (ESM + TypeScript)
|
|
168
|
+
- `topic-dispatch` ^3.0.0 (ESM + TypeScript)
|
|
169
|
+
- `logging` ^4.2.0 (ESM)
|
|
170
|
+
- **Modern tooling**: Built with TypeScript, tested with Vitest.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EAKZ,MAAM,YAAY,CAAA;AAmLnB,MAAM,CAAC,OAAO,WAAW,UAAU,EAAE,aAAa,GAAG,WAAW,CAsB/D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import _ from 'fauxdash';
|
|
2
|
+
import Dispatch from 'topic-dispatch';
|
|
3
|
+
import logging from 'logging';
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
const createLogger = logging.default || logging;
|
|
6
|
+
const log = createLogger('fsm');
|
|
7
|
+
function after(topic) {
|
|
8
|
+
const { promise, resolve } = _.future();
|
|
9
|
+
this.once(topic, () => resolve());
|
|
10
|
+
return promise;
|
|
11
|
+
}
|
|
12
|
+
function cleanup() {
|
|
13
|
+
log.debug(`${this.getContext()} cleaning up all listeners'`);
|
|
14
|
+
this.removeAllListeners();
|
|
15
|
+
}
|
|
16
|
+
function deferredLog(machine, f, data) {
|
|
17
|
+
if (f.debug) {
|
|
18
|
+
log.debug(interpret(machine, f.debug, data));
|
|
19
|
+
}
|
|
20
|
+
else if (f.info) {
|
|
21
|
+
log.info(interpret(machine, f.info, data));
|
|
22
|
+
}
|
|
23
|
+
else if (f.warn) {
|
|
24
|
+
log.warn(interpret(machine, f.warn, data));
|
|
25
|
+
}
|
|
26
|
+
else if (f.error) {
|
|
27
|
+
log.error(interpret(machine, f.error, data));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function deferUntil(state, event, data) {
|
|
31
|
+
log.debug(`${this.getContext()} deferring handling of '${event}' until '${state}'`);
|
|
32
|
+
return function () {
|
|
33
|
+
this.once(state, () => {
|
|
34
|
+
log.debug(`${this.getContext()} handling deferred event, '${event}', on change to state '${state}'`);
|
|
35
|
+
this.handle(event, data);
|
|
36
|
+
});
|
|
37
|
+
}.bind(this);
|
|
38
|
+
}
|
|
39
|
+
function forward(state, event, data) {
|
|
40
|
+
process.nextTick(() => {
|
|
41
|
+
this.next(state, data);
|
|
42
|
+
});
|
|
43
|
+
return this.deferUntil(state, event, data);
|
|
44
|
+
}
|
|
45
|
+
function getContext() {
|
|
46
|
+
return `(${this.getIdentifier()}[${this.currentState}])`;
|
|
47
|
+
}
|
|
48
|
+
function getIdentifier() {
|
|
49
|
+
const id = this.id;
|
|
50
|
+
const name = this.name;
|
|
51
|
+
return id || name || 'anonymous';
|
|
52
|
+
}
|
|
53
|
+
function handle(eventName, event) {
|
|
54
|
+
const current = this.states[this.currentState];
|
|
55
|
+
const handler = current?.[eventName];
|
|
56
|
+
log.debug(`${this.getContext()} handling event, '${eventName}'`);
|
|
57
|
+
if (handler) {
|
|
58
|
+
try {
|
|
59
|
+
;
|
|
60
|
+
handler(event);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
log.error(`${this.getContext()} error occurred handling '${eventName}':\n${e}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
log.debug(`${this.getContext()} no handler for ${eventName}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function interpret(machine, logger, data) {
|
|
71
|
+
if (typeof logger === 'function') {
|
|
72
|
+
return logger.call(machine, data);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
return logger;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function next(state, data) {
|
|
79
|
+
log.debug(`${this.getContext()} transitioning to '${state}' from ${this.currentState}`);
|
|
80
|
+
this.previousState = this.currentState;
|
|
81
|
+
this.currentState = state;
|
|
82
|
+
const { promise, resolve, reject } = _.future();
|
|
83
|
+
process.nextTick(() => {
|
|
84
|
+
const s = this.states[state];
|
|
85
|
+
if (s?.onEntry) {
|
|
86
|
+
log.debug(`${this.getContext()} calling onEntry function`);
|
|
87
|
+
const result = s.onEntry(data);
|
|
88
|
+
resolve(result);
|
|
89
|
+
}
|
|
90
|
+
else if (!s) {
|
|
91
|
+
const err = `${this.getContext()} next was called for missing state '${state}'`;
|
|
92
|
+
log.error(err);
|
|
93
|
+
reject(new Error(err));
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
resolve();
|
|
97
|
+
}
|
|
98
|
+
this.emit(state, data);
|
|
99
|
+
});
|
|
100
|
+
return promise;
|
|
101
|
+
}
|
|
102
|
+
function isDeclarative(handler) {
|
|
103
|
+
return typeof handler !== 'function';
|
|
104
|
+
}
|
|
105
|
+
function buildFromDeclarative(machine, eventName, definition) {
|
|
106
|
+
return function (data) {
|
|
107
|
+
const relay = data ?? definition.data;
|
|
108
|
+
const nextState = definition.next ?? definition.forward;
|
|
109
|
+
const after = definition.deferUntil ?? definition.forward ?? definition.after;
|
|
110
|
+
deferredLog(machine, definition, relay);
|
|
111
|
+
if (after) {
|
|
112
|
+
machine.once(after, () => {
|
|
113
|
+
log.debug(`${this.getContext()} replaying event ${eventName} in ${after}`);
|
|
114
|
+
machine.handle(eventName, relay);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (definition.emit) {
|
|
118
|
+
setTimeout(() => {
|
|
119
|
+
machine.emit(definition.emit, relay);
|
|
120
|
+
}, definition.wait ?? 0);
|
|
121
|
+
}
|
|
122
|
+
if (nextState) {
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
log.debug(`${this.getContext()} transitioning to ${nextState} on event '${eventName}'`);
|
|
125
|
+
machine.next(nextState, relay);
|
|
126
|
+
}, definition.wait ?? 0);
|
|
127
|
+
}
|
|
128
|
+
}.bind(machine);
|
|
129
|
+
}
|
|
130
|
+
function bindHandles(machine) {
|
|
131
|
+
_.each(machine.states, (state, _name) => {
|
|
132
|
+
_.each(state, (definition, eventName) => {
|
|
133
|
+
if (!definition || typeof eventName !== 'string')
|
|
134
|
+
return;
|
|
135
|
+
if (isDeclarative(definition)) {
|
|
136
|
+
state[eventName] = buildFromDeclarative(machine, eventName, definition);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
state[eventName] = definition.bind(machine);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
export default function (definition) {
|
|
145
|
+
const baseline = {
|
|
146
|
+
after,
|
|
147
|
+
cleanup,
|
|
148
|
+
deferUntil,
|
|
149
|
+
forward,
|
|
150
|
+
getContext,
|
|
151
|
+
getIdentifier,
|
|
152
|
+
handle,
|
|
153
|
+
next
|
|
154
|
+
};
|
|
155
|
+
const machine = _.melter({}, baseline, _.clone(definition.api ?? {}), _.clone(definition.init), { states: _.clone(definition.states) }, Dispatch());
|
|
156
|
+
bindHandles(machine);
|
|
157
|
+
machine.next(machine.default);
|
|
158
|
+
return machine;
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,UAAU,CAAA;AACxB,OAAO,QAAQ,MAAM,gBAAgB,CAAA;AACrC,OAAO,OAAO,MAAM,SAAS,CAAA;AAU7B,8DAA8D;AAC9D,MAAM,YAAY,GAAI,OAAe,CAAC,OAAO,IAAI,OAAO,CAAA;AACxD,MAAM,GAAG,GAAG,YAAY,CAAC,KAAK,CAAC,CAAA;AAE/B,SAAS,KAAK,CAAoB,KAAa;IAC7C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC,MAAM,EAAE,CAAA;IACvC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAA;IACjC,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,SAAS,OAAO;IACd,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,UAAU,EAAE,6BAA6B,CAAC,CAAA;IAC5D,IAAI,CAAC,kBAAkB,EAAE,CAAA;AAC3B,CAAC;AAED,SAAS,WAAW,CAClB,OAAoB,EACpB,CAAqB,EACrB,IAAa;IAEb,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;QACZ,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAA;IAC9C,CAAC;SAAM,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;QAClB,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE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EAAE;YACjF,IAAI,CAAC,UAAU,IAAI,OAAO,SAAS,KAAK,QAAQ;gBAAE,OAAM;YACxD,IAAI,aAAa,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC9B,KAAK,CAAC,SAAS,CAAC,GAAG,oBAAoB,CAAC,OAAO,EAAE,SAAS,EAAE,UAAU,CAAC,CAAA;YACzE,CAAC;iBAAM,CAAC;gBACN,KAAK,CAAC,SAAS,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YAC7C,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,CAAC,OAAO,WAAW,UAAyB;IAChD,MAAM,QAAQ,GAAG;QACf,KAAK;QACL,OAAO;QACP,UAAU;QACV,OAAO;QACP,UAAU;QACV,aAAa;QACb,MAAM;QACN,IAAI;KACL,CAAA;IACD,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CACtB,EAAE,EACF,QAAQ,EACR,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,IAAI,EAAE,CAAC,EAC7B,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,EACxB,EAAE,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,EACtC,QAAQ,EAAE,CACI,CAAA;IAChB,WAAW,CAAC,OAAO,CAAC,CAAA;IACpB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,OAAiB,CAAC,CAAA;IACvC,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { EventEmitter } from 'events';
|
|
2
|
+
/**
|
|
3
|
+
* Declarative handler configuration for state events
|
|
4
|
+
*/
|
|
5
|
+
export interface DeclarativeHandler {
|
|
6
|
+
/** Next state to transition to */
|
|
7
|
+
next?: string;
|
|
8
|
+
/** State to defer this event until */
|
|
9
|
+
deferUntil?: string;
|
|
10
|
+
/** State to forward to (alias for deferUntil) */
|
|
11
|
+
forward?: string;
|
|
12
|
+
/** State to wait for before handling */
|
|
13
|
+
after?: string;
|
|
14
|
+
/** Event to emit */
|
|
15
|
+
emit?: string;
|
|
16
|
+
/** Delay in milliseconds before executing next/emit */
|
|
17
|
+
wait?: number;
|
|
18
|
+
/** Data to pass with the transition/emit */
|
|
19
|
+
data?: unknown;
|
|
20
|
+
/** Debug log message (string or function) */
|
|
21
|
+
debug?: string | ((data: unknown) => string);
|
|
22
|
+
/** Info log message (string or function) */
|
|
23
|
+
info?: string | ((data: unknown) => string);
|
|
24
|
+
/** Warn log message (string or function) */
|
|
25
|
+
warn?: string | ((data: unknown) => string);
|
|
26
|
+
/** Error log message (string or function) */
|
|
27
|
+
error?: string | ((data: unknown) => string);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Functional handler - bound to FSM instance
|
|
31
|
+
*/
|
|
32
|
+
export type EventHandler = (data?: unknown) => void | Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Handler can be either declarative config or functional
|
|
35
|
+
*/
|
|
36
|
+
export type StateHandler = DeclarativeHandler | EventHandler;
|
|
37
|
+
/**
|
|
38
|
+
* State configuration - maps event names to handlers
|
|
39
|
+
*/
|
|
40
|
+
export interface StateConfig {
|
|
41
|
+
/** Special handler called when entering this state */
|
|
42
|
+
onEntry?: StateHandler;
|
|
43
|
+
/** Event handlers for this state */
|
|
44
|
+
[eventName: string]: StateHandler | undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Collection of all states in the FSM
|
|
48
|
+
*/
|
|
49
|
+
export interface FSMStates {
|
|
50
|
+
[stateName: string]: StateConfig;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Custom API methods added to the FSM instance
|
|
54
|
+
*/
|
|
55
|
+
export interface FSMApi {
|
|
56
|
+
[methodName: string]: (this: FSMInstance, ...args: unknown[]) => unknown;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Initialization configuration
|
|
60
|
+
*/
|
|
61
|
+
export interface FSMInit {
|
|
62
|
+
/** Default/initial state name */
|
|
63
|
+
default: string;
|
|
64
|
+
/** Optional identifier for logging */
|
|
65
|
+
id?: string;
|
|
66
|
+
/** Optional name for logging */
|
|
67
|
+
name?: string;
|
|
68
|
+
/** Additional initialization properties */
|
|
69
|
+
[key: string]: unknown;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* FSM definition passed to the factory function
|
|
73
|
+
*/
|
|
74
|
+
export interface FSMDefinition {
|
|
75
|
+
/** State definitions */
|
|
76
|
+
states: FSMStates;
|
|
77
|
+
/** Initialization config */
|
|
78
|
+
init: FSMInit;
|
|
79
|
+
/** Custom API methods */
|
|
80
|
+
api?: FSMApi;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Base FSM instance methods
|
|
84
|
+
*/
|
|
85
|
+
export interface FSMBaseMethods {
|
|
86
|
+
/** Wait for a state to be entered, returns a promise */
|
|
87
|
+
after(topic: string): Promise<void>;
|
|
88
|
+
/** Clean up all event listeners */
|
|
89
|
+
cleanup(): void;
|
|
90
|
+
/** Defer handling an event until a specific state is reached */
|
|
91
|
+
deferUntil(state: string, event: string, data?: unknown): () => void;
|
|
92
|
+
/** Forward to a state and defer event handling */
|
|
93
|
+
forward(state: string, event: string, data?: unknown): () => void;
|
|
94
|
+
/** Get context string for logging */
|
|
95
|
+
getContext(): string;
|
|
96
|
+
/** Get identifier for this FSM instance */
|
|
97
|
+
getIdentifier(): string;
|
|
98
|
+
/** Handle an event in the current state */
|
|
99
|
+
handle(eventName: string, event?: unknown): void;
|
|
100
|
+
/** Transition to a new state */
|
|
101
|
+
next(state: string, data?: unknown): Promise<void>;
|
|
102
|
+
/** Current state name */
|
|
103
|
+
currentState: string;
|
|
104
|
+
/** Previous state name */
|
|
105
|
+
previousState?: string;
|
|
106
|
+
/** State definitions */
|
|
107
|
+
states: FSMStates;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* FSM instance combines base methods, custom API, init properties, and EventEmitter
|
|
111
|
+
*/
|
|
112
|
+
export type FSMInstance = FSMBaseMethods & EventEmitter & Record<string, unknown>;
|
|
113
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAE1C;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,kCAAkC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,sCAAsC;IACtC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,wCAAwC;IACxC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,oBAAoB;IACpB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,uDAAuD;IACvD,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,4CAA4C;IAC5C,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,6CAA6C;IAC7C,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,MAAM,CAAC,CAAA;IAC5C,4CAA4C;IAC5C,IAAI,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,MAAM,CAAC,CAAA;IAC3C,4CAA4C;IAC5C,IAAI,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,MAAM,CAAC,CAAA;IAC3C,6CAA6C;IAC7C,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,MAAM,CAAC,CAAA;CAC7C;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,CAAC,EAAE,OAAO,KA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AAG,MAAM,IAAI,CAAA;IAEpE,kDAAkD;IAClD,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,MAAM,IAAI,CAAA;IAEjE,qCAAqC;IACrC,UAAU,IAAI,MAAM,CAAA;IAEpB,2CAA2C;IAC3C,aAAa,IAAI,MAAM,CAAA;IAEvB,2CAA2C;IAC3C,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IAEhD,gCAAgC;IAChC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAElD,yBAAyB;IACzB,YAAY,EAAE,MAAM,CAAA;IAEpB,0BAA0B;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAA;IAEtB,wBAAwB;IACxB,MAAM,EAAE,SAAS,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,cAAc,GACtC,YAAY,GACZ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,40 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mfsm",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "my finite state machine. not yours. don't use it.",
|
|
5
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
6
17
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"test": "vitest run",
|
|
21
|
+
"test:watch": "vitest",
|
|
22
|
+
"test:coverage": "vitest run --coverage",
|
|
23
|
+
"lint": "eslint src tests",
|
|
24
|
+
"lint:fix": "eslint src tests --fix",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
9
26
|
},
|
|
10
27
|
"repository": {
|
|
11
28
|
"type": "git",
|
|
12
29
|
"url": "github.com/deftly/mfsm"
|
|
13
30
|
},
|
|
14
31
|
"keywords": [
|
|
15
|
-
"fsm"
|
|
32
|
+
"fsm",
|
|
33
|
+
"finite state machine",
|
|
34
|
+
"typescript"
|
|
16
35
|
],
|
|
17
36
|
"author": "Alex Robson",
|
|
18
37
|
"license": "MIT",
|
|
19
38
|
"dependencies": {
|
|
20
|
-
"fauxdash": "^1.
|
|
21
|
-
"logging": "^
|
|
22
|
-
"topic-dispatch": "^
|
|
39
|
+
"fauxdash": "^1.8.6",
|
|
40
|
+
"logging": "^4.2.0",
|
|
41
|
+
"topic-dispatch": "^3.0.0"
|
|
23
42
|
},
|
|
24
43
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"env": [
|
|
35
|
-
"mocha",
|
|
36
|
-
"_",
|
|
37
|
-
"should"
|
|
38
|
-
]
|
|
44
|
+
"@types/node": "^22.10.5",
|
|
45
|
+
"@typescript-eslint/eslint-plugin": "^8.20.0",
|
|
46
|
+
"@typescript-eslint/parser": "^8.20.0",
|
|
47
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
48
|
+
"@vitest/ui": "^4.0.18",
|
|
49
|
+
"eslint": "^9.18.0",
|
|
50
|
+
"globals": "^17.3.0",
|
|
51
|
+
"typescript": "^5.9.3",
|
|
52
|
+
"vitest": "^4.0.18"
|
|
39
53
|
}
|
|
40
54
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
-
|
|
5
|
-
## [1.6.0](///compare/v1.5.0...v1.6.0) (2021-04-06)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
### Features
|
|
9
|
-
|
|
10
|
-
* add forward as an API call ([8621479](///commit/86214793b77e6330368bb37774eed93a388b004b))
|
|
11
|
-
|
|
12
|
-
## [1.5.0](///compare/v1.4.0...v1.5.0) (2021-03-12)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
### Features
|
|
16
|
-
|
|
17
|
-
* improve declarative handling by removing mutual exclusions ([323c37f](///commit/323c37f98597ba289f0640bd8c73bc8151f9333c))
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
### Bug Fixes
|
|
21
|
-
|
|
22
|
-
* bump topic-dispatch dependency to 1.3.1 ([b9fe650](///commit/b9fe650fac3fb8bbf6ae254ecff6cbabd81bbfcd))
|
|
23
|
-
|
|
24
|
-
## [1.4.0](///compare/v1.3.3...v1.4.0) (2021-02-27)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
### Features
|
|
28
|
-
|
|
29
|
-
* improve declarative ability by adding forwarding and deferred logs ([fbe4137](///commit/fbe41370452b0db6601408704554d55cd1d17264))
|
|
30
|
-
|
|
31
|
-
### [1.3.3](///compare/v1.3.1...v1.3.3) (2021-02-24)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
### Bug Fixes
|
|
35
|
-
|
|
36
|
-
* add debug level logging to assist with troubleshooting state transitions ([eddc5e3](///commit/eddc5e34e59d6dc7302370820b29e92f3595b1d0))
|
|
37
|
-
* improve debug messages by including state. fix broken pass through of event between states ([ec876e4](///commit/ec876e4ec9267c55bc9f18bb38dfb1c0bd950dd7))
|
|
38
|
-
|
|
39
|
-
### [1.3.2](///compare/v1.3.1...v1.3.2) (2021-02-22)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
### Bug Fixes
|
|
43
|
-
|
|
44
|
-
* add debug level logging to assist with troubleshooting state transitions ([eddc5e3](///commit/eddc5e34e59d6dc7302370820b29e92f3595b1d0))
|
|
45
|
-
|
|
46
|
-
### [1.3.1](///compare/v1.3.0...v1.3.1) (2021-02-22)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
### Bug Fixes
|
|
50
|
-
|
|
51
|
-
* log error when next is called for a non-existent state ([48052c6](///commit/48052c6d6eb42a56b0432f3141e29050878336ec))
|
|
52
|
-
|
|
53
|
-
## [1.3.0](///compare/v1.2.0...v1.3.0) (2021-02-22)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
### Features
|
|
57
|
-
|
|
58
|
-
* support handling events by transitioning to a new state with declarative ([d1fe8b5](///commit/d1fe8b5edc2ada6a47771b39b26609bd25b7af00))
|
|
59
|
-
|
|
60
|
-
## [1.2.0](///compare/v1.1.2...v1.2.0) (2021-01-27)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
### Features
|
|
64
|
-
|
|
65
|
-
* update API to work with topic-dispatch 1.2.0 ([e7a5a6a](///commit/e7a5a6a8d226c082b06c5f7f82682c28e1e2bb2c))
|
|
66
|
-
|
|
67
|
-
### [1.1.2](///compare/v1.1.1...v1.1.2) (2021-01-24)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
### Bug Fixes
|
|
71
|
-
|
|
72
|
-
* bump topic dispatch to v 1.1.4 ([f626366](///commit/f626366aa67b6084f2ca8fce6ce18ad908160dcf))
|
|
73
|
-
|
|
74
|
-
### [1.1.1](///compare/v1.1.0...v1.1.1) (2021-01-24)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
### Bug Fixes
|
|
78
|
-
|
|
79
|
-
* bump topic dispatch to v 1.1.3 ([1a2bf5e](///commit/1a2bf5eed86a5a940c355f160d7088745d99926f))
|
|
80
|
-
|
|
81
|
-
## [1.1.0](///compare/v1.0.0...v1.1.0) (2021-01-22)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
### Features
|
|
85
|
-
|
|
86
|
-
* add entry handler and allow for dispatch handlers to be defined declaritively ([63f554f](///commit/63f554f4799fca89a3d3b635137d5a4dfceb2496))
|
package/CODE_OF_CONDUCT.md
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
|
2
|
-
|
|
3
|
-
## Our Pledge
|
|
4
|
-
|
|
5
|
-
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
-
|
|
7
|
-
## Our Standards
|
|
8
|
-
|
|
9
|
-
Examples of behavior that contributes to creating a positive environment include:
|
|
10
|
-
|
|
11
|
-
* Using welcoming and inclusive language
|
|
12
|
-
* Being respectful of differing viewpoints and experiences
|
|
13
|
-
* Gracefully accepting constructive criticism
|
|
14
|
-
* Focusing on what is best for the community
|
|
15
|
-
* Showing empathy towards other community members
|
|
16
|
-
|
|
17
|
-
Examples of unacceptable behavior by participants include:
|
|
18
|
-
|
|
19
|
-
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
|
20
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
21
|
-
* Public or private harassment
|
|
22
|
-
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
|
23
|
-
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
|
24
|
-
|
|
25
|
-
## Our Responsibilities
|
|
26
|
-
|
|
27
|
-
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
|
28
|
-
|
|
29
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
|
30
|
-
|
|
31
|
-
## Scope
|
|
32
|
-
|
|
33
|
-
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
|
34
|
-
|
|
35
|
-
## Enforcement
|
|
36
|
-
|
|
37
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at asrobson@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
|
38
|
-
|
|
39
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
|
40
|
-
|
|
41
|
-
## Attribution
|
|
42
|
-
|
|
43
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
|
44
|
-
|
|
45
|
-
[homepage]: http://contributor-covenant.org
|
|
46
|
-
[version]: http://contributor-covenant.org/version/1/4/
|
package/src/index.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
const _ = require('fauxdash')
|
|
2
|
-
const Dispatch = require('topic-dispatch')
|
|
3
|
-
const createLogger = require('logging').default
|
|
4
|
-
const log = createLogger('fsm')
|
|
5
|
-
|
|
6
|
-
function after (topic) {
|
|
7
|
-
var { promise, resolve } = _.future()
|
|
8
|
-
this.once(topic, () => resolve())
|
|
9
|
-
return promise
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function cleanup () {
|
|
13
|
-
log.debug(`${this.getContext()} cleaning up all listeners'`)
|
|
14
|
-
this.removeAllListeners()
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function deferredLog(machine, f, data) {
|
|
18
|
-
if (f.debug) {
|
|
19
|
-
log.debug(interpret(machine, f.debug, data))
|
|
20
|
-
} else if (f.info){
|
|
21
|
-
log.info(interpret(machine, f.info, data))
|
|
22
|
-
} else if (f.warn){
|
|
23
|
-
log.warn(interpret(machine, f.warn, data))
|
|
24
|
-
} else if (f.error){
|
|
25
|
-
log.error(interpret(machine, f.error, data))
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function deferUntil(state, event, data) {
|
|
30
|
-
log.debug(`${this.getContext()} deferring handling of '${event}' until '${state}'`)
|
|
31
|
-
return function () {
|
|
32
|
-
this.once(state, () => {
|
|
33
|
-
log.debug(`${this.getContext()} handling deferred event, '${event}', on change to state '${state}'`)
|
|
34
|
-
this.handle(event, data)
|
|
35
|
-
})
|
|
36
|
-
}.bind(this)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function forward(state, event, data) {
|
|
40
|
-
process.nextTick(() => {
|
|
41
|
-
this.next(state, data)
|
|
42
|
-
})
|
|
43
|
-
return this.deferUntil(state, event, data)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function getContext() {
|
|
47
|
-
return `(${this.getIdentifier()}[${this.currentState}])`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function getIdentifier() {
|
|
51
|
-
return this.id || this.name || 'anonymous'
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function handle (eventName, event) {
|
|
55
|
-
var current = this.states[this.currentState]
|
|
56
|
-
var handler = current[eventName]
|
|
57
|
-
log.debug(`${this.getContext()} handling event, '${eventName}'`)
|
|
58
|
-
if (handler) {
|
|
59
|
-
try {
|
|
60
|
-
handler(event)
|
|
61
|
-
} catch (e) {
|
|
62
|
-
log.error(`${this.getContext()} error occurred handling '${eventName}':\n${e}`)
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
log.debug(`${this.getContext()} no handler for ${eventName}`)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function interpret(machine, logger, data) {
|
|
70
|
-
if (typeof logger == 'function') {
|
|
71
|
-
return logger.call(machine, data)
|
|
72
|
-
} else {
|
|
73
|
-
return logger
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function next (state, data) {
|
|
78
|
-
log.debug(`${this.getContext()} transitioning to '${state}' from ${this.currentState}`)
|
|
79
|
-
this.previousState = this.currentState
|
|
80
|
-
this.currentState = state
|
|
81
|
-
const { promise, resolve, reject } = _.future()
|
|
82
|
-
process.nextTick(() => {
|
|
83
|
-
const s = this.states[state]
|
|
84
|
-
if (s && s.onEntry) {
|
|
85
|
-
log.debug(`${this.getContext()} calling onEntry function`)
|
|
86
|
-
resolve(s.onEntry(data))
|
|
87
|
-
} else if (!s) {
|
|
88
|
-
const err = `${this.getContext()} next was called for missing state '${state}'`
|
|
89
|
-
log.error(err)
|
|
90
|
-
reject(new Error(err))
|
|
91
|
-
}
|
|
92
|
-
this.emit(state, data)
|
|
93
|
-
})
|
|
94
|
-
return promise
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function buildFromDeclarative (machine, eventName, definition) {
|
|
98
|
-
return function (data) {
|
|
99
|
-
const relay = data || definition.data
|
|
100
|
-
const next = definition.next || definition.forward
|
|
101
|
-
const after = definition.deferUntil || definition.forward || definition.after
|
|
102
|
-
deferredLog(machine, definition, relay)
|
|
103
|
-
if (after) {
|
|
104
|
-
machine.once(after, () => {
|
|
105
|
-
log.debug(`${this.getContext()} replaying event ${eventName} in ${after}`)
|
|
106
|
-
machine.handle(eventName, relay)
|
|
107
|
-
})
|
|
108
|
-
}
|
|
109
|
-
if (definition.emit) {
|
|
110
|
-
setTimeout(() => {
|
|
111
|
-
machine.emit(definition.emit, relay)
|
|
112
|
-
}, definition.wait || 0)
|
|
113
|
-
}
|
|
114
|
-
if (next) {
|
|
115
|
-
setTimeout(() => {
|
|
116
|
-
log.debug(`${this.getContext()} transitioning to ${next} on event '${eventName}'`)
|
|
117
|
-
machine.next(next, relay)
|
|
118
|
-
}, definition.wait || 0)
|
|
119
|
-
}
|
|
120
|
-
}.bind(machine)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function bindHandles (machine) {
|
|
124
|
-
_.each(machine.states, (state, name) => {
|
|
125
|
-
_.each(state, (definition, eventName) => {
|
|
126
|
-
if (typeof definition === 'function') {
|
|
127
|
-
state[eventName] = definition.bind(machine)
|
|
128
|
-
} else {
|
|
129
|
-
state[eventName] = buildFromDeclarative(machine, eventName, definition)
|
|
130
|
-
}
|
|
131
|
-
})
|
|
132
|
-
})
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
module.exports = function (definition) {
|
|
136
|
-
const baseline = {
|
|
137
|
-
after,
|
|
138
|
-
cleanup,
|
|
139
|
-
deferUntil,
|
|
140
|
-
forward,
|
|
141
|
-
getContext,
|
|
142
|
-
getIdentifier,
|
|
143
|
-
handle,
|
|
144
|
-
next
|
|
145
|
-
}
|
|
146
|
-
var machine = _.melter({},
|
|
147
|
-
baseline,
|
|
148
|
-
_.clone(definition.api),
|
|
149
|
-
_.clone(definition.init),
|
|
150
|
-
{states: _.clone(definition.states)},
|
|
151
|
-
Dispatch()
|
|
152
|
-
)
|
|
153
|
-
bindHandles(machine)
|
|
154
|
-
machine.next(machine.default)
|
|
155
|
-
return machine
|
|
156
|
-
}
|