@alwatr/fsm 3.1.1 → 3.2.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/CHANGELOG.md CHANGED
@@ -3,6 +3,24 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.2.0](https://github.com/Alwatr/flux/compare/v3.1.1...v3.2.0) (2024-10-11)
7
+
8
+ ### Bug Fixes
9
+
10
+ * prevent `sideeffects` from `build` result ([fbc7a9f](https://github.com/Alwatr/flux/commit/fbc7a9f21898e3a96f28ce4a105460af0cf513eb)) by @mohammadhonarvar
11
+
12
+ ### Code Refactoring
13
+
14
+ * update `import`s & packages based on the latest changes of `nanolib` ([b905288](https://github.com/Alwatr/flux/commit/b9052881b7549207c01b0eef92dc459d44b56ac0)) by @mohammadhonarvar
15
+
16
+ ### Dependencies update
17
+
18
+ * bump the alwatr-dependencies group across 1 directory with 7 updates ([daf1c3f](https://github.com/Alwatr/flux/commit/daf1c3f7ef8d17cf7388df2676b5fe808616ba57)) by @dependabot[bot]
19
+ * bump the alwatr-dependencies group with 4 updates ([7ce1b54](https://github.com/Alwatr/flux/commit/7ce1b54235cc2fd4f386052e7a4c4d324cc74888)) by @dependabot[bot]
20
+ * bump the alwatr-dependencies group with 8 updates ([bc520ba](https://github.com/Alwatr/flux/commit/bc520ba6ac7ed6bcff2c4a3eea81d1a2e502b0cf)) by @dependabot[bot]
21
+ * bump the development-dependencies group with 10 updates ([01de77c](https://github.com/Alwatr/flux/commit/01de77cd1d9fdfb6db06ebd5035c43e46cc8aa17)) by @dependabot[bot]
22
+ * update ([4dc21b2](https://github.com/Alwatr/flux/commit/4dc21b2bf01d7176aea6e0d81cdc3e1f77b97e0f)) by @mohammadhonarvar
23
+
6
24
  ## [3.1.1](https://github.com/Alwatr/flux/compare/v3.1.0...v3.1.1) (2024-09-29)
7
25
 
8
26
  ### Miscellaneous Chores
package/dist/base.d.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  import { AlwatrObservable, type AlwatrObservableConfig } from '@alwatr/observable';
2
- import '@alwatr/polyfill-has-own';
3
2
  import type { ActionRecord, StateEventDetail, StateRecord } from './type.js';
4
- import type { MaybePromise } from '@alwatr/type-helper';
5
3
  export interface AlwatrFluxStateMachineConfig<S extends string> extends AlwatrObservableConfig {
6
4
  initialState: S;
7
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAE,KAAK,sBAAsB,EAAC,MAAM,oBAAoB,CAAC;AACjF,OAAO,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAa,YAAY,EAAE,gBAAgB,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEvF,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAItD,MAAM,WAAW,4BAA4B,CAAC,CAAC,SAAS,MAAM,CAAE,SAAQ,sBAAsB;IAC5F,YAAY,EAAE,CAAC,CAAC;CACjB;AAED;;GAEG;AACH,8BAAsB,0BAA0B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAE,SAAQ,gBAAgB,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAC,CAAC;IACvH;;OAEG;IACH,SAAS,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAM;IAE/C;;OAEG;IACH,SAAS,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAM;IAEjD,SAAS,CAAC,aAAa,EAAE,CAAC,CAAC;IAE3B,UAAmB,QAAQ,EAAE;QAAC,KAAK,EAAE,CAAC,CAAA;KAAC,CAAC;gBAE5B,MAAM,EAAE,4BAA4B,CAAC,CAAC,CAAC;IAMnD;;OAEG;IACH,SAAS,CAAC,oBAAoB,IAAI,IAAI;IAKt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AAC,GAAG,YAAY,CAAC,OAAO,CAAC;IAKxF;;OAEG;cACa,WAAW,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBpD;;OAEG;YACW,gBAAgB;IAqB9B;;OAEG;IACH,OAAO,CAAC,YAAY;CAOrB"}
package/dist/main.cjs CHANGED
@@ -1,3 +1,137 @@
1
- /* @alwatr/fsm v3.1.1 */
2
- "use strict";var a=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var m=(i,o)=>{for(var t in o)a(i,t,{get:o[t],enumerable:!0})},g=(i,o,t,r)=>{if(o&&typeof o=="object"||typeof o=="function")for(let e of h(o))!S.call(i,e)&&e!==t&&a(i,e,{get:()=>o[e],enumerable:!(r=l(o,e))||r.enumerable});return i};var p=i=>g(a({},"__esModule",{value:!0}),i);var x={};m(x,{AlwatrFluxStateMachine:()=>n,AlwatrFluxStateMachineBase:()=>s});module.exports=p(x);var c=require("@alwatr/logger"),d=require("@alwatr/observable"),A=require("@alwatr/polyfill-has-own");(0,c.definePackage)("@alwatr/fsm","3.1.1");var s=class extends d.AlwatrObservable{constructor(t){t.loggerPrefix??(t.loggerPrefix="flux-state-machine");super(t);this.stateRecord_={};this.actionRecord_={};this.message_={state:this.initialState_=t.initialState}}resetToInitialState_(){this.logger_.logMethod?.("resetToInitialState_"),this.message_={state:this.initialState_}}shouldTransition_(t){return this.logger_.logMethodFull?.("shouldTransition_",t,!0),!0}async transition_(t){let r=this.message_.state,e=this.stateRecord_[r]?.[t]??this.stateRecord_._all?.[t];if(this.logger_.logMethodArgs?.("transition_",{fromState:r,event:t,toState:e}),e==null){this.logger_.incident?.("transition","invalid_target_state",{fromState:r,event:t});return}let _={from:r,event:t,to:e};await this.shouldTransition_(_)===!0&&(this.notify_({state:e}),this.postTransition__(_))}async postTransition__(t){this.logger_.logMethodArgs?.("_transitioned",t),await this.execAction__(`on_${t.event}`,t),t.from!==t.to&&(await this.execAction__("on_state_exit",t),await this.execAction__(`on_${t.from}_exit`,t),await this.execAction__("on_state_enter",t),await this.execAction__(`on_${t.to}_enter`,t)),Object.hasOwn(this,`on_${t.from}_${t.event}`)?this.execAction__(`on_${t.from}_${t.event}`,t):this.execAction__(`on_all_${t.event}`,t)}execAction__(t,r){let e=this.actionRecord_[t];if(typeof e=="function")return this.logger_.logMethodArgs?.("_$execAction",t),e.call(this,r)}};var n=class extends s{get state(){return this.message_.state}transition(o){this.transition_(o)}resetToInitialState(){this.resetToInitialState_()}};0&&(module.exports={AlwatrFluxStateMachine,AlwatrFluxStateMachineBase});
1
+ /* @alwatr/fsm v3.2.0 */
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/main.ts
22
+ var main_exports = {};
23
+ __export(main_exports, {
24
+ AlwatrFluxStateMachine: () => AlwatrFluxStateMachine,
25
+ AlwatrFluxStateMachineBase: () => AlwatrFluxStateMachineBase
26
+ });
27
+ module.exports = __toCommonJS(main_exports);
28
+
29
+ // src/base.ts
30
+ var import_nanolib = require("@alwatr/nanolib");
31
+ var import_observable = require("@alwatr/observable");
32
+ __dev_mode__: import_nanolib.packageTracer.add("@alwatr/fsm", "3.2.0");
33
+ var AlwatrFluxStateMachineBase = class extends import_observable.AlwatrObservable {
34
+ constructor(config) {
35
+ config.loggerPrefix ?? (config.loggerPrefix = "flux-state-machine");
36
+ super(config);
37
+ /**
38
+ * States and transitions config.
39
+ */
40
+ this.stateRecord_ = {};
41
+ /**
42
+ * Bind actions name to class methods
43
+ */
44
+ this.actionRecord_ = {};
45
+ this.message_ = { state: this.initialState_ = config.initialState };
46
+ }
47
+ /**
48
+ * Reset machine to initial state without notify.
49
+ */
50
+ resetToInitialState_() {
51
+ this.logger_.logMethod?.("resetToInitialState_");
52
+ this.message_ = { state: this.initialState_ };
53
+ }
54
+ /**
55
+ * Transition condition.
56
+ */
57
+ shouldTransition_(_eventDetail) {
58
+ this.logger_.logMethodFull?.("shouldTransition_", _eventDetail, true);
59
+ return true;
60
+ }
61
+ /**
62
+ * Transition flux state machine instance to new state.
63
+ */
64
+ async transition_(event) {
65
+ const fromState = this.message_.state;
66
+ const toState = this.stateRecord_[fromState]?.[event] ?? this.stateRecord_._all?.[event];
67
+ this.logger_.logMethodArgs?.("transition_", { fromState, event, toState });
68
+ if (toState == null) {
69
+ this.logger_.incident?.("transition", "invalid_target_state", {
70
+ fromState,
71
+ event
72
+ });
73
+ return;
74
+ }
75
+ const eventDetail = { from: fromState, event, to: toState };
76
+ if (await this.shouldTransition_(eventDetail) !== true) return;
77
+ this.notify_({ state: toState });
78
+ this.postTransition__(eventDetail);
79
+ }
80
+ /**
81
+ * Execute all actions for current state.
82
+ */
83
+ async postTransition__(eventDetail) {
84
+ this.logger_.logMethodArgs?.("_transitioned", eventDetail);
85
+ await this.execAction__(`on_${eventDetail.event}`, eventDetail);
86
+ if (eventDetail.from !== eventDetail.to) {
87
+ await this.execAction__(`on_state_exit`, eventDetail);
88
+ await this.execAction__(`on_${eventDetail.from}_exit`, eventDetail);
89
+ await this.execAction__(`on_state_enter`, eventDetail);
90
+ await this.execAction__(`on_${eventDetail.to}_enter`, eventDetail);
91
+ }
92
+ if (Object.hasOwn(this, `on_${eventDetail.from}_${eventDetail.event}`)) {
93
+ this.execAction__(`on_${eventDetail.from}_${eventDetail.event}`, eventDetail);
94
+ } else {
95
+ this.execAction__(`on_all_${eventDetail.event}`, eventDetail);
96
+ }
97
+ }
98
+ /**
99
+ * Execute action name if defined in _actionRecord.
100
+ */
101
+ execAction__(name, eventDetail) {
102
+ const actionFn = this.actionRecord_[name];
103
+ if (typeof actionFn === "function") {
104
+ this.logger_.logMethodArgs?.("_$execAction", name);
105
+ return actionFn.call(this, eventDetail);
106
+ }
107
+ }
108
+ };
109
+
110
+ // src/fsm.ts
111
+ var AlwatrFluxStateMachine = class extends AlwatrFluxStateMachineBase {
112
+ /**
113
+ * Current state.
114
+ */
115
+ get state() {
116
+ return this.message_.state;
117
+ }
118
+ /**
119
+ * Transition flux state machine instance to new state.
120
+ */
121
+ transition(event) {
122
+ this.transition_(event);
123
+ }
124
+ /**
125
+ * Reset machine to initial state without notify.
126
+ */
127
+ resetToInitialState() {
128
+ this.resetToInitialState_();
129
+ }
130
+ };
131
+ // Annotate the CommonJS export names for ESM import in node:
132
+ 0 && (module.exports = {
133
+ AlwatrFluxStateMachine,
134
+ AlwatrFluxStateMachineBase
135
+ });
136
+ /*! For license information please see main.cjs.LEGAL.txt */
3
137
  //# sourceMappingURL=main.cjs.map
package/dist/main.cjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/main.ts", "../src/base.ts", "../src/fsm.ts"],
4
- "sourcesContent": ["export * from './base.js';\nexport * from './fsm.js';\nexport * from './type.js';\n", "import {definePackage} from '@alwatr/logger';\nimport {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\nimport '@alwatr/polyfill-has-own';\n\nimport type {ActionName, ActionRecord, StateEventDetail, StateRecord} from './type.js';\nimport type {} from '@alwatr/nano-build';\nimport type {MaybePromise} from '@alwatr/type-helper';\n\ndefinePackage('@alwatr/fsm', __package_version__);\n\nexport interface AlwatrFluxStateMachineConfig<S extends string> extends AlwatrObservableConfig {\n initialState: S;\n}\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachineBase<S extends string, E extends string> extends AlwatrObservable<{state: S}> {\n /**\n * States and transitions config.\n */\n protected stateRecord_: StateRecord<S, E> = {};\n\n /**\n * Bind actions name to class methods\n */\n protected actionRecord_: ActionRecord<S, E> = {};\n\n protected initialState_: S;\n\n protected override message_: {state: S};\n\n constructor(config: AlwatrFluxStateMachineConfig<S>) {\n config.loggerPrefix ??= 'flux-state-machine';\n super(config);\n this.message_ = {state: this.initialState_ = config.initialState};\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n protected resetToInitialState_(): void {\n this.logger_.logMethod?.('resetToInitialState_');\n this.message_ = {state: this.initialState_};\n }\n\n /**\n * Transition condition.\n */\n protected shouldTransition_(_eventDetail: StateEventDetail<S, E>): MaybePromise<boolean> {\n this.logger_.logMethodFull?.('shouldTransition_', _eventDetail, true);\n return true;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n protected async transition_(event: E): Promise<void> {\n const fromState = this.message_.state;\n const toState = this.stateRecord_[fromState]?.[event] ?? this.stateRecord_._all?.[event];\n\n this.logger_.logMethodArgs?.('transition_', {fromState, event, toState});\n\n if (toState == null) {\n this.logger_.incident?.('transition', 'invalid_target_state', {\n fromState,\n event,\n });\n return;\n }\n\n const eventDetail: StateEventDetail<S, E> = {from: fromState, event, to: toState};\n\n if ((await this.shouldTransition_(eventDetail)) !== true) return;\n\n this.notify_({state: toState});\n\n this.postTransition__(eventDetail);\n }\n\n /**\n * Execute all actions for current state.\n */\n private async postTransition__(eventDetail: StateEventDetail<S, E>): Promise<void> {\n this.logger_.logMethodArgs?.('_transitioned', eventDetail);\n\n await this.execAction__(`on_${eventDetail.event}`, eventDetail);\n\n if (eventDetail.from !== eventDetail.to) {\n await this.execAction__(`on_state_exit`, eventDetail);\n await this.execAction__(`on_${eventDetail.from}_exit`, eventDetail);\n await this.execAction__(`on_state_enter`, eventDetail);\n await this.execAction__(`on_${eventDetail.to}_enter`, eventDetail);\n }\n\n if (Object.hasOwn(this, `on_${eventDetail.from}_${eventDetail.event}`)) {\n this.execAction__(`on_${eventDetail.from}_${eventDetail.event}`, eventDetail);\n }\n else {\n // The action `all_eventName` is executed only if the action `fromState_eventName` is not defined.\n this.execAction__(`on_all_${eventDetail.event}`, eventDetail);\n }\n }\n\n /**\n * Execute action name if defined in _actionRecord.\n */\n private execAction__(name: ActionName<S, E>, eventDetail: StateEventDetail<S, E>): MaybePromise<void> {\n const actionFn = this.actionRecord_[name];\n if (typeof actionFn === 'function') {\n this.logger_.logMethodArgs?.('_$execAction', name);\n return actionFn.call(this, eventDetail);\n }\n }\n}\n", "import {AlwatrFluxStateMachineBase} from './base.js';\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachine<S extends string, E extends string> extends AlwatrFluxStateMachineBase<S, E> {\n /**\n * Current state.\n */\n get state(): S {\n return this.message_.state;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n transition(event: E): void {\n this.transition_(event);\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n resetToInitialState(): void {\n this.resetToInitialState_();\n }\n}\n"],
5
- "mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,4BAAAE,EAAA,+BAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAA4B,0BAC5BC,EAA4D,8BAC5DC,EAAO,uCAMP,iBAAc,cAAe,OAAmB,EASzC,IAAeC,EAAf,cAAsF,kBAA6B,CAexH,YAAYC,EAAyC,CACnDA,EAAO,eAAPA,EAAO,aAAiB,sBACxB,MAAMA,CAAM,EAbd,KAAU,aAAkC,CAAC,EAK7C,KAAU,cAAoC,CAAC,EAS7C,KAAK,SAAW,CAAC,MAAO,KAAK,cAAgBA,EAAO,YAAY,CAClE,CAKU,sBAA6B,CACrC,KAAK,QAAQ,YAAY,sBAAsB,EAC/C,KAAK,SAAW,CAAC,MAA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AcD,CAAI,EACxC,GAAI,OAAOC,GAAa,WACtB,YAAK,QAAQ,gBAAgB,eAAgBD,CAAI,EAC1CC,EAAS,KAAK,KAAMF,CAAW,CAE1C,CACF,EC7GO,IAAeG,EAAf,cAAkFC,CAAiC,CAIxH,IAAI,OAAW,CACb,OAAO,KAAK,SAAS,KACvB,CAKA,WAAWC,EAAgB,CACzB,KAAK,YAAYA,CAAK,CACxB,CAKA,qBAA4B,CAC1B,KAAK,qBAAqB,CAC5B,CACF",
6
- "names": ["main_exports", "__export", "AlwatrFluxStateMachine", "AlwatrFluxStateMachineBase", "__toCommonJS", "import_logger", "import_observable", "import_polyfill_has_own", "AlwatrFluxStateMachineBase", "config", "_eventDetail", "event", "fromState", "toState", "eventDetail", "name", "actionFn", "AlwatrFluxStateMachine", "AlwatrFluxStateMachineBase", "event"]
4
+ "sourcesContent": ["export * from './base.js';\nexport * from './fsm.js';\nexport * from './type.js';\n", "import {packageTracer} from '@alwatr/nanolib';\nimport {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\n\nimport type {ActionName, ActionRecord, StateEventDetail, StateRecord} from './type.js';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport interface AlwatrFluxStateMachineConfig<S extends string> extends AlwatrObservableConfig {\n initialState: S;\n}\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachineBase<S extends string, E extends string> extends AlwatrObservable<{state: S}> {\n /**\n * States and transitions config.\n */\n protected stateRecord_: StateRecord<S, E> = {};\n\n /**\n * Bind actions name to class methods\n */\n protected actionRecord_: ActionRecord<S, E> = {};\n\n protected initialState_: S;\n\n protected override message_: {state: S};\n\n constructor(config: AlwatrFluxStateMachineConfig<S>) {\n config.loggerPrefix ??= 'flux-state-machine';\n super(config);\n this.message_ = {state: this.initialState_ = config.initialState};\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n protected resetToInitialState_(): void {\n this.logger_.logMethod?.('resetToInitialState_');\n this.message_ = {state: this.initialState_};\n }\n\n /**\n * Transition condition.\n */\n protected shouldTransition_(_eventDetail: StateEventDetail<S, E>): MaybePromise<boolean> {\n this.logger_.logMethodFull?.('shouldTransition_', _eventDetail, true);\n return true;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n protected async transition_(event: E): Promise<void> {\n const fromState = this.message_.state;\n const toState = this.stateRecord_[fromState]?.[event] ?? this.stateRecord_._all?.[event];\n\n this.logger_.logMethodArgs?.('transition_', {fromState, event, toState});\n\n if (toState == null) {\n this.logger_.incident?.('transition', 'invalid_target_state', {\n fromState,\n event,\n });\n return;\n }\n\n const eventDetail: StateEventDetail<S, E> = {from: fromState, event, to: toState};\n\n if ((await this.shouldTransition_(eventDetail)) !== true) return;\n\n this.notify_({state: toState});\n\n this.postTransition__(eventDetail);\n }\n\n /**\n * Execute all actions for current state.\n */\n private async postTransition__(eventDetail: StateEventDetail<S, E>): Promise<void> {\n this.logger_.logMethodArgs?.('_transitioned', eventDetail);\n\n await this.execAction__(`on_${eventDetail.event}`, eventDetail);\n\n if (eventDetail.from !== eventDetail.to) {\n await this.execAction__(`on_state_exit`, eventDetail);\n await this.execAction__(`on_${eventDetail.from}_exit`, eventDetail);\n await this.execAction__(`on_state_enter`, eventDetail);\n await this.execAction__(`on_${eventDetail.to}_enter`, eventDetail);\n }\n\n if (Object.hasOwn(this, `on_${eventDetail.from}_${eventDetail.event}`)) {\n this.execAction__(`on_${eventDetail.from}_${eventDetail.event}`, eventDetail);\n }\n else {\n // The action `all_eventName` is executed only if the action `fromState_eventName` is not defined.\n this.execAction__(`on_all_${eventDetail.event}`, eventDetail);\n }\n }\n\n /**\n * Execute action name if defined in _actionRecord.\n */\n private execAction__(name: ActionName<S, E>, eventDetail: StateEventDetail<S, E>): MaybePromise<void> {\n const actionFn = this.actionRecord_[name];\n if (typeof actionFn === 'function') {\n this.logger_.logMethodArgs?.('_$execAction', name);\n return actionFn.call(this, eventDetail);\n }\n }\n}\n", "import {AlwatrFluxStateMachineBase} from './base.js';\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachine<S extends string, E extends string> extends AlwatrFluxStateMachineBase<S, E> {\n /**\n * Current state.\n */\n get state(): S {\n return this.message_.state;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n transition(event: E): void {\n this.transition_(event);\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n resetToInitialState(): void {\n this.resetToInitialState_();\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAA4B;AAC5B,wBAA4D;AAI5D,aAAc,8BAAc,IAAI,eAAkB,OAAmB;AAS9D,IAAe,6BAAf,cAAsF,mCAA6B;AAAA,EAexH,YAAY,QAAyC;AACnD,WAAO,iBAAP,OAAO,eAAiB;AACxB,UAAM,MAAM;AAbd;AAAA;AAAA;AAAA,SAAU,eAAkC,CAAC;AAK7C;AAAA;AAAA;AAAA,SAAU,gBAAoC,CAAC;AAS7C,SAAK,WAAW,EAAC,OAAO,KAAK,gBAAgB,OAAO,aAAY;AAAA,EAClE;AAAA;AAAA;AAAA;AAAA,EAKU,uBAA6B;AACrC,SAAK,QAAQ,YAAY,sBAAsB;AAC/C,SAAK,WAAW,EAAC,OAAO,KAAK,cAAa;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA,EAKU,kBAAkB,cAA6D;AACvF,SAAK,QAAQ,gBAAgB,qBAAqB,cAAc,IAAI;AACpE,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,MAAgB,YAAY,OAAyB;AACnD,UAAM,YAAY,KAAK,SAAS;AAChC,UAAM,UAAU,KAAK,aAAa,SAAS,IAAI,KAAK,KAAK,KAAK,aAAa,OAAO,KAAK;AAEvF,SAAK,QAAQ,gBAAgB,eAAe,EAAC,WAAW,OAAO,QAAO,CAAC;AAEvE,QAAI,WAAW,MAAM;AACnB,WAAK,QAAQ,WAAW,cAAc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A;AAAA,EAKA,WAAW,OAAgB;AACzB,SAAK,YAAY,KAAK;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,sBAA4B;AAC1B,SAAK,qBAAqB;AAAA,EAC5B;AACF;",
6
+ "names": []
7
7
  }
package/dist/main.mjs CHANGED
@@ -1,3 +1,110 @@
1
- /* @alwatr/fsm v3.1.1 */
2
- import{definePackage as _}from"@alwatr/logger";import{AlwatrObservable as c}from"@alwatr/observable";import"@alwatr/polyfill-has-own";_("@alwatr/fsm","3.1.1");var i=class extends c{constructor(t){t.loggerPrefix??(t.loggerPrefix="flux-state-machine");super(t);this.stateRecord_={};this.actionRecord_={};this.message_={state:this.initialState_=t.initialState}}resetToInitialState_(){this.logger_.logMethod?.("resetToInitialState_"),this.message_={state:this.initialState_}}shouldTransition_(t){return this.logger_.logMethodFull?.("shouldTransition_",t,!0),!0}async transition_(t){let o=this.message_.state,e=this.stateRecord_[o]?.[t]??this.stateRecord_._all?.[t];if(this.logger_.logMethodArgs?.("transition_",{fromState:o,event:t,toState:e}),e==null){this.logger_.incident?.("transition","invalid_target_state",{fromState:o,event:t});return}let s={from:o,event:t,to:e};await this.shouldTransition_(s)===!0&&(this.notify_({state:e}),this.postTransition__(s))}async postTransition__(t){this.logger_.logMethodArgs?.("_transitioned",t),await this.execAction__(`on_${t.event}`,t),t.from!==t.to&&(await this.execAction__("on_state_exit",t),await this.execAction__(`on_${t.from}_exit`,t),await this.execAction__("on_state_enter",t),await this.execAction__(`on_${t.to}_enter`,t)),Object.hasOwn(this,`on_${t.from}_${t.event}`)?this.execAction__(`on_${t.from}_${t.event}`,t):this.execAction__(`on_all_${t.event}`,t)}execAction__(t,o){let e=this.actionRecord_[t];if(typeof e=="function")return this.logger_.logMethodArgs?.("_$execAction",t),e.call(this,o)}};var a=class extends i{get state(){return this.message_.state}transition(r){this.transition_(r)}resetToInitialState(){this.resetToInitialState_()}};export{a as AlwatrFluxStateMachine,i as AlwatrFluxStateMachineBase};
1
+ /* @alwatr/fsm v3.2.0 */
2
+
3
+ // src/base.ts
4
+ import { packageTracer } from "@alwatr/nanolib";
5
+ import { AlwatrObservable } from "@alwatr/observable";
6
+ __dev_mode__: packageTracer.add("@alwatr/fsm", "3.2.0");
7
+ var AlwatrFluxStateMachineBase = class extends AlwatrObservable {
8
+ constructor(config) {
9
+ config.loggerPrefix ?? (config.loggerPrefix = "flux-state-machine");
10
+ super(config);
11
+ /**
12
+ * States and transitions config.
13
+ */
14
+ this.stateRecord_ = {};
15
+ /**
16
+ * Bind actions name to class methods
17
+ */
18
+ this.actionRecord_ = {};
19
+ this.message_ = { state: this.initialState_ = config.initialState };
20
+ }
21
+ /**
22
+ * Reset machine to initial state without notify.
23
+ */
24
+ resetToInitialState_() {
25
+ this.logger_.logMethod?.("resetToInitialState_");
26
+ this.message_ = { state: this.initialState_ };
27
+ }
28
+ /**
29
+ * Transition condition.
30
+ */
31
+ shouldTransition_(_eventDetail) {
32
+ this.logger_.logMethodFull?.("shouldTransition_", _eventDetail, true);
33
+ return true;
34
+ }
35
+ /**
36
+ * Transition flux state machine instance to new state.
37
+ */
38
+ async transition_(event) {
39
+ const fromState = this.message_.state;
40
+ const toState = this.stateRecord_[fromState]?.[event] ?? this.stateRecord_._all?.[event];
41
+ this.logger_.logMethodArgs?.("transition_", { fromState, event, toState });
42
+ if (toState == null) {
43
+ this.logger_.incident?.("transition", "invalid_target_state", {
44
+ fromState,
45
+ event
46
+ });
47
+ return;
48
+ }
49
+ const eventDetail = { from: fromState, event, to: toState };
50
+ if (await this.shouldTransition_(eventDetail) !== true) return;
51
+ this.notify_({ state: toState });
52
+ this.postTransition__(eventDetail);
53
+ }
54
+ /**
55
+ * Execute all actions for current state.
56
+ */
57
+ async postTransition__(eventDetail) {
58
+ this.logger_.logMethodArgs?.("_transitioned", eventDetail);
59
+ await this.execAction__(`on_${eventDetail.event}`, eventDetail);
60
+ if (eventDetail.from !== eventDetail.to) {
61
+ await this.execAction__(`on_state_exit`, eventDetail);
62
+ await this.execAction__(`on_${eventDetail.from}_exit`, eventDetail);
63
+ await this.execAction__(`on_state_enter`, eventDetail);
64
+ await this.execAction__(`on_${eventDetail.to}_enter`, eventDetail);
65
+ }
66
+ if (Object.hasOwn(this, `on_${eventDetail.from}_${eventDetail.event}`)) {
67
+ this.execAction__(`on_${eventDetail.from}_${eventDetail.event}`, eventDetail);
68
+ } else {
69
+ this.execAction__(`on_all_${eventDetail.event}`, eventDetail);
70
+ }
71
+ }
72
+ /**
73
+ * Execute action name if defined in _actionRecord.
74
+ */
75
+ execAction__(name, eventDetail) {
76
+ const actionFn = this.actionRecord_[name];
77
+ if (typeof actionFn === "function") {
78
+ this.logger_.logMethodArgs?.("_$execAction", name);
79
+ return actionFn.call(this, eventDetail);
80
+ }
81
+ }
82
+ };
83
+
84
+ // src/fsm.ts
85
+ var AlwatrFluxStateMachine = class extends AlwatrFluxStateMachineBase {
86
+ /**
87
+ * Current state.
88
+ */
89
+ get state() {
90
+ return this.message_.state;
91
+ }
92
+ /**
93
+ * Transition flux state machine instance to new state.
94
+ */
95
+ transition(event) {
96
+ this.transition_(event);
97
+ }
98
+ /**
99
+ * Reset machine to initial state without notify.
100
+ */
101
+ resetToInitialState() {
102
+ this.resetToInitialState_();
103
+ }
104
+ };
105
+ export {
106
+ AlwatrFluxStateMachine,
107
+ AlwatrFluxStateMachineBase
108
+ };
109
+ /*! For license information please see main.mjs.LEGAL.txt */
3
110
  //# sourceMappingURL=main.mjs.map
package/dist/main.mjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/base.ts", "../src/fsm.ts"],
4
- "sourcesContent": ["import {definePackage} from '@alwatr/logger';\nimport {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\nimport '@alwatr/polyfill-has-own';\n\nimport type {ActionName, ActionRecord, StateEventDetail, StateRecord} from './type.js';\nimport type {} from '@alwatr/nano-build';\nimport type {MaybePromise} from '@alwatr/type-helper';\n\ndefinePackage('@alwatr/fsm', __package_version__);\n\nexport interface AlwatrFluxStateMachineConfig<S extends string> extends AlwatrObservableConfig {\n initialState: S;\n}\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachineBase<S extends string, E extends string> extends AlwatrObservable<{state: S}> {\n /**\n * States and transitions config.\n */\n protected stateRecord_: StateRecord<S, E> = {};\n\n /**\n * Bind actions name to class methods\n */\n protected actionRecord_: ActionRecord<S, E> = {};\n\n protected initialState_: S;\n\n protected override message_: {state: S};\n\n constructor(config: AlwatrFluxStateMachineConfig<S>) {\n config.loggerPrefix ??= 'flux-state-machine';\n super(config);\n this.message_ = {state: this.initialState_ = config.initialState};\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n protected resetToInitialState_(): void {\n this.logger_.logMethod?.('resetToInitialState_');\n this.message_ = {state: this.initialState_};\n }\n\n /**\n * Transition condition.\n */\n protected shouldTransition_(_eventDetail: StateEventDetail<S, E>): MaybePromise<boolean> {\n this.logger_.logMethodFull?.('shouldTransition_', _eventDetail, true);\n return true;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n protected async transition_(event: E): Promise<void> {\n const fromState = this.message_.state;\n const toState = this.stateRecord_[fromState]?.[event] ?? this.stateRecord_._all?.[event];\n\n this.logger_.logMethodArgs?.('transition_', {fromState, event, toState});\n\n if (toState == null) {\n this.logger_.incident?.('transition', 'invalid_target_state', {\n fromState,\n event,\n });\n return;\n }\n\n const eventDetail: StateEventDetail<S, E> = {from: fromState, event, to: toState};\n\n if ((await this.shouldTransition_(eventDetail)) !== true) return;\n\n this.notify_({state: toState});\n\n this.postTransition__(eventDetail);\n }\n\n /**\n * Execute all actions for current state.\n */\n private async postTransition__(eventDetail: StateEventDetail<S, E>): Promise<void> {\n this.logger_.logMethodArgs?.('_transitioned', eventDetail);\n\n await this.execAction__(`on_${eventDetail.event}`, eventDetail);\n\n if (eventDetail.from !== eventDetail.to) {\n await this.execAction__(`on_state_exit`, eventDetail);\n await this.execAction__(`on_${eventDetail.from}_exit`, eventDetail);\n await this.execAction__(`on_state_enter`, eventDetail);\n await this.execAction__(`on_${eventDetail.to}_enter`, eventDetail);\n }\n\n if (Object.hasOwn(this, `on_${eventDetail.from}_${eventDetail.event}`)) {\n this.execAction__(`on_${eventDetail.from}_${eventDetail.event}`, eventDetail);\n }\n else {\n // The action `all_eventName` is executed only if the action `fromState_eventName` is not defined.\n this.execAction__(`on_all_${eventDetail.event}`, eventDetail);\n }\n }\n\n /**\n * Execute action name if defined in _actionRecord.\n */\n private execAction__(name: ActionName<S, E>, eventDetail: StateEventDetail<S, E>): MaybePromise<void> {\n const actionFn = this.actionRecord_[name];\n if (typeof actionFn === 'function') {\n this.logger_.logMethodArgs?.('_$execAction', name);\n return actionFn.call(this, eventDetail);\n }\n }\n}\n", "import {AlwatrFluxStateMachineBase} from './base.js';\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachine<S extends string, E extends string> extends AlwatrFluxStateMachineBase<S, E> {\n /**\n * Current state.\n */\n get state(): S {\n return this.message_.state;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n transition(event: E): void {\n this.transition_(event);\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n resetToInitialState(): void {\n this.resetToInitialState_();\n }\n}\n"],
5
- "mappings": ";AAAA,OAAQ,iBAAAA,MAAoB,iBAC5B,OAAQ,oBAAAC,MAAoD,qBAC5D,MAAO,2BAMPD,EAAc,cAAe,OAAmB,EASzC,IAAeE,EAAf,cAAsFD,CAA6B,CAexH,YAAYE,EAAyC,CACnDA,EAAO,eAAPA,EAAO,aAAiB,sBACxB,MAAMA,CAAM,EAbd,KAAU,aAAkC,CAAC,EAK7C,KAAU,cAAoC,CAAC,EAS7C,KAAK,SAAW,CAAC,MAAO,KAAK,cAAgBA,EAAO,YAAY,CAClE,CAKU,sBAA6B,CACrC,KAAK,QAAQ,YAAY,sBAAsB,EAC/C,KAAK,SAAW,CAAC,MAAO,KAAK,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I,EACxC,GAAI,OAAOC,GAAa,WACtB,YAAK,QAAQ,gBAAgB,eAAgBD,CAAI,EAC1CC,EAAS,KAAK,KAAMF,CAAW,CAE1C,CACF,EC7GO,IAAeG,EAAf,cAAkFC,CAAiC,CAIxH,IAAI,OAAW,CACb,OAAO,KAAK,SAAS,KACvB,CAKA,WAAWC,EAAgB,CACzB,KAAK,YAAYA,CAAK,CACxB,CAKA,qBAA4B,CAC1B,KAAK,qBAAqB,CAC5B,CACF",
6
- "names": ["definePackage", "AlwatrObservable", "AlwatrFluxStateMachineBase", "config", "_eventDetail", "event", "fromState", "toState", "eventDetail", "name", "actionFn", "AlwatrFluxStateMachine", "AlwatrFluxStateMachineBase", "event"]
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/nanolib';\nimport {AlwatrObservable, type AlwatrObservableConfig} from '@alwatr/observable';\n\nimport type {ActionName, ActionRecord, StateEventDetail, StateRecord} from './type.js';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport interface AlwatrFluxStateMachineConfig<S extends string> extends AlwatrObservableConfig {\n initialState: S;\n}\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachineBase<S extends string, E extends string> extends AlwatrObservable<{state: S}> {\n /**\n * States and transitions config.\n */\n protected stateRecord_: StateRecord<S, E> = {};\n\n /**\n * Bind actions name to class methods\n */\n protected actionRecord_: ActionRecord<S, E> = {};\n\n protected initialState_: S;\n\n protected override message_: {state: S};\n\n constructor(config: AlwatrFluxStateMachineConfig<S>) {\n config.loggerPrefix ??= 'flux-state-machine';\n super(config);\n this.message_ = {state: this.initialState_ = config.initialState};\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n protected resetToInitialState_(): void {\n this.logger_.logMethod?.('resetToInitialState_');\n this.message_ = {state: this.initialState_};\n }\n\n /**\n * Transition condition.\n */\n protected shouldTransition_(_eventDetail: StateEventDetail<S, E>): MaybePromise<boolean> {\n this.logger_.logMethodFull?.('shouldTransition_', _eventDetail, true);\n return true;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n protected async transition_(event: E): Promise<void> {\n const fromState = this.message_.state;\n const toState = this.stateRecord_[fromState]?.[event] ?? this.stateRecord_._all?.[event];\n\n this.logger_.logMethodArgs?.('transition_', {fromState, event, toState});\n\n if (toState == null) {\n this.logger_.incident?.('transition', 'invalid_target_state', {\n fromState,\n event,\n });\n return;\n }\n\n const eventDetail: StateEventDetail<S, E> = {from: fromState, event, to: toState};\n\n if ((await this.shouldTransition_(eventDetail)) !== true) return;\n\n this.notify_({state: toState});\n\n this.postTransition__(eventDetail);\n }\n\n /**\n * Execute all actions for current state.\n */\n private async postTransition__(eventDetail: StateEventDetail<S, E>): Promise<void> {\n this.logger_.logMethodArgs?.('_transitioned', eventDetail);\n\n await this.execAction__(`on_${eventDetail.event}`, eventDetail);\n\n if (eventDetail.from !== eventDetail.to) {\n await this.execAction__(`on_state_exit`, eventDetail);\n await this.execAction__(`on_${eventDetail.from}_exit`, eventDetail);\n await this.execAction__(`on_state_enter`, eventDetail);\n await this.execAction__(`on_${eventDetail.to}_enter`, eventDetail);\n }\n\n if (Object.hasOwn(this, `on_${eventDetail.from}_${eventDetail.event}`)) {\n this.execAction__(`on_${eventDetail.from}_${eventDetail.event}`, eventDetail);\n }\n else {\n // The action `all_eventName` is executed only if the action `fromState_eventName` is not defined.\n this.execAction__(`on_all_${eventDetail.event}`, eventDetail);\n }\n }\n\n /**\n * Execute action name if defined in _actionRecord.\n */\n private execAction__(name: ActionName<S, E>, eventDetail: StateEventDetail<S, E>): MaybePromise<void> {\n const actionFn = this.actionRecord_[name];\n if (typeof actionFn === 'function') {\n this.logger_.logMethodArgs?.('_$execAction', name);\n return actionFn.call(this, eventDetail);\n }\n }\n}\n", "import {AlwatrFluxStateMachineBase} from './base.js';\n\n/**\n * Flux (Finite) State Machine Base Class\n */\nexport abstract class AlwatrFluxStateMachine<S extends string, E extends string> extends AlwatrFluxStateMachineBase<S, E> {\n /**\n * Current state.\n */\n get state(): S {\n return this.message_.state;\n }\n\n /**\n * Transition flux state machine instance to new state.\n */\n transition(event: E): void {\n this.transition_(event);\n }\n\n /**\n * Reset machine to initial state without notify.\n */\n resetToInitialState(): void {\n this.resetToInitialState_();\n }\n}\n"],
5
+ "mappings": ";;;AAAA,SAAQ,qBAAoB;AAC5B,SAAQ,wBAAoD;AAI5D,aAAc,eAAc,IAAI,eAAkB,OAAmB;AAS9D,IAAe,6BAAf,cAAsF,iBAA6B;AAAA,EAexH,YAAY,QAAyC;AACnD,WAAO,iBAAP,OAAO,eAAiB;AACxB,UAAM,MAAM;AAbd;AAAA;AAAA;AAAA,SAAU,eAAkC,CAAC;AAK7C;AAAA;AAAA;AAAA,SAAU,gBAAoC,CAAC;AAS7C,SAAK,WAAW,EAAC,OAAO,KAAK,gBAAgB,OAAO,aAAY;AAAA,EAClE;AAAA;AAAA;AAAA;AAAA,EAKU,uBAA6B;AACrC,SAAK,QAAQ,YAAY,sBAAsB;AAC/C,SAAK,WAAW,EAAC,OAAO,KAAK,cAAa;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA,EAKU,kBAAkB,cAA6D;AACvF,SAAK,QAAQ,gBAAgB,qBAAqB,cAAc,IAAI;AACpE,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,MAAgB,YAAY,OAAyB;AACnD,UAAM,YAAY,KAAK,SAAS;AAChC,UAAM,UAAU,KAAK,aAAa,SAAS,IAAI,KAAK,KAAK,KAAK,aAAa,OAAO,KAAK;AAEvF,SAAK,QAAQ,gBAAgB,eAAe,EAAC,WAAW,OAAO,QAAO,CAAC;AAEvE,QAAI,WAAW,MAAM;AACnB,WAAK,QAAQ,WAAW,cAAc,wBAAwB;AAAA,QAC5D;AAAA,QACA;AAAA,MACF,CAAC;AACD;AAAA,IACF;AAEA,UAAM,cAAsC,EAAC,MAAM,WAAW,OAAO,IAAI,QAAO;AAEhF,QAAK,MAAM,KAAK,kBAAkB,WAAW,MAAO,KA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type.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { MaybePromise } from '@alwatr/type-helper';
2
1
  export interface StateEventDetail<S, E> {
3
2
  from: S;
4
3
  event: E;
@@ -1 +1 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAEtD,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC;IACpC,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,CAAC,CAAC;IACT,EAAE,EAAE,CAAC,CAAC;CACP;AAED,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEjH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,WAAW,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;AAEtH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IACrD,MAAM,CAAC,EAAE,GACT,eAAe,GACf,gBAAgB,GAChB,MAAM,CAAC,OAAO,GACd,MAAM,CAAC,QAAQ,GACf,MAAM,CAAC,IAAI,CAAC,EAAE,GACd,UAAU,CAAC,EAAE,CAAC;AAElB,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC;IACpC,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,CAAC,CAAC;IACT,EAAE,EAAE,CAAC,CAAC;CACP;AAED,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEjH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,WAAW,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;AAEtH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IACrD,MAAM,CAAC,EAAE,GACT,eAAe,GACf,gBAAgB,GAChB,MAAM,CAAC,OAAO,GACd,MAAM,CAAC,QAAQ,GACf,MAAM,CAAC,IAAI,CAAC,EAAE,GACd,UAAU,CAAC,EAAE,CAAC;AAElB,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alwatr/fsm",
3
- "version": "3.1.1",
3
+ "version": "3.2.0",
4
4
  "description": "A robust TypeScript library for implementing Flux (Finite) State Machines, enabling clear and organized management of application state and transitions.",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
6
6
  "keywords": [
@@ -58,18 +58,17 @@
58
58
  "clean": "rm -rfv dist *.tsbuildinfo"
59
59
  },
60
60
  "dependencies": {
61
- "@alwatr/logger": "^3.2.14",
62
- "@alwatr/observable": "^3.1.1",
63
- "@alwatr/polyfill-has-own": "^1.0.10"
61
+ "@alwatr/nanolib": "^1.2.0",
62
+ "@alwatr/observable": "^3.2.0"
64
63
  },
65
64
  "devDependencies": {
66
- "@alwatr/nano-build": "^1.3.10",
67
- "@alwatr/prettier-config": "^1.0.4",
68
- "@alwatr/tsconfig-base": "^1.2.0",
69
- "@alwatr/type-helper": "^1.2.6",
70
- "@types/node": "^22.7.4",
65
+ "@alwatr/nano-build": "^2.0.1",
66
+ "@alwatr/prettier-config": "^1.0.6",
67
+ "@alwatr/tsconfig-base": "^1.3.2",
68
+ "@alwatr/type-helper": "^2.0.2",
69
+ "@types/node": "^22.7.5",
71
70
  "jest": "^29.7.0",
72
- "typescript": "^5.6.2"
71
+ "typescript": "^5.6.3"
73
72
  },
74
- "gitHead": "e0a53586929eaaf25cdde4a0845650b8c7012ab7"
73
+ "gitHead": "52e38b665b848a23e74024340208ae4a4d98cbb8"
75
74
  }