feat: add methods for getting available states

This commit is contained in:
Dmitriy Pleshevskiy 2019-11-01 12:38:59 +03:00
parent e10701bc0b
commit c1835d270d
2 changed files with 10 additions and 12 deletions

View file

@ -1,6 +1,6 @@
{
"name": "it-fsm",
"version": "1.0.5",
"version": "1.0.6",
"description": "Simple finite state machine for nodejs",
"main": "./src/index.js",
"types": "./src/index.d.ts",

View file

@ -94,20 +94,18 @@ export class StateMachine {
}
public can(eventName: string): boolean {
const events = this._eventsByState[this._currentState];
if (!events) {
return false;
}
return Object.keys(events).includes(eventName);
return this.getAvailableActions().includes(eventName);
}
public canToState(stateName: StateType) {
const states = this._statesByState[this._currentState];
if (!states) {
return false;
}
return this.getAvailableStates().includes(stateName);
}
return states.includes(stateName);
public getAvailableStates(): StateType[] {
return this._statesByState[this._currentState] || []
}
public getAvailableActions(): string[] {
return Object.keys(this._eventsByState[this._currentState] || {});
}
}