API Docs for:
Show:

Mixins.FSM Class

The FSM utility as a mixin, provides a finite state machine

Methods

addState

(
  • state
)
This

Allows to add a new state (see example for more info)

Parameters:

  • state Object

    The new state

Returns:

This:

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
)
This

Allows to add an array of new states all at once

Parameters:

  • states Array

    The new states

Returns:

This:

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
)
This

Transitions from the current state to the state specified by the given name of event to consume.

Parameters:

  • eventName String

    Name of the event to consume

Returns:

This:

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:

Undefined:

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:

String | Null:

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
)
This

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:

  • state Object

    The state to remove

Returns:

This:

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
    }
});