Mixins.FSM Class
The FSM utility as a mixin, provides a finite state machine
Item Index
Methods
addState
-
state
Allows to add a new state (see example for more info)
Parameters:
-
stateObjectThe new state
Returns:
The instance
Example:
import FSM from './mixins/fsm';
const MyClass = FSM(class {
constructor() {
// New states MyClass can be in are added
this.addState({
name: 'init',
events: {next: 'after'}
}).
addState({
name: 'after',
events: {next: 'last', prev: 'init'}
}).
addState({
name: 'last',
events: {prev: 'after'}
});
}
});
addStates
-
states
Allows to add an array of new states all at once
Parameters:
-
statesArrayThe new states
Returns:
The instance
Example:
import FSM from './mixins/fsm';
const MyStates = [
{name: 'init', events: {next: 'after'}},
{name: 'after', events: {next: 'last', prev: 'init'}},
{name: 'last', events: {prev: 'after'}}
];
const MyClass = FSM(class {
constructor() {
// New states MyClass can be in are added
this.addStates(MyStates);
}
});
consume
-
eventName
Transitions from the current state to the state specified by the given name of event to consume.
Parameters:
-
eventNameStringName of the event to consume
Returns:
The instance
Example:
import FSM from './mixins/fsm';
const MyStates = [
{name: 'init', events: {next: 'after'}},
{name: 'after', events: {next: 'last', prev: 'init'}},
{name: 'last', events: {prev: 'after'}}
];
const MyClass = FSM(class {
constructor() {
// New states MyClass can be in are added
this.addStates(MyStates);
}
});
let myInstance = new MyClass();
myInstance.getState(); // -> 'init'
myInstance.consume('next').consume('next').getState(); // -> 'last'
destroyStates
()
Undefined
Destroys all states associated with the FSM
Returns:
Nothing
Example:
import FSM from './mixins/fsm';
const MyStates = [
{name: 'init', events: {next: 'after'}},
{name: 'after', events: {next: 'last', prev: 'init'}},
{name: 'last', events: {prev: 'after'}}
];
const MyClass = FSM(class {
constructor() {
// New states MyClass can be in are added
this.addStates(MyStates);
}
});
let myInstance = new MyClass();
myInstance.destroyStates();
getState
()
String | Null
Returns the name of the current state or null if no state is set
Returns:
Name of the current state or null
Example:
import FSM from './mixins/fsm';
const MyStates = [
{name: 'init', events: {next: 'after'}},
{name: 'after', events: {next: 'last', prev: 'init'}},
{name: 'last', events: {prev: 'after'}}
];
const MyClass = FSM(class {
constructor() {
// New states MyClass can be in are added
this.addStates(MyStates);
}
});
let myInstance = new MyClass();
myInstance.getState(); // -> 'init'
removeState
-
state
Removes a state from the FSM, if it is not the active one. Please note that you have to take care to preserve a valid path of events to consume, or otherwise the FSM will be trapped in a single state (which may be fine if that is your intention)
Parameters:
-
stateObjectThe state to remove
Returns:
The instance
Example:
import FSM from './mixins/fsm';
const MyStates = [
{name: 'init', events: {next: 'after'}},
{name: 'after', events: {next: 'last', prev: 'init'}},
{name: 'last', events: {prev: 'after'}}
];
const MyClass = FSM(class {
constructor() {
// New states MyClass can be in are added
this.addStates(MyStates);
this.removeState(MyStates[2]); // remove the 'last' state
}
});
