项目作者: jdwije

项目描述 :
A tiny cascading state machine.
高级语言: TypeScript
项目地址: git://github.com/jdwije/cascade.git
创建时间: 2017-04-24T12:03:29Z
项目社区:https://github.com/jdwije/cascade

开源协议:MIT License

下载


Cascade

A tiny cascading state machine.

Build Status

quick-start

installation:

  1. npm i --save @jdw/cascade

usage:

  1. import cascade from '@jdw/cascade';
  2. const increment = x => x++;
  3. const square = x => Math.pow(x, x);
  4. // apply state to functions and sequentially
  5. // calculate a result
  6. cascade(100)
  7. .chain(square)
  8. .chain(increment)
  9. .read(); // 10001

API

cascade(initialState) => Cascade

This initialization method takes an initial state and returns a Cascade
object.

Cascade

chain(fn) => Cascade

This method takes function fn invoking it with the machines state as an
argument and then mutating state to the result.

example:

  1. return cascade(1)
  2. .chain(x => x + 1) // internal state set to: 2
  3. .chain(x => x * x) // internal state set to: 4
  4. .read() // return internal state: 4

chain(fn, cb) => Cascade

As above however with the optional recovery method cb supplied. cb is invoked
in case something went wrong with the error, state, and fn supplied as its
arguments.

example:

  1. const process = state => throw new Error();
  2. const recover = (err, state, fn) => state + 10;
  3. return cascade(90)
  4. .chain(process, recover) // errors on process() invoking recover().
  5. .read(); // outputs: 100

read() => any

Returns the current state of the machine.

example:

  1. return cascade(true)
  2. .read(); // true