New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

event-machine

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-machine

Object Oriented ES6 state machine with promises supporting timeout transitions

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-71.43%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 13 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc