@alwatr/fsm 3.0.3 → 3.1.1
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 +25 -0
- package/README.md +8 -8
- package/dist/fsm.d.ts +4 -0
- package/dist/fsm.d.ts.map +1 -1
- package/dist/main.cjs +2 -2
- package/dist/main.cjs.map +2 -2
- package/dist/main.mjs +2 -2
- package/dist/main.mjs.map +2 -2
- package/dist/type.d.ts +1 -1
- package/dist/type.d.ts.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,31 @@
|
|
|
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.1.1](https://github.com/Alwatr/flux/compare/v3.1.0...v3.1.1) (2024-09-29)
|
|
7
|
+
|
|
8
|
+
### Miscellaneous Chores
|
|
9
|
+
|
|
10
|
+
* edited README ([fff9b3f](https://github.com/Alwatr/flux/commit/fff9b3f6ccc52e2257bdfe306e032ded07497b4a)) by @ArmanAsadian
|
|
11
|
+
* **fsm:** change the license to AGPL-3.0 ([babc4a8](https://github.com/Alwatr/flux/commit/babc4a82bd0421981ec40c150f0de262f0f81f42)) by @ArmanAsadian
|
|
12
|
+
|
|
13
|
+
### Dependencies update
|
|
14
|
+
|
|
15
|
+
* update ([fb148fd](https://github.com/Alwatr/flux/commit/fb148fdbe7f84acc3eda625e5e8c5773747d32e9)) by @
|
|
16
|
+
|
|
17
|
+
## [3.1.0](https://github.com/Alwatr/flux/compare/v3.0.3...v3.1.0) (2024-09-26)
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* **fsm:** Add resetToInitialState() method ([86b2479](https://github.com/Alwatr/flux/commit/86b2479c0319b33c8108dfd0319b2c444dc5f6de)) by @AliMD
|
|
22
|
+
|
|
23
|
+
### Code Refactoring
|
|
24
|
+
|
|
25
|
+
* update action names in fetch-state-machine and fsm packages and remove `_` prefix ([a90d959](https://github.com/Alwatr/flux/commit/a90d95921b322a288c4a60671ce90ff9fe709c00)) by @AliMD
|
|
26
|
+
|
|
27
|
+
### Dependencies update
|
|
28
|
+
|
|
29
|
+
* bump @types/node in the development-dependencies group ([979223c](https://github.com/Alwatr/flux/commit/979223c3cdbb002a926e72e1a7f79c82ff7395d4)) by @dependabot[bot]
|
|
30
|
+
|
|
6
31
|
## [3.0.3](https://github.com/Alwatr/flux/compare/v3.0.2...v3.0.3) (2024-09-24)
|
|
7
32
|
|
|
8
33
|
**Note:** Version bump only for package @alwatr/fsm
|
package/README.md
CHANGED
|
@@ -47,9 +47,9 @@ class MyStateMachine extends FluxStateMachineBase<MyState, MyEvent> {
|
|
|
47
47
|
|
|
48
48
|
// Define actions (optional)
|
|
49
49
|
this.actionRecord_ = {
|
|
50
|
-
'
|
|
51
|
-
'
|
|
52
|
-
'
|
|
50
|
+
'on_fetch': this.handleFetch,
|
|
51
|
+
'on_success': this.handleSuccess,
|
|
52
|
+
'on_error': this.handleError,
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -87,14 +87,14 @@ class MyStateMachine extends FluxStateMachineBase<MyState, MyEvent> {
|
|
|
87
87
|
|
|
88
88
|
## Sponsors
|
|
89
89
|
|
|
90
|
-
The following companies, organizations, and individuals support
|
|
90
|
+
The following companies, organizations, and individuals support flux ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.
|
|
91
91
|
|
|
92
92
|
[](https://exirstudio.com)
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
### Contributing
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
Contributions are welcome! Please read our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md) before submitting a pull request.
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
### License
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
This project is licensed under the [AGPL-3.0 License](LICENSE).
|
package/dist/fsm.d.ts
CHANGED
|
@@ -11,5 +11,9 @@ export declare abstract class AlwatrFluxStateMachine<S extends string, E extends
|
|
|
11
11
|
* Transition flux state machine instance to new state.
|
|
12
12
|
*/
|
|
13
13
|
transition(event: E): void;
|
|
14
|
+
/**
|
|
15
|
+
* Reset machine to initial state without notify.
|
|
16
|
+
*/
|
|
17
|
+
resetToInitialState(): void;
|
|
14
18
|
}
|
|
15
19
|
//# sourceMappingURL=fsm.d.ts.map
|
package/dist/fsm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fsm.d.ts","sourceRoot":"","sources":["../src/fsm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,0BAA0B,EAAC,MAAM,WAAW,CAAC;AAErD;;GAEG;AACH,8BAAsB,sBAAsB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAE,SAAQ,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAC;IACvH;;OAEG;IACH,IAAI,KAAK,IAAI,CAAC,CAEb;IAED;;OAEG;IACH,UAAU,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"fsm.d.ts","sourceRoot":"","sources":["../src/fsm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,0BAA0B,EAAC,MAAM,WAAW,CAAC;AAErD;;GAEG;AACH,8BAAsB,sBAAsB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAE,SAAQ,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAC;IACvH;;OAEG;IACH,IAAI,KAAK,IAAI,CAAC,CAEb;IAED;;OAEG;IACH,UAAU,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAI1B;;OAEG;IACH,mBAAmB,IAAI,IAAI;CAG5B"}
|
package/dist/main.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* @alwatr/fsm v3.
|
|
2
|
-
"use strict";var a=Object.defineProperty;var
|
|
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});
|
|
3
3
|
//# 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__(`
|
|
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,MAAO,KAAK,aAAa,CAC5C,CAKU,kBAAkBC,EAA6D,CACvF,YAAK,QAAQ,gBAAgB,oBAAqBA,EAAc,EAAI,EAC7D,EACT,CAKA,MAAgB,YAAYC,EAAyB,CACnD,IAAMC,EAAY,KAAK,SAAS,MAC1BC,EAAU,KAAK,aAAaD,CAAS,IAAID,CAAK,GAAK,KAAK,aAAa,OAAOA,CAAK,EAIvF,GAFA,KAAK,QAAQ,gBAAgB,cAAe,CAAC,UAAAC,EAAW,MAAAD,EAAO,QAAAE,CAAO,CAAC,EAEnEA,GAAW,KAAM,CACnB,KAAK,QAAQ,WAAW,aAAc,uBAAwB,CAC5D,UAAAD,EACA,MAAAD,CACF,CAAC,EACD,MACF,CAEA,IAAMG,EAAsC,CAAC,KAAMF,EAAW,MAAAD,EAAO,GAAIE,CAAO,EAE3E,MAAM,KAAK,kBAAkBC,CAAW,IAAO,KAEpD,KAAK,QAAQ,CAAC,MAAOD,CAAO,CAAC,EAE7B,KAAK,iBAAiBC,CAAW,EACnC,CAKA,MAAc,iBAAiBA,EAAoD,CACjF,KAAK,QAAQ,gBAAgB,gBAAiBA,CAAW,EAEzD,MAAM,KAAK,aAAa,
|
|
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,MAAO,KAAK,aAAa,CAC5C,CAKU,kBAAkBC,EAA6D,CACvF,YAAK,QAAQ,gBAAgB,oBAAqBA,EAAc,EAAI,EAC7D,EACT,CAKA,MAAgB,YAAYC,EAAyB,CACnD,IAAMC,EAAY,KAAK,SAAS,MAC1BC,EAAU,KAAK,aAAaD,CAAS,IAAID,CAAK,GAAK,KAAK,aAAa,OAAOA,CAAK,EAIvF,GAFA,KAAK,QAAQ,gBAAgB,cAAe,CAAC,UAAAC,EAAW,MAAAD,EAAO,QAAAE,CAAO,CAAC,EAEnEA,GAAW,KAAM,CACnB,KAAK,QAAQ,WAAW,aAAc,uBAAwB,CAC5D,UAAAD,EACA,MAAAD,CACF,CAAC,EACD,MACF,CAEA,IAAMG,EAAsC,CAAC,KAAMF,EAAW,MAAAD,EAAO,GAAIE,CAAO,EAE3E,MAAM,KAAK,kBAAkBC,CAAW,IAAO,KAEpD,KAAK,QAAQ,CAAC,MAAOD,CAAO,CAAC,EAE7B,KAAK,iBAAiBC,CAAW,EACnC,CAKA,MAAc,iBAAiBA,EAAoD,CACjF,KAAK,QAAQ,gBAAgB,gBAAiBA,CAAW,EAEzD,MAAM,KAAK,aAAa,MAAMA,EAAY,KAAK,GAAIA,CAAW,EAE1DA,EAAY,OAASA,EAAY,KACnC,MAAM,KAAK,aAAa,gBAAiBA,CAAW,EACpD,MAAM,KAAK,aAAa,MAAMA,EAAY,IAAI,QAASA,CAAW,EAClE,MAAM,KAAK,aAAa,iBAAkBA,CAAW,EACrD,MAAM,KAAK,aAAa,MAAMA,EAAY,EAAE,SAAUA,CAAW,GAG/D,OAAO,OAAO,KAAM,MAAMA,EAAY,IAAI,IAAIA,EAAY,KAAK,EAAE,EACnE,KAAK,aAAa,MAAMA,EAAY,IAAI,IAAIA,EAAY,KAAK,GAAIA,CAAW,EAI5E,KAAK,aAAa,UAAUA,EAAY,KAAK,GAAIA,CAAW,CAEhE,CAKQ,aAAaC,EAAwBD,EAAyD,CACpG,IAAME,EAAW,KAAK,cAAcD,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
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"]
|
|
7
7
|
}
|
package/dist/main.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* @alwatr/fsm v3.
|
|
2
|
-
import{definePackage as _}from"@alwatr/logger";import{AlwatrObservable as c}from"@alwatr/observable";import"@alwatr/polyfill-has-own";_("@alwatr/fsm","3.
|
|
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};
|
|
3
3
|
//# 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__(`
|
|
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,aAAa,CAC5C,CAKU,kBAAkBC,EAA6D,CACvF,YAAK,QAAQ,gBAAgB,oBAAqBA,EAAc,EAAI,EAC7D,EACT,CAKA,MAAgB,YAAYC,EAAyB,CACnD,IAAMC,EAAY,KAAK,SAAS,MAC1BC,EAAU,KAAK,aAAaD,CAAS,IAAID,CAAK,GAAK,KAAK,aAAa,OAAOA,CAAK,EAIvF,GAFA,KAAK,QAAQ,gBAAgB,cAAe,CAAC,UAAAC,EAAW,MAAAD,EAAO,QAAAE,CAAO,CAAC,EAEnEA,GAAW,KAAM,CACnB,KAAK,QAAQ,WAAW,aAAc,uBAAwB,CAC5D,UAAAD,EACA,MAAAD,CACF,CAAC,EACD,MACF,CAEA,IAAMG,EAAsC,CAAC,KAAMF,EAAW,MAAAD,EAAO,GAAIE,CAAO,EAE3E,MAAM,KAAK,kBAAkBC,CAAW,IAAO,KAEpD,KAAK,QAAQ,CAAC,MAAOD,CAAO,CAAC,EAE7B,KAAK,iBAAiBC,CAAW,EACnC,CAKA,MAAc,iBAAiBA,EAAoD,CACjF,KAAK,QAAQ,gBAAgB,gBAAiBA,CAAW,EAEzD,MAAM,KAAK,aAAa,
|
|
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,aAAa,CAC5C,CAKU,kBAAkBC,EAA6D,CACvF,YAAK,QAAQ,gBAAgB,oBAAqBA,EAAc,EAAI,EAC7D,EACT,CAKA,MAAgB,YAAYC,EAAyB,CACnD,IAAMC,EAAY,KAAK,SAAS,MAC1BC,EAAU,KAAK,aAAaD,CAAS,IAAID,CAAK,GAAK,KAAK,aAAa,OAAOA,CAAK,EAIvF,GAFA,KAAK,QAAQ,gBAAgB,cAAe,CAAC,UAAAC,EAAW,MAAAD,EAAO,QAAAE,CAAO,CAAC,EAEnEA,GAAW,KAAM,CACnB,KAAK,QAAQ,WAAW,aAAc,uBAAwB,CAC5D,UAAAD,EACA,MAAAD,CACF,CAAC,EACD,MACF,CAEA,IAAMG,EAAsC,CAAC,KAAMF,EAAW,MAAAD,EAAO,GAAIE,CAAO,EAE3E,MAAM,KAAK,kBAAkBC,CAAW,IAAO,KAEpD,KAAK,QAAQ,CAAC,MAAOD,CAAO,CAAC,EAE7B,KAAK,iBAAiBC,CAAW,EACnC,CAKA,MAAc,iBAAiBA,EAAoD,CACjF,KAAK,QAAQ,gBAAgB,gBAAiBA,CAAW,EAEzD,MAAM,KAAK,aAAa,MAAMA,EAAY,KAAK,GAAIA,CAAW,EAE1DA,EAAY,OAASA,EAAY,KACnC,MAAM,KAAK,aAAa,gBAAiBA,CAAW,EACpD,MAAM,KAAK,aAAa,MAAMA,EAAY,IAAI,QAASA,CAAW,EAClE,MAAM,KAAK,aAAa,iBAAkBA,CAAW,EACrD,MAAM,KAAK,aAAa,MAAMA,EAAY,EAAE,SAAUA,CAAW,GAG/D,OAAO,OAAO,KAAM,MAAMA,EAAY,IAAI,IAAIA,EAAY,KAAK,EAAE,EACnE,KAAK,aAAa,MAAMA,EAAY,IAAI,IAAIA,EAAY,KAAK,GAAIA,CAAW,EAI5E,KAAK,aAAa,UAAUA,EAAY,KAAK,GAAIA,CAAW,CAEhE,CAKQ,aAAaC,EAAwBD,EAAyD,CACpG,IAAME,EAAW,KAAK,cAAcD,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
6
|
"names": ["definePackage", "AlwatrObservable", "AlwatrFluxStateMachineBase", "config", "_eventDetail", "event", "fromState", "toState", "eventDetail", "name", "actionFn", "AlwatrFluxStateMachine", "AlwatrFluxStateMachineBase", "event"]
|
|
7
7
|
}
|
package/dist/type.d.ts
CHANGED
|
@@ -6,6 +6,6 @@ export interface StateEventDetail<S, E> {
|
|
|
6
6
|
}
|
|
7
7
|
export type StateRecord<S extends string, E extends string> = Partial<Record<S | '_all', Partial<Record<E, S>>>>;
|
|
8
8
|
export type Action<S extends string, E extends string> = (eventDetail?: StateEventDetail<S, E>) => MaybePromise<void>;
|
|
9
|
-
export type ActionName<S extends string, E extends string> = `
|
|
9
|
+
export type ActionName<S extends string, E extends string> = `on_${E}` | `on_state_exit` | `on_state_enter` | `on_${S}_exit` | `on_${S}_enter` | `on_${S}_${E}` | `on_all_${E}`;
|
|
10
10
|
export type ActionRecord<S extends string, E extends string> = Partial<Record<ActionName<S, E>, Action<S, E>>>;
|
|
11
11
|
//# sourceMappingURL=type.d.ts.map
|
package/dist/type.d.ts.map
CHANGED
|
@@ -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,
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/fsm",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
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": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"require": "./dist/main.cjs"
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
|
-
"license": "
|
|
26
|
+
"license": "AGPL-3.0-only",
|
|
27
27
|
"files": [
|
|
28
28
|
"**/*.{js,mjs,cjs,map,d.ts,html,md}",
|
|
29
29
|
"!demo/**/*"
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@alwatr/logger": "^3.2.14",
|
|
62
|
-
"@alwatr/observable": "^3.
|
|
62
|
+
"@alwatr/observable": "^3.1.1",
|
|
63
63
|
"@alwatr/polyfill-has-own": "^1.0.10"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
@@ -67,9 +67,9 @@
|
|
|
67
67
|
"@alwatr/prettier-config": "^1.0.4",
|
|
68
68
|
"@alwatr/tsconfig-base": "^1.2.0",
|
|
69
69
|
"@alwatr/type-helper": "^1.2.6",
|
|
70
|
-
"@types/node": "^22.
|
|
70
|
+
"@types/node": "^22.7.4",
|
|
71
71
|
"jest": "^29.7.0",
|
|
72
72
|
"typescript": "^5.6.2"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "e0a53586929eaaf25cdde4a0845650b8c7012ab7"
|
|
75
75
|
}
|