event-machine
Object Oriented ES6 state machine with promises supporting timeout transitions
Last updated 5 years ago by langpavel .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install event-machine 
SYNC missed versions from official npm registry.

event-machine

Object Oriented ES6 state machine with promises supporting timeout transitions

Install

npm install event-machine

Example:

import Promise from 'bluebird';
import State from '../src/state';
import StateMachine from '../src/stateMachine';


class LoggingState extends State {
    $onStateEnter(oldStateClass, oldStateInstance) {
        console.log(`${this} entered`);
    }

    $onStateLeave(newStateClass, newStateInstance) {
        console.log(`${this} leaved`);
    }

    $destroy() {
        console.log(`${this} destroyed`);
    }

    log(msg, ...args) {
        console.log(`${this} ${msg}`, ...args);
    }
}


class A extends LoggingState {
    $init() {
        this.i = 0;
    }

    $onStateEnter(oldStateClass, oldStateInstance) {
        this.i++;
        super.$onStateEnter(oldStateClass, oldStateInstance);
        this.$timeout(GameEnd, 5000);
    }

    $onStateLeave(newStateClass, newStateInstance) {
        return newStateClass !== A;
    }

    transitionA(data) {
        this.log('A -> A ...', data);
        return Promise.resolve(A);
    }

    transitionB() {
        this.log('A -> B ...');
        return B;
    }
}


class B extends LoggingState {
    transitionA(data) {
        this.log('B -> A ...', data);
        return A;
    }

    transitionB() {
        this.log('B -> B ...');
        return B;
    }
}


class GameEnd extends LoggingState {}


const machine = new StateMachine(A);

machine.transition('transitionA', 'data1').then((state) => {
    console.log(`Transition done, state ${state}`);
    return machine.transition('transitionB', 'data2');
}).then((state) => {
    console.log(`Transition done, state ${state}`);
    return machine.transition('transitionA', 'data3');
}).then((state) => {
    console.log(`Transition done, state ${state}`);
    return machine.transition('transitionA', 'data4');
});

Code above outputs this:

[state A {"i":1}] entered
[state A {"i":1,"$timeouted":0}] A -> A ... data1
[state A {"i":2,"$timeouted":0}] entered
Transition done, state [state A {"i":2,"$timeouted":0}]
[state A {"i":2,"$timeouted":0}] A -> B ...
[state B {}] entered
[state A {"i":2,"$timeouted":0}] destroyed
Transition done, state [state B {}]
[state B {}] B -> A ... data3
[state B {}] leaved
[state A {"i":1}] entered
Transition done, state [state A {"i":1,"$timeouted":0}]
[state A {"i":1,"$timeouted":0}] A -> A ... data4
[state A {"i":2,"$timeouted":0}] entered
... after 5 seconds
[state GameEnd {}] entered
[state A {"i":2,"$timeouted":1434098785003}] destroyed

Current Tags

  • 0.0.3                                ...           latest (5 years ago)

3 Versions

  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 4
Dependencies (1)
Dev Dependencies (6)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |