@axi-engine/states 0.2.1 → 0.3.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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -6
- package/dist/index.mjs +5 -2
- package/package.json +4 -5
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -25,7 +25,8 @@ __export(index_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
26
|
|
|
27
27
|
// src/state-machine.ts
|
|
28
|
-
var
|
|
28
|
+
var import_ensure = require("@axijs/ensure");
|
|
29
|
+
var import_emitter = require("@axijs/emitter");
|
|
29
30
|
var StateMachine = class {
|
|
30
31
|
/**
|
|
31
32
|
* @protected
|
|
@@ -46,7 +47,7 @@ var StateMachine = class {
|
|
|
46
47
|
* console.log(`State transitioned from ${from} to ${to}`);
|
|
47
48
|
* });
|
|
48
49
|
*/
|
|
49
|
-
onChange = new
|
|
50
|
+
onChange = new import_emitter.Emitter();
|
|
50
51
|
/**
|
|
51
52
|
* Gets the current state of the machine.
|
|
52
53
|
* @returns The current state identifier, or `undefined` if the machine has not been started.
|
|
@@ -72,7 +73,9 @@ var StateMachine = class {
|
|
|
72
73
|
* });
|
|
73
74
|
*/
|
|
74
75
|
register(state, handler) {
|
|
75
|
-
if ((0,
|
|
76
|
+
if ((0, import_ensure.isUndefined)(handler)) {
|
|
77
|
+
this.states.set(state, {});
|
|
78
|
+
} else if ((0, import_ensure.isFunction)(handler)) {
|
|
76
79
|
this.states.set(state, { onEnter: handler });
|
|
77
80
|
} else {
|
|
78
81
|
this.states.set(state, handler);
|
|
@@ -99,9 +102,9 @@ var StateMachine = class {
|
|
|
99
102
|
const oldState = this._state;
|
|
100
103
|
const oldStateConfig = this._state ? this.states.get(this._state) : void 0;
|
|
101
104
|
const newStateConfig = this.states.get(newState);
|
|
102
|
-
(0,
|
|
103
|
-
(0,
|
|
104
|
-
!(0,
|
|
105
|
+
(0, import_ensure.throwIfEmpty)(newStateConfig, `State ${String(newState)} is not registered.`);
|
|
106
|
+
(0, import_ensure.throwIf)(
|
|
107
|
+
!(0, import_ensure.isNullOrUndefined)(newStateConfig.allowedFrom) && !(0, import_ensure.isNullOrUndefined)(oldState) && !newStateConfig.allowedFrom.includes(oldState),
|
|
105
108
|
`Transition from ${String(oldState)} to ${String(newState)} is not allowed.`
|
|
106
109
|
);
|
|
107
110
|
await oldStateConfig?.onExit?.();
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/state-machine.ts
|
|
2
|
-
import {
|
|
2
|
+
import { isFunction, isNullOrUndefined, isUndefined, throwIf, throwIfEmpty } from "@axijs/ensure";
|
|
3
|
+
import { Emitter } from "@axijs/emitter";
|
|
3
4
|
var StateMachine = class {
|
|
4
5
|
/**
|
|
5
6
|
* @protected
|
|
@@ -46,7 +47,9 @@ var StateMachine = class {
|
|
|
46
47
|
* });
|
|
47
48
|
*/
|
|
48
49
|
register(state, handler) {
|
|
49
|
-
if (isUndefined(handler)
|
|
50
|
+
if (isUndefined(handler)) {
|
|
51
|
+
this.states.set(state, {});
|
|
52
|
+
} else if (isFunction(handler)) {
|
|
50
53
|
this.states.set(state, { onEnter: handler });
|
|
51
54
|
} else {
|
|
52
55
|
this.states.set(state, handler);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axi-engine/states",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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": {
|
|
@@ -38,10 +38,9 @@
|
|
|
38
38
|
"files": [
|
|
39
39
|
"dist"
|
|
40
40
|
],
|
|
41
|
-
"devDependencies": {
|
|
42
|
-
"@axi-engine/utils": "^0.2.1"
|
|
43
|
-
},
|
|
44
41
|
"peerDependencies": {
|
|
45
|
-
"@
|
|
42
|
+
"@axijs/emitter": "^1.0.0",
|
|
43
|
+
"@axijs/ensure": "^1.0.0",
|
|
44
|
+
"@axi-engine/utils": "^0.3.0"
|
|
46
45
|
}
|
|
47
46
|
}
|