Comparing version 2.4.4 to 2.4.5
@@ -6,4 +6,4 @@ import EventEmitter from 'eventemitter3'; | ||
declare const cacheResult: unique symbol; | ||
export type State = string | MiddleState; | ||
type contextType = string | object; | ||
export declare type State = string | MiddleState; | ||
declare type contextType = string | object; | ||
export interface ChangeOption { | ||
@@ -10,0 +10,0 @@ ignoreError?: boolean; |
80
index.js
@@ -83,19 +83,12 @@ import EventEmitter from 'eventemitter3'; | ||
setState.call(fsm, middle); | ||
try { | ||
const result = origin.apply(this, arg); | ||
const success = (result) => { | ||
var _a; | ||
fsm[cacheResult] = result; | ||
if (!middle.aborted) { | ||
setState.call(fsm, to); | ||
(_a = opt.success) === null || _a === void 0 ? void 0 : _a.call(this, fsm[cacheResult]); | ||
} | ||
return result; | ||
}; | ||
if (thenAble(result)) | ||
return result.then(success); | ||
else | ||
return success(result); | ||
} | ||
catch (err) { | ||
const success = (result) => { | ||
var _a; | ||
fsm[cacheResult] = result; | ||
if (!middle.aborted) { | ||
setState.call(fsm, to); | ||
(_a = opt.success) === null || _a === void 0 ? void 0 : _a.call(this, fsm[cacheResult]); | ||
} | ||
return result; | ||
}; | ||
const failed = (err) => { | ||
const msg = err instanceof Error ? err.message : String(err); | ||
@@ -109,3 +102,13 @@ setState.call(fsm, old, err); | ||
throw err; | ||
}; | ||
try { | ||
const result = origin.apply(this, arg); | ||
if (thenAble(result)) | ||
return result.then(success).catch(failed); | ||
else | ||
return success(result); | ||
} | ||
catch (err) { | ||
failed(err); | ||
} | ||
}; | ||
@@ -185,3 +188,23 @@ }; | ||
} | ||
class FSM extends EventEmitter { | ||
export class FSM extends EventEmitter { | ||
constructor(name, groupName, prototype) { | ||
super(); | ||
this.name = name; | ||
this.groupName = groupName; | ||
this._state = FSM.INIT; | ||
if (!name) | ||
name = Date.now().toString(36); | ||
if (!prototype) | ||
prototype = Object.getPrototypeOf(this); | ||
else | ||
Object.setPrototypeOf(this, prototype); | ||
if (!groupName) | ||
this.groupName = this.constructor.name; | ||
const names = prototype[instance]; | ||
if (!names) | ||
prototype[instance] = { name: this.name, count: 0 }; | ||
else | ||
this.name = names.name + "-" + names.count++; | ||
this.updateDevTools({ diagram: this.stateDiagram }); | ||
} | ||
get stateDiagram() { | ||
@@ -255,22 +278,2 @@ const protoType = Object.getPrototypeOf(this); | ||
} | ||
constructor(name, groupName, prototype) { | ||
super(); | ||
this.name = name; | ||
this.groupName = groupName; | ||
this._state = FSM.INIT; | ||
if (!name) | ||
name = Date.now().toString(36); | ||
if (!prototype) | ||
prototype = Object.getPrototypeOf(this); | ||
else | ||
Object.setPrototypeOf(this, prototype); | ||
if (!groupName) | ||
this.groupName = this.constructor.name; | ||
const names = prototype[instance]; | ||
if (!names) | ||
prototype[instance] = { name: this.name, count: 0 }; | ||
else | ||
this.name = names.name + "-" + names.count++; | ||
this.updateDevTools({ diagram: this.stateDiagram }); | ||
} | ||
updateDevTools(payload = {}) { | ||
@@ -293,3 +296,2 @@ sendDevTools(FSM.UPDATEAFSM, Object.assign({ name: this.name, group: this.groupName }, payload)); | ||
FSM.instances2 = new WeakMap(); | ||
export { FSM }; | ||
//# sourceMappingURL=index.js.map |
29
index.ts
@@ -90,15 +90,11 @@ import EventEmitter from 'eventemitter3'; | ||
setState.call(fsm, middle); | ||
try { | ||
const result = origin.apply(this, arg); | ||
const success = (result: any) => { | ||
fsm[cacheResult] = result; | ||
if (!middle.aborted) { | ||
setState.call(fsm, to); | ||
opt.success?.call(this, fsm[cacheResult]); | ||
} | ||
return result; | ||
}; | ||
if (thenAble(result)) return result.then(success); | ||
else return success(result); | ||
} catch (err) { | ||
const success = (result: any) => { | ||
fsm[cacheResult] = result; | ||
if (!middle.aborted) { | ||
setState.call(fsm, to); | ||
opt.success?.call(this, fsm[cacheResult]); | ||
} | ||
return result; | ||
}; | ||
const failed = (err: unknown) => { | ||
const msg = err instanceof Error ? err.message : String(err); | ||
@@ -109,2 +105,9 @@ setState.call(fsm, old, err); | ||
else throw err; | ||
}; | ||
try { | ||
const result = origin.apply(this, arg); | ||
if (thenAble(result)) return result.then(success).catch(failed); | ||
else return success(result); | ||
} catch (err) { | ||
failed(err); | ||
} | ||
@@ -111,0 +114,0 @@ }; |
{ | ||
"name": "afsm", | ||
"version": "2.4.4", | ||
"version": "2.4.5", | ||
"description": "automatic finite state machine", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "tsc --watch", | ||
"build": "tsc --target es2017", | ||
"docs:dev": "vitepress dev docs", | ||
@@ -11,0 +11,0 @@ "docs:build": "vitepress build docs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
4089857
70
22048