Code Monkey home page Code Monkey logo

evstate's Introduction

evstate

Event-based finite state machines.

Quickstart

import FSM from 'evstate';

const publishWorkflow = new FSM(
  {
    'draft': ['inReview'],
    'inReview': ['changesNeeded', 'approved'],
    'changesNeeded': ['inReview', 'approved'],
    'approved': ['draft', 'scheduled', 'published'],
    'scheduled': ['draft', 'published'],
    'published': null,
  },
  'draft'
);

// Add (optional) behavior when state changes happen.
publishWorkflow
  .on(FSM.ANY, (obj) => {
    obj.state = publishWorkflow.currentState;
  })
  .on('inReview', (obj) => {
    // Send a notification to a reviewer.
    emailReviewer(obj);
  })
  .on('changesNeeded', (obj) => {
    // Send a notification to the author to make changes.
    emailAuthor(obj);
  })
  .on('published', (obj) => {
    obj.publishDate = new Date();
  })
  .onError((msg) => {
    console.log(msg);
  });

// Create a draft.
// This is a plain old Object, but could be a Model or anything else!
const firstPost = {
  title: 'Hello World!',
  content: 'This is my very first PASTE to the website!',
  state: publishWorkflow.currentState,
  created: new Date(),
);

// Get a reviewer to look.
publishWorkflow.emit('inReview', firstPost);

// Oops! There was a typo!
publishWorkflow.emit('changesNeeded', firstPost);

// The author fixes it.
firstPost.content = 'This is my very first post to the website!';
publishWorkflow.emit('inReview', firstPost);

// Reviewer approves it!
publishWorkflow.emit('approved', firstPost);

// Somebody makes a mistake & tries to put it back in review!
// This logs a console error & DOES NOT save the change to the post!
publishWorkflow.emit('inReview', firstPost);

// Editor checks the list of stories ready to go...
// ...and says it's time to publish the first one!
publishWorkflow.emit('published', firstPost);

Installation

$ npm install evstate

Requirements

  • ES6 (or similar translation/polyfill)

Tests

$ npm test

Docs

$ jsdoc -r -d ~/Desktop/out --package package.json --readme README.md src

License

New BSD

evstate's People

Contributors

toastdriven avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.