kjirou / downspout

A framework to serialize the execution of event handlers for disorderly event emitting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

downspout

npm version CircleCI

README: ( English | 日本語 )

A framework to serialize the execution of event handlers for disorderly event emitting

Functions of this module

  • Provide a framework for describing event handlers.
  • Allow one event handler to execute as transaction processing.
  • Queue the execution of event handlers.

Installation

npm install --save downspout

Please use browserify (or webpack) when using with browser.

Usage

Overview

const Downspout = require('downspout');

const state = {
  counter: 0,
};

const useCases = {
  increment: () => {
    return Promise.resolve({
      type: 'INCREMENT',
    });
  },
  decrement: () => {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          type: 'DECREMENT',
        });
      }, 1000);
    });
  },
};

const downspout = new Downspout(useCases);

downspout.on('execution:resolved', ({ result: action }) => {
  switch (action.type) {
    case 'INCREMENT':
      state.counter += 1;
      break;
    case 'DECREMENT':
      state.counter -= 1;
      break;
  }

  process.stdout.write(`${ state.counter }\n`);
});

downspout.execute('increment');  // -> "1"
downspout.execute('increment');  // -> "2"
downspout.execute('decrement');  // -> "1"
downspout.execute('increment');  // -> "2"
downspout.execute('decrement');  // -> "1"

Basic usage by CLI counter samples

with the React

(Later)

About

A framework to serialize the execution of event handlers for disorderly event emitting

License:MIT License


Languages

Language:JavaScript 100.0%