@alwatr/fsm 0.30.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 ADDED
@@ -0,0 +1,23 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [0.30.0](https://github.com/AliMD/alwatr/compare/v0.29.0...v0.30.0) (2023-03-06)
7
+
8
+ ### Bug Fixes
9
+
10
+ - **fsm:** every signal mather ([0dc504d](https://github.com/AliMD/alwatr/commit/0dc504dacbb1ec68f154244619d644ff8e43cc04))
11
+ - **fsm:** remove additional import ([231337b](https://github.com/AliMD/alwatr/commit/231337b95ee7b046fe35429f50931ddf85be291f))
12
+ - **fsm:** update context in transition bug ([28a21d0](https://github.com/AliMD/alwatr/commit/28a21d00d903b6189d814303c72ba6e784852f33))
13
+
14
+ ### Features
15
+
16
+ - Alwatr Finite State Machines ([d5900b4](https://github.com/AliMD/alwatr/commit/d5900b4ee8685b120188888871405853f5a69417))
17
+ - **fsm:** $all and $self state ([69adf41](https://github.com/AliMD/alwatr/commit/69adf41064ca0f55497484c50e298ebc26c42dcc))
18
+ - **fsm:** enhance types ([3b13046](https://github.com/AliMD/alwatr/commit/3b130463a102f59c38603b0de470be5c87ee88c9))
19
+ - **fsm:** make simple state machine ([ff9ae1c](https://github.com/AliMD/alwatr/commit/ff9ae1ca04156e8b811899ff0f62480e1c37af72))
20
+ - **fsm:** new state context type with {to, from, by} ([11423e6](https://github.com/AliMD/alwatr/commit/11423e6a89159b92e82cfd1e774ad37983581090))
21
+ - **fsm:** rewrite with signal power ([01a1651](https://github.com/AliMD/alwatr/commit/01a1651e231a817d5eebb54cf84d51d620bfd6e8))
22
+ - **fsm:** share state events ([de42522](https://github.com/AliMD/alwatr/commit/de42522a97fdf6be8bee73d91a35820e2a5e6efb))
23
+ - **fsm:** transition with partial context set ([823377e](https://github.com/AliMD/alwatr/commit/823377e65028ea3e713f060ae678776c609c1661))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 S. Ali Mihandoost
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Alwatr Finite State Machines - `@alwatr/fsm`
2
+
3
+ Managing invocations finite-state machines as actors written in tiny TypeScript module.
package/core.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ import type { Stringifyable, StringifyableRecord } from '@alwatr/type';
2
+ export interface MachineConfig<TState extends string, TEventId extends string, TContext extends Stringifyable> extends StringifyableRecord {
3
+ /**
4
+ * Machine ID (It is used in the state change signal identifier, so it must be unique).
5
+ */
6
+ id: string;
7
+ /**
8
+ * Initial state.
9
+ */
10
+ initial: TState;
11
+ /**
12
+ * Initial context.
13
+ */
14
+ context: TContext;
15
+ /**
16
+ * States list
17
+ */
18
+ states: {
19
+ [S in TState | '$all']: {
20
+ /**
21
+ * An object mapping eventId (keys) to state.
22
+ */
23
+ on: {
24
+ [E in TEventId]?: TState | '$self';
25
+ };
26
+ };
27
+ };
28
+ }
29
+ export interface StateContext<TState extends string, TEventId extends string> {
30
+ [T: string]: string;
31
+ to: TState;
32
+ from: TState | 'init';
33
+ by: TEventId | 'INIT';
34
+ }
35
+ export declare class FiniteStateMachine<TState extends string = string, TEventId extends string = string, TContext extends StringifyableRecord = StringifyableRecord> {
36
+ readonly config: Readonly<MachineConfig<TState, TEventId, TContext>>;
37
+ state: StateContext<TState, TEventId>;
38
+ context: TContext;
39
+ signal: {
40
+ readonly id: string;
41
+ readonly getValue: () => StateContext<TState, TEventId> | undefined;
42
+ readonly untilChange: () => Promise<StateContext<TState, TEventId>>;
43
+ readonly subscribe: (listenerCallback: import("@alwatr/signal/type.js").ListenerFunction<StateContext<TState, TEventId>>, options?: Partial<import("@alwatr/signal/type.js").SubscribeOptions> | undefined) => import("@alwatr/signal").ListenerSpec;
44
+ readonly unsubscribe: (listener: import("@alwatr/signal").ListenerSpec) => void;
45
+ };
46
+ protected _logger: import("@alwatr/logger").AlwatrLogger;
47
+ protected setState(to: TState, by: TEventId | 'INIT'): void;
48
+ constructor(config: Readonly<MachineConfig<TState, TEventId, TContext>>);
49
+ /**
50
+ * Machine transition.
51
+ */
52
+ transition(event: TEventId, context?: Partial<TContext>): TState | null;
53
+ }
54
+ //# sourceMappingURL=core.d.ts.map
package/core.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["src/core.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,aAAa,EAAE,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAOrE,MAAM,WAAW,aAAa,CAAC,MAAM,SAAS,MAAM,EAAE,QAAQ,SAAS,MAAM,EAAE,QAAQ,SAAS,aAAa,CAC3G,SAAQ,mBAAmB;IAC3B;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;I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-0,0 +1,63 @@
1
+ import { createLogger, globalAlwatr } from '@alwatr/logger';
2
+ import { contextConsumer } from '@alwatr/signal';
3
+ import { dispatch } from '@alwatr/signal/core.js';
4
+ globalAlwatr.registeredList.push({
5
+ name: '@alwatr/fsm',
6
+ version: _ALWATR_VERSION_,
7
+ });
8
+ export class FiniteStateMachine {
9
+ setState(to, by) {
10
+ var _a, _b;
11
+ this.state = {
12
+ to,
13
+ from: (_b = (_a = this.signal.getValue()) === null || _a === void 0 ? void 0 : _a.to) !== null && _b !== void 0 ? _b : 'init',
14
+ by,
15
+ };
16
+ dispatch(this.signal.id, this.state, { debounce: 'NextCycle' });
17
+ }
18
+ constructor(config) {
19
+ this.config = config;
20
+ this.state = {
21
+ to: this.config.initial,
22
+ from: 'init',
23
+ by: 'INIT',
24
+ };
25
+ this.context = this.config.context;
26
+ this.signal = contextConsumer.bind('finite-state-machine-' + this.config.id);
27
+ this._logger = createLogger(`alwatr/fsm:${this.config.id}`);
28
+ this._logger.logMethodArgs('constructor', config);
29
+ dispatch(this.signal.id, this.state, { debounce: 'NextCycle' });
30
+ if (!config.states[config.initial]) {
31
+ this._logger.error('constructor', 'invalid_initial_state', config);
32
+ }
33
+ }
34
+ /**
35
+ * Machine transition.
36
+ */
37
+ transition(event, context) {
38
+ var _a, _b, _c, _d, _e, _f, _g;
39
+ const fromState = this.state.to;
40
+ let toState = (_c = (_b = (_a = this.config.states[fromState]) === null || _a === void 0 ? void 0 : _a.on) === null || _b === void 0 ? void 0 : _b[event]) !== null && _c !== void 0 ? _c : (_e = (_d = this.config.states.$all) === null || _d === void 0 ? void 0 : _d.on) === null || _e === void 0 ? void 0 : _e[event];
41
+ if (toState === '$self') {
42
+ toState = fromState;
43
+ }
44
+ this._logger.logMethodFull('transition', { fromState, event, context }, toState);
45
+ if (context !== undefined) {
46
+ this.context = {
47
+ ...this.context,
48
+ ...context,
49
+ };
50
+ }
51
+ if (toState == null) {
52
+ this._logger.incident('transition', 'invalid_target_state', 'Defined target state for this event not found in state config', {
53
+ fromState,
54
+ event,
55
+ events: { ...(_f = this.config.states.$all) === null || _f === void 0 ? void 0 : _f.on, ...(_g = this.config.states[fromState]) === null || _g === void 0 ? void 0 : _g.on },
56
+ });
57
+ return null;
58
+ }
59
+ this.setState(toState, event);
60
+ return toState;
61
+ }
62
+ }
63
+ //# sourceMappingURL=core.js.map
package/core.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.js","sourceRoot":"","sources":["src/core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAC,eAAe,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,QAAQ,EAAC,MAAM,wBAAwB,CAAC;AAIhD,YAAY,CAAC,cAAc,CAAC,IAAI,CAAC;IAC/B,IAAI,EAAE,aAAa;IACnB,OAAO,EAAE,gBAAgB;CAC1B,CAAC,CAAC;AAyCH,MAAM,OAAO,kBAAkB;IAenB,QAAQ,CAAC,EAAU,EAAE,EAAqB;;QAClD,IAAI,CAAC,KAAK,GAAG;YACX,EAAE;YACF,IAAI,EAAE,MAAA,MAAA,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,0CAAE,EAAE,mCAAI,MAAM;YAC1C,EAAE;SACH,CAAC;QACF,QAAQ,CAAiC,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,EAAC,QAAQ,EAAE,WAAW,EAAC,CAAC,CAAC;IAChG,CAAC;IAED,YAA4B,MAA2D;QAA3D,WAAM,GAAN,MAAM,CAAqD;QAnBvF,UAAK,GAAmC;YACtC,EAAE,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO;YACvB,IAAI,EAAE,MAAM;YACZ,EAAE,EAAE,MAAM;SACX,CAAC;QACF,YAAO,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;QAC9B,WAAM,GAAG,eAAe,CAAC,IAAI,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AAO,CAAC,QAAQ,CACjB,YAAY,EACZ,sBAAsB,EACtB,+DAA+D,EAC/D;gBACE,SAAS;gBACT,KAAK;gBACL,MAAM,EAAE,EAAC,GAAG,MAAA,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,0CAAE,EAAE,EAAE,GAAG,MAAA,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,0CAAE,EAAE,EAAC;aAC/E,CACJ,CAAC;YACF,OAAO,IAAI,CAAC;SACb;QAED,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAC9B,OAAO,OAAO,CAAC;IACjB,CAAC;CACF","sourcesContent":["import {createLogger, globalAlwatr} from '@alwatr/logger';\nimport {contextConsumer} from '@alwatr/signal';\nimport {dispatch} from '@alwatr/signal/core.js';\n\nimport type {Stringifyable, StringifyableRecord} from '@alwatr/type';\n\nglobalAlwatr.registeredList.push({\n name: '@alwatr/fsm',\n version: _ALWATR_VERSION_,\n});\n\nexport interface MachineConfig<TState extends string, TEventId extends string, TContext extends Stringifyable>\n extends StringifyableRecord {\n /**\n * Machine ID (It is used in the state change signal identifier, so it must be unique).\n */\n id: string;\n\n /**\n * Initial state.\n */\n initial: TState;\n\n /**\n * Initial context.\n */\n context: TContext;\n\n /**\n * States list\n */\n states: {\n [S in TState | '$all']: {\n /**\n * An object mapping eventId (keys) to state.\n */\n on: {\n [E in TEventId]?: TState | '$self';\n };\n };\n };\n}\n\nexport interface StateContext<TState extends string, TEventId extends string> {\n [T: string]: string;\n to: TState;\n from: TState | 'init';\n by: TEventId | 'INIT';\n}\n\nexport class FiniteStateMachine<\n TState extends string = string,\n TEventId extends string = string,\n TContext extends StringifyableRecord = StringifyableRecord\n> {\n state: StateContext<TState, TEventId> = {\n to: this.config.initial,\n from: 'init',\n by: 'INIT',\n };\n context = this.config.context;\n signal = contextConsumer.bind<StateContext<TState, TEventId>>('finite-state-machine-' + this.config.id);\n\n protected _logger = createLogger(`alwatr/fsm:${this.config.id}`);\n\n protected setState(to: TState, by: TEventId | 'INIT'): void {\n this.state = {\n to,\n from: this.signal.getValue()?.to ?? 'init',\n by,\n };\n dispatch<StateContext<TState, TEventId>>(this.signal.id, this.state, {debounce: 'NextCycle'});\n }\n\n constructor(public readonly config: Readonly<MachineConfig<TState, TEventId, TContext>>) {\n this._logger.logMethodArgs('constructor', config);\n dispatch<StateContext<TState, TEventId>>(this.signal.id, this.state, {debounce: 'NextCycle'});\n if (!config.states[config.initial]) {\n this._logger.error('constructor', 'invalid_initial_state', config);\n }\n }\n\n /**\n * Machine transition.\n */\n transition(event: TEventId, context?: Partial<TContext>): TState | null {\n const fromState = this.state.to;\n\n let toState: TState | '$self' | undefined =\n this.config.states[fromState]?.on?.[event] ?? this.config.states.$all?.on?.[event];\n\n if (toState === '$self') {\n toState = fromState;\n }\n\n this._logger.logMethodFull('transition', {fromState, event, context}, toState);\n\n if (context !== undefined) {\n this.context = {\n ...this.context,\n ...context,\n };\n }\n\n if (toState == null) {\n this._logger.incident(\n 'transition',\n 'invalid_target_state',\n 'Defined target state for this event not found in state config',\n {\n fromState,\n event,\n events: {...this.config.states.$all?.on, ...this.config.states[fromState]?.on},\n },\n );\n return null;\n }\n\n this.setState(toState, event);\n return toState;\n }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@alwatr/fsm",
3
+ "version": "0.30.0",
4
+ "description": "Managing invocations finite-state machines as actors written in tiny TypeScript module.",
5
+ "keywords": [
6
+ "state",
7
+ "finite",
8
+ "machine",
9
+ "typescript",
10
+ "esm",
11
+ "alwatr"
12
+ ],
13
+ "main": "core.js",
14
+ "type": "module",
15
+ "types": "core.d.ts",
16
+ "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
17
+ "license": "MIT",
18
+ "files": [
19
+ "**/*.{d.ts.map,d.ts,js.map,js,html,md}"
20
+ ],
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/AliMD/alwatr",
27
+ "directory": "core/fsm"
28
+ },
29
+ "homepage": "https://github.com/AliMD/alwatr/tree/main/core/fsm#readme",
30
+ "bugs": {
31
+ "url": "https://github.com/AliMD/alwatr/issues"
32
+ },
33
+ "dependencies": {
34
+ "@alwatr/logger": "^0.30.0",
35
+ "@alwatr/signal": "^0.30.0",
36
+ "@alwatr/type": "^0.30.0",
37
+ "tslib": "^2.5.0"
38
+ },
39
+ "gitHead": "36f55780ccdcb1acc07400b0cdb3fe7b0df56cca"
40
+ }