Comparing version 2.2.1 to 2.2.2
35
index.js
import EventEmitter from 'eventemitter3'; | ||
const instance = Symbol('instance'); | ||
const abortCtrl = Symbol('abortCtrl'); | ||
const cacheResult = Symbol('cacheResult'); | ||
// 中间过渡状态 | ||
@@ -89,14 +91,14 @@ export class MiddleState { | ||
if (this.state === to) | ||
return this._cacheResult; | ||
return this[cacheResult]; | ||
if (Array.isArray(from)) { | ||
if (from.length == 0) { | ||
if (this.abortCtrl) | ||
this.abortCtrl.aborted = true; | ||
if (this[abortCtrl]) | ||
this[abortCtrl].aborted = true; | ||
} | ||
else if ((typeof this.state != "string" || !from.includes(this.state))) | ||
throw new FSMError(this._state, `${this.name} ${action} to ${to} faild: current state ${this._state} not in from config`); | ||
throw new FSMError(this._state, `${this.name} ${action} to ${to} failed: current state ${this._state} not in from config`); | ||
} | ||
else { | ||
if (from !== this.state) | ||
throw new FSMError(this._state, `${this.name} ${action} to ${to} faild: current state ${this._state} not from ${from}`); | ||
throw new FSMError(this._state, `${this.name} ${action} to ${to} failed: current state ${this._state} not from ${from}`); | ||
} | ||
@@ -106,17 +108,18 @@ const old = this.state; | ||
const abort = { aborted: false }; | ||
this.abortCtrl = abort; | ||
this[abortCtrl] = abort; | ||
try { | ||
this._cacheResult = await origin.apply(this, arg); | ||
this[cacheResult] = await origin.apply(this, arg); | ||
if (abort.aborted) { | ||
return this._cacheResult; | ||
return this[cacheResult]; | ||
} | ||
else { | ||
this.abortCtrl = void 0; | ||
this[abortCtrl] = void 0; | ||
} | ||
setState.call(this, to); | ||
return this._cacheResult; | ||
return this[cacheResult]; | ||
} | ||
catch (err) { | ||
setState.call(this, old, String(err)); | ||
throw new FSMError(this._state, `action '${action}' failed`, err instanceof Error ? err : new Error(String(err))); | ||
const msg = err instanceof Error ? err.message : String(err); | ||
setState.call(this, old, msg); | ||
throw new FSMError(this._state, `action '${action}' failed :${msg}`, err instanceof Error ? err : new Error(msg)); | ||
} | ||
@@ -133,3 +136,3 @@ }; | ||
if (!states.includes(this.state.toString())) | ||
throw new FSMError(this.state, `${this.name} ${action} faild: current state ${this.state} not in ${states}`); | ||
throw new FSMError(this.state, `${this.name} ${action} failed: current state ${this.state} not in ${states}`); | ||
return origin.apply(this, arg); | ||
@@ -146,3 +149,3 @@ }; | ||
if (states.includes(this.state.toString())) | ||
throw new FSMError(this.state, `${this.name} ${action} faild: current state ${this.state} in ${states}`); | ||
throw new FSMError(this.state, `${this.name} ${action} failed: current state ${this.state} in ${states}`); | ||
return origin.apply(this, arg); | ||
@@ -183,4 +186,4 @@ }; | ||
_state = FSM.INIT; | ||
_cacheResult; | ||
abortCtrl; | ||
[cacheResult]; | ||
[abortCtrl]; | ||
constructor(name, groupName) { | ||
@@ -187,0 +190,0 @@ super(); |
33
index.ts
@@ -6,2 +6,4 @@ import EventEmitter from 'eventemitter3'; | ||
const instance = Symbol('instance'); | ||
const abortCtrl = Symbol('abortCtrl'); | ||
const cacheResult = Symbol('cacheResult'); | ||
export type State = string | MiddleState; | ||
@@ -79,9 +81,9 @@ // 中间过渡状态 | ||
// if (typeof this.state != "string") throw new Error(`${this.name} ${action} to ${to} faild: last action ${this.state.action} to ${this.state.newState} not complete`); | ||
if (this.state === to) return this._cacheResult; | ||
if (this.state === to) return this[cacheResult]; | ||
if (Array.isArray(from)) { | ||
if (from.length == 0) { | ||
if (this.abortCtrl) this.abortCtrl.aborted = true; | ||
} else if ((typeof this.state != "string" || !from.includes(this.state))) throw new FSMError(this._state, `${this.name} ${action} to ${to} faild: current state ${this._state} not in from config`); | ||
if (this[abortCtrl]) this[abortCtrl].aborted = true; | ||
} else if ((typeof this.state != "string" || !from.includes(this.state))) throw new FSMError(this._state, `${this.name} ${action} to ${to} failed: current state ${this._state} not in from config`); | ||
} else { | ||
if (from !== this.state) throw new FSMError(this._state, `${this.name} ${action} to ${to} faild: current state ${this._state} not from ${from}`); | ||
if (from !== this.state) throw new FSMError(this._state, `${this.name} ${action} to ${to} failed: current state ${this._state} not from ${from}`); | ||
} | ||
@@ -91,15 +93,16 @@ const old = this.state; | ||
const abort = { aborted: false }; | ||
this.abortCtrl = abort; | ||
this[abortCtrl] = abort; | ||
try { | ||
this._cacheResult = await origin.apply(this, arg); | ||
this[cacheResult] = await origin.apply(this, arg); | ||
if (abort.aborted) { | ||
return this._cacheResult; | ||
return this[cacheResult]; | ||
} else { | ||
this.abortCtrl = void 0; | ||
this[abortCtrl] = void 0; | ||
} | ||
setState.call(this, to); | ||
return this._cacheResult; | ||
return this[cacheResult]; | ||
} catch (err) { | ||
setState.call(this, old, String(err)); | ||
throw new FSMError(this._state, `action '${action}' failed`, err instanceof Error ? err : new Error(String(err))); | ||
const msg = err instanceof Error ? err.message : String(err); | ||
setState.call(this, old, msg); | ||
throw new FSMError(this._state, `action '${action}' failed :${msg}`, err instanceof Error ? err : new Error(msg)); | ||
} | ||
@@ -115,3 +118,3 @@ }; | ||
descriptor.value = function (this: IAFSM, ...arg: any[]) { | ||
if (!states.includes(this.state.toString())) throw new FSMError(this.state, `${this.name} ${action} faild: current state ${this.state} not in ${states}`); | ||
if (!states.includes(this.state.toString())) throw new FSMError(this.state, `${this.name} ${action} failed: current state ${this.state} not in ${states}`); | ||
return origin.apply(this, arg); | ||
@@ -127,3 +130,3 @@ }; | ||
descriptor.value = async function (this: IAFSM, ...arg: any[]) { | ||
if (states.includes(this.state.toString())) throw new FSMError(this.state, `${this.name} ${action} faild: current state ${this.state} in ${states}`); | ||
if (states.includes(this.state.toString())) throw new FSMError(this.state, `${this.name} ${action} failed: current state ${this.state} in ${states}`); | ||
return origin.apply(this, arg); | ||
@@ -162,4 +165,4 @@ }; | ||
_state: State = FSM.INIT; | ||
_cacheResult: any; | ||
abortCtrl?: { aborted: boolean; }; | ||
[cacheResult]: any; | ||
[abortCtrl]?: { aborted: boolean; }; | ||
constructor(public name?: string, public groupName?: string) { | ||
@@ -166,0 +169,0 @@ super(); |
{ | ||
"name": "afsm", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "automatic finite state machine", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
3973310
18391