@axi-engine/states 0.1.1 → 0.1.3

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
@@ -1,117 +1,3 @@
1
- import { Emitter } from '@axi-engine/utils';
2
-
3
- type StateHandler<P = void> = P extends void ? () => void | Promise<void> : (payload: P) => void | Promise<void>;
4
- interface StateHandlerConfig<T, P = void> {
5
- onEnter?: StateHandler<P>;
6
- onExit?: () => void | Promise<void>;
7
- allowedFrom?: T[];
8
- }
9
- type StateHandlerRegistration<T, P = void> = StateHandler<P> | StateHandlerConfig<T, P>;
10
-
11
- /**
12
- * A minimal, type-safe finite state machine.
13
- * It manages states, transitions, and associated lifecycle hooks (`onEnter`, `onExit`).
14
- *
15
- * @template T The type used for state identifiers (e.g., a string or an enum).
16
- * @template P The default payload type for state handlers. Can be overridden per state.
17
- * @example
18
- * enum PlayerState { Idle, Walk, Run }
19
- *
20
- * const playerFsm = new StateMachine<PlayerState>();
21
- *
22
- * playerFsm.register(PlayerState.Idle, () => console.log('Player is now idle.'));
23
- * playerFsm.register(PlayerState.Walk, () => console.log('Player is walking.'));
24
- *
25
- * async function start() {
26
- * await playerFsm.call(PlayerState.Idle);
27
- * await playerFsm.call(PlayerState.Walk);
28
- * }
29
- */
30
- declare class StateMachine<T, P = void> {
31
- /**
32
- * @protected
33
- * The internal representation of the current state.
34
- */
35
- protected _state?: T;
36
- /**
37
- * @protected
38
- * A map storing all registered state configurations.
39
- */
40
- protected states: Map<T, StateHandlerConfig<T, P> | undefined>;
41
- /**
42
- * Public emitter that fires an event whenever the state changes.
43
- * The event provides the old state, the new state, and the payload.
44
- * @see Emitter
45
- * @example
46
- * fsm.onChange.subscribe((from, to, payload) => {
47
- * console.log(`State transitioned from ${from} to ${to}`);
48
- * });
49
- */
50
- readonly onChange: Emitter<[from?: T | undefined, to?: T | undefined, payload?: P | undefined]>;
51
- /**
52
- * Gets the current state of the machine.
53
- * @returns The current state identifier, or `undefined` if the machine has not been started.
54
- */
55
- get state(): T | undefined;
56
- /**
57
- * Registers a state and its associated handler or configuration.
58
- * If a handler is already registered for the given state, it will be overwritten.
59
- *
60
- * @param state The identifier for the state to register.
61
- * @param handler A handler function (`onEnter`) or a full configuration object.
62
- * @example
63
- * // Simple registration
64
- * fsm.register(MyState.Idle, () => console.log('Entering Idle'));
65
- *
66
- * // Advanced registration
67
- * fsm.register(MyState.Walking, {
68
- * onEnter: () => console.log('Start walking animation'),
69
- * onExit: () => console.log('Stop walking animation'),
70
- * allowedFrom: [MyState.Idle]
71
- * });
72
- */
73
- register(state: T, handler?: StateHandlerRegistration<T, P>): void;
74
- /**
75
- * Transitions the machine to a new state.
76
- * This method is asynchronous to accommodate async `onEnter` and `onExit` handlers.
77
- * It will execute the `onExit` handler of the old state, then the `onEnter` handler of the new state.
78
- *
79
- * @param newState The identifier of the state to transition to.
80
- * @param payload An optional payload to pass to the new state's `onEnter` handler.
81
- * @returns A promise that resolves when the transition is complete.
82
- * @throws {Error} if the `newState` has not been registered.
83
- * @throws {Error} if the transition from the current state to the `newState` is not allowed by the `allowedFrom` rule.
84
- * @example
85
- * try {
86
- * await fsm.call(PlayerState.Run, { speed: 10 });
87
- * } catch (e) {
88
- * console.error('State transition failed:', e.message);
89
- * }
90
- */
91
- call(newState: T, payload?: P): Promise<void>;
92
- /**
93
- * Removes a single state configuration from the machine.
94
- * If the removed state is the currently active one, the machine's state will be reset to `undefined`.
95
- *
96
- * @param state The identifier of the state to remove.
97
- * @returns `true` if the state was found and removed, otherwise `false`.
98
- * @example
99
- * fsm.register(MyState.Temp, () => {});
100
- * // ...
101
- * const wasRemoved = fsm.unregister(MyState.Temp);
102
- * console.log('Temporary state removed:', wasRemoved);
103
- */
104
- unregister(state: T): boolean;
105
- /**
106
- * Removes all registered states and resets the machine to its initial, undefined state.
107
- * This does not clear `onChange` subscribers.
108
- * @example
109
- * fsm.register(MyState.One);
110
- * fsm.register(MyState.Two);
111
- * // ...
112
- * fsm.clear(); // The machine is now empty.
113
- */
114
- clear(): void;
115
- }
116
-
117
- export { type StateHandler, type StateHandlerConfig, type StateHandlerRegistration, StateMachine };
1
+ export * from './state-handler';
2
+ export * from './state-machine';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA"}
package/dist/index.js CHANGED
@@ -1,147 +1,3 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- StateMachine: () => StateMachine
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
-
27
- // src/state-machine.ts
28
- var import_utils = require("@axi-engine/utils");
29
- var StateMachine = class {
30
- /**
31
- * @protected
32
- * The internal representation of the current state.
33
- */
34
- _state;
35
- /**
36
- * @protected
37
- * A map storing all registered state configurations.
38
- */
39
- states = /* @__PURE__ */ new Map();
40
- /**
41
- * Public emitter that fires an event whenever the state changes.
42
- * The event provides the old state, the new state, and the payload.
43
- * @see Emitter
44
- * @example
45
- * fsm.onChange.subscribe((from, to, payload) => {
46
- * console.log(`State transitioned from ${from} to ${to}`);
47
- * });
48
- */
49
- onChange = new import_utils.Emitter();
50
- /**
51
- * Gets the current state of the machine.
52
- * @returns The current state identifier, or `undefined` if the machine has not been started.
53
- */
54
- get state() {
55
- return this._state;
56
- }
57
- /**
58
- * Registers a state and its associated handler or configuration.
59
- * If a handler is already registered for the given state, it will be overwritten.
60
- *
61
- * @param state The identifier for the state to register.
62
- * @param handler A handler function (`onEnter`) or a full configuration object.
63
- * @example
64
- * // Simple registration
65
- * fsm.register(MyState.Idle, () => console.log('Entering Idle'));
66
- *
67
- * // Advanced registration
68
- * fsm.register(MyState.Walking, {
69
- * onEnter: () => console.log('Start walking animation'),
70
- * onExit: () => console.log('Stop walking animation'),
71
- * allowedFrom: [MyState.Idle]
72
- * });
73
- */
74
- register(state, handler) {
75
- if ((0, import_utils.isUndefined)(handler) || typeof handler === "function") {
76
- this.states.set(state, { onEnter: handler });
77
- } else {
78
- this.states.set(state, handler);
79
- }
80
- }
81
- /**
82
- * Transitions the machine to a new state.
83
- * This method is asynchronous to accommodate async `onEnter` and `onExit` handlers.
84
- * It will execute the `onExit` handler of the old state, then the `onEnter` handler of the new state.
85
- *
86
- * @param newState The identifier of the state to transition to.
87
- * @param payload An optional payload to pass to the new state's `onEnter` handler.
88
- * @returns A promise that resolves when the transition is complete.
89
- * @throws {Error} if the `newState` has not been registered.
90
- * @throws {Error} if the transition from the current state to the `newState` is not allowed by the `allowedFrom` rule.
91
- * @example
92
- * try {
93
- * await fsm.call(PlayerState.Run, { speed: 10 });
94
- * } catch (e) {
95
- * console.error('State transition failed:', e.message);
96
- * }
97
- */
98
- async call(newState, payload) {
99
- const oldState = this._state;
100
- const oldStateConfig = this._state ? this.states.get(this._state) : void 0;
101
- const newStateConfig = this.states.get(newState);
102
- (0, import_utils.throwIfEmpty)(newStateConfig, `State ${String(newState)} is not registered.`);
103
- (0, import_utils.throwIf)(
104
- !(0, import_utils.isNullOrUndefined)(newStateConfig.allowedFrom) && !(0, import_utils.isNullOrUndefined)(oldState) && !newStateConfig.allowedFrom.includes(oldState),
105
- `Transition from ${String(oldState)} to ${String(newState)} is not allowed.`
106
- );
107
- await oldStateConfig?.onExit?.();
108
- this._state = newState;
109
- await newStateConfig.onEnter?.(payload);
110
- this.onChange.emit(oldState, newState, payload);
111
- }
112
- /**
113
- * Removes a single state configuration from the machine.
114
- * If the removed state is the currently active one, the machine's state will be reset to `undefined`.
115
- *
116
- * @param state The identifier of the state to remove.
117
- * @returns `true` if the state was found and removed, otherwise `false`.
118
- * @example
119
- * fsm.register(MyState.Temp, () => {});
120
- * // ...
121
- * const wasRemoved = fsm.unregister(MyState.Temp);
122
- * console.log('Temporary state removed:', wasRemoved);
123
- */
124
- unregister(state) {
125
- if (this._state === state) {
126
- this._state = void 0;
127
- }
128
- return this.states.delete(state);
129
- }
130
- /**
131
- * Removes all registered states and resets the machine to its initial, undefined state.
132
- * This does not clear `onChange` subscribers.
133
- * @example
134
- * fsm.register(MyState.One);
135
- * fsm.register(MyState.Two);
136
- * // ...
137
- * fsm.clear(); // The machine is now empty.
138
- */
139
- clear() {
140
- this.states.clear();
141
- this._state = void 0;
142
- }
143
- };
144
- // Annotate the CommonJS export names for ESM import in node:
145
- 0 && (module.exports = {
146
- StateMachine
147
- });
1
+ export * from './state-handler';
2
+ export * from './state-machine';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,iBAAiB,CAAA"}
@@ -0,0 +1,8 @@
1
+ export type StateHandler<P = void> = P extends void ? () => void | Promise<void> : (payload: P) => void | Promise<void>;
2
+ export interface StateHandlerConfig<T, P = void> {
3
+ onEnter?: StateHandler<P>;
4
+ onExit?: () => void | Promise<void>;
5
+ allowedFrom?: T[];
6
+ }
7
+ export type StateHandlerRegistration<T, P = void> = StateHandler<P> | StateHandlerConfig<T, P>;
8
+ //# sourceMappingURL=state-handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-handler.d.ts","sourceRoot":"","sources":["../src/state-handler.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,IAAI,IAC/B,CAAC,SAAS,IAAI,GACZ,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GACxB,CAAC,OAAO,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAG3C,MAAM,WAAW,kBAAkB,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI;IAC7C,OAAO,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC;CACnB;AAED,MAAM,MAAM,wBAAwB,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,IAAI,YAAY,CAAC,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=state-handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-handler.js","sourceRoot":"","sources":["../src/state-handler.ts"],"names":[],"mappings":""}
@@ -0,0 +1,108 @@
1
+ import { StateHandlerConfig, StateHandlerRegistration } from "./state-handler";
2
+ import { Emitter } from '@axi-engine/utils';
3
+ /**
4
+ * A minimal, type-safe finite state machine.
5
+ * It manages states, transitions, and associated lifecycle hooks (`onEnter`, `onExit`).
6
+ *
7
+ * @template T The type used for state identifiers (e.g., a string or an enum).
8
+ * @template P The default payload type for state handlers. Can be overridden per state.
9
+ * @example
10
+ * enum PlayerState { Idle, Walk, Run }
11
+ *
12
+ * const playerFsm = new StateMachine<PlayerState>();
13
+ *
14
+ * playerFsm.register(PlayerState.Idle, () => console.log('Player is now idle.'));
15
+ * playerFsm.register(PlayerState.Walk, () => console.log('Player is walking.'));
16
+ *
17
+ * async function start() {
18
+ * await playerFsm.call(PlayerState.Idle);
19
+ * await playerFsm.call(PlayerState.Walk);
20
+ * }
21
+ */
22
+ export declare class StateMachine<T, P = void> {
23
+ /**
24
+ * @protected
25
+ * The internal representation of the current state.
26
+ */
27
+ protected _state?: T;
28
+ /**
29
+ * @protected
30
+ * A map storing all registered state configurations.
31
+ */
32
+ protected states: Map<T, StateHandlerConfig<T, P> | undefined>;
33
+ /**
34
+ * Public emitter that fires an event whenever the state changes.
35
+ * The event provides the old state, the new state, and the payload.
36
+ * @see Emitter
37
+ * @example
38
+ * fsm.onChange.subscribe((from, to, payload) => {
39
+ * console.log(`State transitioned from ${from} to ${to}`);
40
+ * });
41
+ */
42
+ readonly onChange: Emitter<[from?: T | undefined, to?: T | undefined, payload?: P | undefined]>;
43
+ /**
44
+ * Gets the current state of the machine.
45
+ * @returns The current state identifier, or `undefined` if the machine has not been started.
46
+ */
47
+ get state(): T | undefined;
48
+ /**
49
+ * Registers a state and its associated handler or configuration.
50
+ * If a handler is already registered for the given state, it will be overwritten.
51
+ *
52
+ * @param state The identifier for the state to register.
53
+ * @param handler A handler function (`onEnter`) or a full configuration object.
54
+ * @example
55
+ * // Simple registration
56
+ * fsm.register(MyState.Idle, () => console.log('Entering Idle'));
57
+ *
58
+ * // Advanced registration
59
+ * fsm.register(MyState.Walking, {
60
+ * onEnter: () => console.log('Start walking animation'),
61
+ * onExit: () => console.log('Stop walking animation'),
62
+ * allowedFrom: [MyState.Idle]
63
+ * });
64
+ */
65
+ register(state: T, handler?: StateHandlerRegistration<T, P>): void;
66
+ /**
67
+ * Transitions the machine to a new state.
68
+ * This method is asynchronous to accommodate async `onEnter` and `onExit` handlers.
69
+ * It will execute the `onExit` handler of the old state, then the `onEnter` handler of the new state.
70
+ *
71
+ * @param newState The identifier of the state to transition to.
72
+ * @param payload An optional payload to pass to the new state's `onEnter` handler.
73
+ * @returns A promise that resolves when the transition is complete.
74
+ * @throws {Error} if the `newState` has not been registered.
75
+ * @throws {Error} if the transition from the current state to the `newState` is not allowed by the `allowedFrom` rule.
76
+ * @example
77
+ * try {
78
+ * await fsm.call(PlayerState.Run, { speed: 10 });
79
+ * } catch (e) {
80
+ * console.error('State transition failed:', e.message);
81
+ * }
82
+ */
83
+ call(newState: T, payload?: P): Promise<void>;
84
+ /**
85
+ * Removes a single state configuration from the machine.
86
+ * If the removed state is the currently active one, the machine's state will be reset to `undefined`.
87
+ *
88
+ * @param state The identifier of the state to remove.
89
+ * @returns `true` if the state was found and removed, otherwise `false`.
90
+ * @example
91
+ * fsm.register(MyState.Temp, () => {});
92
+ * // ...
93
+ * const wasRemoved = fsm.unregister(MyState.Temp);
94
+ * console.log('Temporary state removed:', wasRemoved);
95
+ */
96
+ unregister(state: T): boolean;
97
+ /**
98
+ * Removes all registered states and resets the machine to its initial, undefined state.
99
+ * This does not clear `onChange` subscribers.
100
+ * @example
101
+ * fsm.register(MyState.One);
102
+ * fsm.register(MyState.Two);
103
+ * // ...
104
+ * fsm.clear(); // The machine is now empty.
105
+ */
106
+ clear(): void;
107
+ }
108
+ //# sourceMappingURL=state-machine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-machine.d.ts","sourceRoot":"","sources":["../src/state-machine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAE,wBAAwB,EAAC,MAAM,iBAAiB,CAAC;AAC7E,OAAO,EAAC,OAAO,EAAwD,MAAM,mBAAmB,CAAC;AAGjG;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,YAAY,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI;IACnC;;;OAGG;IACH,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAErB;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,CAAa;IAE3E;;;;;;;;OAQG;IACH,QAAQ,CAAC,QAAQ,+EAAkD;IAEnE;;;OAGG;IACH,IAAI,KAAK,IAAI,CAAC,GAAG,SAAS,CAEzB;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI;IAQlE;;;;;;;;;;;;;;;;OAgBG;IACG,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBnD;;;;;;;;;;;OAWG;IACH,UAAU,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO;IAO7B;;;;;;;;OAQG;IACH,KAAK,IAAI,IAAI;CAId"}
@@ -0,0 +1,136 @@
1
+ import { Emitter, isNullOrUndefined, isUndefined, throwIf, throwIfEmpty } from '@axi-engine/utils';
2
+ /**
3
+ * A minimal, type-safe finite state machine.
4
+ * It manages states, transitions, and associated lifecycle hooks (`onEnter`, `onExit`).
5
+ *
6
+ * @template T The type used for state identifiers (e.g., a string or an enum).
7
+ * @template P The default payload type for state handlers. Can be overridden per state.
8
+ * @example
9
+ * enum PlayerState { Idle, Walk, Run }
10
+ *
11
+ * const playerFsm = new StateMachine<PlayerState>();
12
+ *
13
+ * playerFsm.register(PlayerState.Idle, () => console.log('Player is now idle.'));
14
+ * playerFsm.register(PlayerState.Walk, () => console.log('Player is walking.'));
15
+ *
16
+ * async function start() {
17
+ * await playerFsm.call(PlayerState.Idle);
18
+ * await playerFsm.call(PlayerState.Walk);
19
+ * }
20
+ */
21
+ export class StateMachine {
22
+ /**
23
+ * @protected
24
+ * The internal representation of the current state.
25
+ */
26
+ _state;
27
+ /**
28
+ * @protected
29
+ * A map storing all registered state configurations.
30
+ */
31
+ states = new Map();
32
+ /**
33
+ * Public emitter that fires an event whenever the state changes.
34
+ * The event provides the old state, the new state, and the payload.
35
+ * @see Emitter
36
+ * @example
37
+ * fsm.onChange.subscribe((from, to, payload) => {
38
+ * console.log(`State transitioned from ${from} to ${to}`);
39
+ * });
40
+ */
41
+ onChange = new Emitter();
42
+ /**
43
+ * Gets the current state of the machine.
44
+ * @returns The current state identifier, or `undefined` if the machine has not been started.
45
+ */
46
+ get state() {
47
+ return this._state;
48
+ }
49
+ /**
50
+ * Registers a state and its associated handler or configuration.
51
+ * If a handler is already registered for the given state, it will be overwritten.
52
+ *
53
+ * @param state The identifier for the state to register.
54
+ * @param handler A handler function (`onEnter`) or a full configuration object.
55
+ * @example
56
+ * // Simple registration
57
+ * fsm.register(MyState.Idle, () => console.log('Entering Idle'));
58
+ *
59
+ * // Advanced registration
60
+ * fsm.register(MyState.Walking, {
61
+ * onEnter: () => console.log('Start walking animation'),
62
+ * onExit: () => console.log('Stop walking animation'),
63
+ * allowedFrom: [MyState.Idle]
64
+ * });
65
+ */
66
+ register(state, handler) {
67
+ if (isUndefined(handler) || typeof handler === 'function') {
68
+ this.states.set(state, { onEnter: handler });
69
+ }
70
+ else {
71
+ this.states.set(state, handler);
72
+ }
73
+ }
74
+ /**
75
+ * Transitions the machine to a new state.
76
+ * This method is asynchronous to accommodate async `onEnter` and `onExit` handlers.
77
+ * It will execute the `onExit` handler of the old state, then the `onEnter` handler of the new state.
78
+ *
79
+ * @param newState The identifier of the state to transition to.
80
+ * @param payload An optional payload to pass to the new state's `onEnter` handler.
81
+ * @returns A promise that resolves when the transition is complete.
82
+ * @throws {Error} if the `newState` has not been registered.
83
+ * @throws {Error} if the transition from the current state to the `newState` is not allowed by the `allowedFrom` rule.
84
+ * @example
85
+ * try {
86
+ * await fsm.call(PlayerState.Run, { speed: 10 });
87
+ * } catch (e) {
88
+ * console.error('State transition failed:', e.message);
89
+ * }
90
+ */
91
+ async call(newState, payload) {
92
+ const oldState = this._state;
93
+ const oldStateConfig = this._state ? this.states.get(this._state) : undefined;
94
+ const newStateConfig = this.states.get(newState);
95
+ throwIfEmpty(newStateConfig, `State ${String(newState)} is not registered.`);
96
+ throwIf(!isNullOrUndefined(newStateConfig.allowedFrom) &&
97
+ !isNullOrUndefined(oldState) &&
98
+ !newStateConfig.allowedFrom.includes(oldState), `Transition from ${String(oldState)} to ${String(newState)} is not allowed.`);
99
+ await oldStateConfig?.onExit?.();
100
+ this._state = newState;
101
+ await newStateConfig.onEnter?.(payload);
102
+ this.onChange.emit(oldState, newState, payload);
103
+ }
104
+ /**
105
+ * Removes a single state configuration from the machine.
106
+ * If the removed state is the currently active one, the machine's state will be reset to `undefined`.
107
+ *
108
+ * @param state The identifier of the state to remove.
109
+ * @returns `true` if the state was found and removed, otherwise `false`.
110
+ * @example
111
+ * fsm.register(MyState.Temp, () => {});
112
+ * // ...
113
+ * const wasRemoved = fsm.unregister(MyState.Temp);
114
+ * console.log('Temporary state removed:', wasRemoved);
115
+ */
116
+ unregister(state) {
117
+ if (this._state === state) {
118
+ this._state = undefined;
119
+ }
120
+ return this.states.delete(state);
121
+ }
122
+ /**
123
+ * Removes all registered states and resets the machine to its initial, undefined state.
124
+ * This does not clear `onChange` subscribers.
125
+ * @example
126
+ * fsm.register(MyState.One);
127
+ * fsm.register(MyState.Two);
128
+ * // ...
129
+ * fsm.clear(); // The machine is now empty.
130
+ */
131
+ clear() {
132
+ this.states.clear();
133
+ this._state = undefined;
134
+ }
135
+ }
136
+ //# sourceMappingURL=state-machine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-machine.js","sourceRoot":"","sources":["../src/state-machine.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAC,MAAM,mBAAmB,CAAC;AAGjG;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,YAAY;IACvB;;;OAGG;IACO,MAAM,CAAK;IAErB;;;OAGG;IACO,MAAM,GAAiD,IAAI,GAAG,EAAE,CAAC;IAE3E;;;;;;;;OAQG;IACM,QAAQ,GAAG,IAAI,OAAO,EAAmC,CAAC;IAEnE;;;OAGG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,KAAQ,EAAE,OAAwC;QACzD,IAAI,WAAW,CAAC,OAAO,CAAC,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;YAC1D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,EAAC,OAAO,EAAE,OAAO,EAAC,CAAC,CAAC;QAC7C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,IAAI,CAAC,QAAW,EAAE,OAAW;QACjC,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC;QAC7B,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC9E,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAEjD,YAAY,CAAC,cAAc,EAAE,SAAS,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;QAE7E,OAAO,CACL,CAAC,iBAAiB,CAAC,cAAc,CAAC,WAAW,CAAC;YAC9C,CAAC,iBAAiB,CAAC,QAAQ,CAAC;YAC5B,CAAC,cAAc,CAAC,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAC9C,mBAAmB,MAAM,CAAC,QAAQ,CAAC,OAAO,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAC7E,CAAC;QAEF,MAAM,cAAc,EAAE,MAAM,EAAE,EAAE,CAAC;QAEjC,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC;QAEvB,MAAO,cAAc,CAAC,OAAiD,EAAE,CAAC,OAAO,CAAC,CAAC;QAEnF,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IAED;;;;;;;;;;;OAWG;IACH,UAAU,CAAC,KAAQ;QACjB,IAAI,IAAI,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;YAC1B,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QAC1B,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,CAAC;CACF"}
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "@axi-engine/states",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "A minimal, type-safe state machine for the Axi Engine, designed for managing game logic and component states with a simple API.",
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/axijs/engine.git",
9
+ "directory": "packages/states"
10
+ },
6
11
  "keywords": [
7
12
  "axi-engine",
8
13
  "typescript",