@statewalker/fsm 0.20.0 → 0.20.2

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/dist/index.d.ts CHANGED
@@ -16,11 +16,13 @@ declare const STATE_INITIAL = "";
16
16
  declare const STATE_FINAL = "";
17
17
  declare const EVENT_ANY = "*";
18
18
  declare const EVENT_EMPTY = "";
19
+ type FsmStateKey = string;
20
+ type FsmEventKey = string;
19
21
  type FsmStateConfig = {
20
- key: string;
21
- transitions: [string, string, string][];
22
+ key: FsmStateKey;
23
+ transitions?: [from: FsmStateKey, event: FsmEventKey, to: FsmStateKey][];
22
24
  states?: FsmStateConfig[];
23
- };
25
+ } & Record<string, any>;
24
26
 
25
27
  declare class FsmStateDescriptor {
26
28
  transitions: Record<string, Record<string, string>>;
@@ -106,4 +108,4 @@ declare function getPrinter(state: FsmState): Printer;
106
108
  declare function setProcessTracer(process: FsmProcess, print?: Printer): () => void;
107
109
  declare function setStateTracer(state: FsmState, print?: Printer): void;
108
110
 
109
- export { EVENT_ANY, EVENT_EMPTY, FsmProcess, type FsmProcessDump, type FsmProcessDumpHandler, type FsmProcessHandler, FsmState, type FsmStateConfig, FsmStateDescriptor, type FsmStateDump, type FsmStateDumpHandler, type FsmStateErrorHandler, type FsmStateHandler, type FsmStateSyncHandler, KEY_HANDLERS, KEY_PRINTER, type Printer, type PrinterConfig, STATE_ANY, STATE_FINAL, STATE_INITIAL, STATUS_ENTER, STATUS_EXIT, STATUS_FINISHED, STATUS_FIRST, STATUS_LAST, STATUS_LEAF, STATUS_NEXT, STATUS_NONE, addSubstateHandlers, callStateHandlers, getPrinter, preparePrinter, setPrinter, setProcessPrinter, setProcessTracer, setStateTracer };
111
+ export { EVENT_ANY, EVENT_EMPTY, type FsmEventKey, FsmProcess, type FsmProcessDump, type FsmProcessDumpHandler, type FsmProcessHandler, FsmState, type FsmStateConfig, FsmStateDescriptor, type FsmStateDump, type FsmStateDumpHandler, type FsmStateErrorHandler, type FsmStateHandler, type FsmStateKey, type FsmStateSyncHandler, KEY_HANDLERS, KEY_PRINTER, type Printer, type PrinterConfig, STATE_ANY, STATE_FINAL, STATE_INITIAL, STATUS_ENTER, STATUS_EXIT, STATUS_FINISHED, STATUS_FIRST, STATUS_LAST, STATUS_LEAF, STATUS_NEXT, STATUS_NONE, addSubstateHandlers, callStateHandlers, getPrinter, preparePrinter, setPrinter, setProcessPrinter, setProcessTracer, setStateTracer };
package/dist/index.js CHANGED
@@ -30,8 +30,7 @@ var FsmBaseClass = class {
30
30
  }
31
31
  _removeHandler(type, handler) {
32
32
  let list = this.handlers[type];
33
- if (!list)
34
- return;
33
+ if (!list) return;
35
34
  list = list.filter((h) => h !== handler);
36
35
  if (list.length > 0) {
37
36
  this.handlers[type] = list;
@@ -123,7 +122,7 @@ var FsmStateDescriptor = class _FsmStateDescriptor {
123
122
  states = {};
124
123
  static build(config) {
125
124
  const descriptor = new _FsmStateDescriptor();
126
- for (const [from, event, to] of config.transitions) {
125
+ for (const [from, event, to] of config.transitions || []) {
127
126
  const index = descriptor.transitions[from] = descriptor.transitions[from] || {};
128
127
  index[event] = to;
129
128
  }
@@ -145,8 +144,7 @@ var FsmStateDescriptor = class _FsmStateDescriptor {
145
144
  for (let i = 0, len = pairs.length; targetKey === void 0 && i < len; i++) {
146
145
  const [stateKey2, eventKey2] = pairs[i];
147
146
  const stateTransitions = this.transitions[stateKey2];
148
- if (!stateTransitions)
149
- continue;
147
+ if (!stateTransitions) continue;
150
148
  targetKey = stateTransitions[eventKey2];
151
149
  }
152
150
  return targetKey !== void 0 ? targetKey : STATE_FINAL;
@@ -180,13 +178,11 @@ var FsmProcess = class extends FsmBaseClass {
180
178
  if (this.status & STATUS_EXIT) {
181
179
  await this.state?._runHandler("onExit", this.state);
182
180
  }
183
- if (!this._update())
184
- return false;
181
+ if (!this._update()) return false;
185
182
  if (this.status & STATUS_ENTER) {
186
183
  await this.state?._runHandler("onEnter", this.state);
187
184
  }
188
- if (this.status & mask)
189
- return true;
185
+ if (this.status & mask) return true;
190
186
  }
191
187
  }
192
188
  async dump(...args) {
@@ -199,8 +195,7 @@ var FsmProcess = class extends FsmBaseClass {
199
195
  return stateDump;
200
196
  };
201
197
  const dumpStates = async (state, stack = []) => {
202
- if (!state)
203
- return stack;
198
+ if (!state) return stack;
204
199
  state.parent && await dumpStates(state.parent, stack);
205
200
  stack.push(await dumpState(state));
206
201
  return stack;
@@ -244,14 +239,12 @@ var FsmProcess = class extends FsmBaseClass {
244
239
  return state;
245
240
  }
246
241
  _getSubstate(parent, prevStateKey) {
247
- if (!parent)
248
- return;
242
+ if (!parent) return;
249
243
  const toState = parent.descriptor?.getTargetStateKey(
250
244
  prevStateKey || STATE_INITIAL,
251
245
  this.event || EVENT_EMPTY
252
246
  ) || STATE_FINAL;
253
- if (!toState)
254
- return;
247
+ if (!toState) return;
255
248
  return this._newSubstate(parent, toState);
256
249
  }
257
250
  _newSubstate(parent, toState) {
@@ -262,8 +255,7 @@ var FsmProcess = class extends FsmBaseClass {
262
255
  return this._newState(parent, toState, descriptor);
263
256
  }
264
257
  _update() {
265
- if (this.status & STATUS_FINISHED)
266
- return false;
258
+ if (this.status & STATUS_FINISHED) return false;
267
259
  const nextState = this.status !== STATUS_NONE ? this.status & STATUS_ENTER ? this._getSubstate(this.state, STATE_INITIAL) : this._getSubstate(this.state?.parent, this.state?.key) : this._newState(void 0, this.config.key, this.rootDescriptor);
268
260
  if (nextState !== void 0) {
269
261
  this.state = nextState;
@@ -275,8 +267,7 @@ var FsmProcess = class extends FsmBaseClass {
275
267
  } else {
276
268
  this.status = STATUS_LEAF;
277
269
  }
278
- if (!this.state)
279
- this.status = STATUS_FINISHED;
270
+ if (!this.state) this.status = STATUS_FINISHED;
280
271
  }
281
272
  return !(this.status & STATUS_FINISHED);
282
273
  }
@@ -335,11 +326,12 @@ function setProcessTracer(process, print) {
335
326
  });
336
327
  }
337
328
  function setStateTracer(state, print) {
338
- const printLine = print || getPrinter(state);
339
329
  state.onEnter(() => {
330
+ const printLine = print || getPrinter(state);
340
331
  printLine(`<${state?.key} event="${state.process.event}">`);
341
332
  });
342
333
  state.onExit(() => {
334
+ const printLine = print || getPrinter(state);
343
335
  printLine(`</${state.key}> <!-- event="${state.process.event}" -->`);
344
336
  });
345
337
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/fsm",
3
- "version": "0.20.0",
3
+ "version": "0.20.2",
4
4
  "description": "HFSM Implementation",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/statewalker/statewalker-fsm",
@@ -24,10 +24,8 @@
24
24
  "import": "./src/index.ts"
25
25
  }
26
26
  },
27
- "dependencies": {
28
- "@statewalker/tree": "0.10.2"
29
- },
30
27
  "devDependencies": {
28
+ "@changesets/cli": "^2.27.7",
31
29
  "@statewalker/eslint-config": "*",
32
30
  "@statewalker/typescript-config": "*",
33
31
  "eslint": "^9.0.0",
@@ -40,7 +38,8 @@
40
38
  "url": "git@github.com:statewalker/statewalker-fsm.git"
41
39
  },
42
40
  "scripts": {
43
- "build": "yarn test && tsup",
41
+ "dist": "yarn test && tsup",
42
+ "build": "yarn dist",
44
43
  "watch": "tsup --watch",
45
44
  "clean": "rm -rf dist",
46
45
  "lint": "eslint \"**/*.(js|ts)\"",
@@ -4,10 +4,10 @@ export const STATE_FINAL = "";
4
4
  export const EVENT_ANY = "*";
5
5
  export const EVENT_EMPTY = "";
6
6
 
7
+ export type FsmStateKey = string;
8
+ export type FsmEventKey = string;
7
9
  export type FsmStateConfig = {
8
- key: string;
9
-
10
- transitions: [string, string, string][];
11
-
10
+ key: FsmStateKey;
11
+ transitions?: [from: FsmStateKey, event: FsmEventKey, to: FsmStateKey][];
12
12
  states?: FsmStateConfig[];
13
- };
13
+ } & Record<string, any>;
@@ -11,7 +11,7 @@ export class FsmStateDescriptor {
11
11
 
12
12
  static build(config: FsmStateConfig) {
13
13
  const descriptor = new FsmStateDescriptor();
14
- for (const [from, event, to] of config.transitions) {
14
+ for (const [from, event, to] of config.transitions || []) {
15
15
  const index = (descriptor.transitions[from] =
16
16
  descriptor.transitions[from] || {});
17
17
  index[event] = to;
@@ -9,11 +9,12 @@ export function setProcessTracer(process: FsmProcess, print?: Printer) {
9
9
  }
10
10
 
11
11
  export function setStateTracer(state: FsmState, print?: Printer) {
12
- const printLine = print || getPrinter(state);
13
12
  state.onEnter(() => {
13
+ const printLine = print || getPrinter(state);
14
14
  printLine(`<${state?.key} event="${state.process.event}">`);
15
15
  });
16
16
  state.onExit(() => {
17
+ const printLine = print || getPrinter(state);
17
18
  printLine(`</${state.key}> <!-- event="${state.process.event}" -->`);
18
19
  });
19
20
  }