@axi-engine/states 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +2 -4
- package/dist/index.mjs +2 -4
- package/package.json +4 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
1
|
import { Emitter } from '@axijs/emitter';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Represents a lifecycle handler function for a state.
|
|
5
|
+
* If a payload type `P` is provided and is not `void`, the handler receives the payload.
|
|
6
|
+
* Supports both synchronous and asynchronous execution.
|
|
7
|
+
*
|
|
8
|
+
* @template P The type of the payload passed to the handler.
|
|
9
|
+
*/
|
|
3
10
|
type StateHandler<P = void> = P extends void ? () => void | Promise<void> : (payload: P) => void | Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Configuration object for defining a state's behavior and transition rules.
|
|
13
|
+
*
|
|
14
|
+
* @template T The type used for state identifiers.
|
|
15
|
+
* @template P The type of the payload passed to the `onEnter` handler.
|
|
16
|
+
*/
|
|
4
17
|
interface StateHandlerConfig<T, P = void> {
|
|
18
|
+
/**
|
|
19
|
+
* Hook executed when transitioning into this state.
|
|
20
|
+
*/
|
|
5
21
|
onEnter?: StateHandler<P>;
|
|
22
|
+
/**
|
|
23
|
+
* Hook executed when transitioning out of this state.
|
|
24
|
+
*/
|
|
6
25
|
onExit?: () => void | Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Optional list of states from which a transition to this state is permitted.
|
|
28
|
+
* If undefined, the state can be entered from any state.
|
|
29
|
+
*/
|
|
7
30
|
allowedFrom?: T[];
|
|
8
31
|
}
|
|
9
32
|
type StateHandlerRegistration<T, P = void> = StateHandler<P> | StateHandlerConfig<T, P>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
1
|
import { Emitter } from '@axijs/emitter';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Represents a lifecycle handler function for a state.
|
|
5
|
+
* If a payload type `P` is provided and is not `void`, the handler receives the payload.
|
|
6
|
+
* Supports both synchronous and asynchronous execution.
|
|
7
|
+
*
|
|
8
|
+
* @template P The type of the payload passed to the handler.
|
|
9
|
+
*/
|
|
3
10
|
type StateHandler<P = void> = P extends void ? () => void | Promise<void> : (payload: P) => void | Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Configuration object for defining a state's behavior and transition rules.
|
|
13
|
+
*
|
|
14
|
+
* @template T The type used for state identifiers.
|
|
15
|
+
* @template P The type of the payload passed to the `onEnter` handler.
|
|
16
|
+
*/
|
|
4
17
|
interface StateHandlerConfig<T, P = void> {
|
|
18
|
+
/**
|
|
19
|
+
* Hook executed when transitioning into this state.
|
|
20
|
+
*/
|
|
5
21
|
onEnter?: StateHandler<P>;
|
|
22
|
+
/**
|
|
23
|
+
* Hook executed when transitioning out of this state.
|
|
24
|
+
*/
|
|
6
25
|
onExit?: () => void | Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Optional list of states from which a transition to this state is permitted.
|
|
28
|
+
* If undefined, the state can be entered from any state.
|
|
29
|
+
*/
|
|
7
30
|
allowedFrom?: T[];
|
|
8
31
|
}
|
|
9
32
|
type StateHandlerRegistration<T, P = void> = StateHandler<P> | StateHandlerConfig<T, P>;
|
package/dist/index.js
CHANGED
|
@@ -73,9 +73,7 @@ var StateMachine = class {
|
|
|
73
73
|
* });
|
|
74
74
|
*/
|
|
75
75
|
register(state, handler) {
|
|
76
|
-
if ((0, import_ensure.isUndefined)(handler)) {
|
|
77
|
-
this.states.set(state, {});
|
|
78
|
-
} else if ((0, import_ensure.isFunction)(handler)) {
|
|
76
|
+
if ((0, import_ensure.isUndefined)(handler) || (0, import_ensure.isFunction)(handler)) {
|
|
79
77
|
this.states.set(state, { onEnter: handler });
|
|
80
78
|
} else {
|
|
81
79
|
this.states.set(state, handler);
|
|
@@ -100,7 +98,7 @@ var StateMachine = class {
|
|
|
100
98
|
*/
|
|
101
99
|
async call(newState, payload) {
|
|
102
100
|
const oldState = this._state;
|
|
103
|
-
const oldStateConfig = this._state ? this.states.get(this._state)
|
|
101
|
+
const oldStateConfig = (0, import_ensure.isUndefined)(this._state) ? void 0 : this.states.get(this._state);
|
|
104
102
|
const newStateConfig = this.states.get(newState);
|
|
105
103
|
(0, import_ensure.throwIfEmpty)(newStateConfig, `State ${String(newState)} is not registered.`);
|
|
106
104
|
(0, import_ensure.throwIf)(
|
package/dist/index.mjs
CHANGED
|
@@ -47,9 +47,7 @@ var StateMachine = class {
|
|
|
47
47
|
* });
|
|
48
48
|
*/
|
|
49
49
|
register(state, handler) {
|
|
50
|
-
if (isUndefined(handler)) {
|
|
51
|
-
this.states.set(state, {});
|
|
52
|
-
} else if (isFunction(handler)) {
|
|
50
|
+
if (isUndefined(handler) || isFunction(handler)) {
|
|
53
51
|
this.states.set(state, { onEnter: handler });
|
|
54
52
|
} else {
|
|
55
53
|
this.states.set(state, handler);
|
|
@@ -74,7 +72,7 @@ var StateMachine = class {
|
|
|
74
72
|
*/
|
|
75
73
|
async call(newState, payload) {
|
|
76
74
|
const oldState = this._state;
|
|
77
|
-
const oldStateConfig = this._state ? this.states.get(this._state)
|
|
75
|
+
const oldStateConfig = isUndefined(this._state) ? void 0 : this.states.get(this._state);
|
|
78
76
|
const newStateConfig = this.states.get(newState);
|
|
79
77
|
throwIfEmpty(newStateConfig, `State ${String(newState)} is not registered.`);
|
|
80
78
|
throwIf(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axi-engine/states",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
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
6
|
"repository": {
|
|
@@ -32,8 +32,9 @@
|
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "tsup",
|
|
35
|
-
"
|
|
36
|
-
"test": "
|
|
35
|
+
"prebuild": "npm test",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"docs": "typedoc src/index.ts --out docs/api --options ../../typedoc.json"
|
|
37
38
|
},
|
|
38
39
|
"files": [
|
|
39
40
|
"dist"
|