feat: add methods for getting available states
This commit is contained in:
parent
e10701bc0b
commit
c1835d270d
2 changed files with 10 additions and 12 deletions
|
@ -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",
|
||||
|
|
20
src/index.ts
20
src/index.ts
|
@ -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] || {});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue