frpfsm
FRP Finite State Machine
Last updated 4 years ago by jschomay .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install frpfsm 
SYNC missed versions from official npm registry.

frpfsm (FRP Finite State Machine)

A small and simple Functionally Reactive Programming (streams) based state machine with a nice API for defining and transitioning states. Built on top of kefir.js.

Concept

Each state is a factory function that takes initial data and returns a stream that emits a single event when it wants to transition to a different state, specifying the transition name and exit data. frpfsm connects all the state streams together.

Usage

Initializing your states and transitions:

frpfsm.loadState({
  name: "Preload",
  fn: preloadState,
  transitions:{
    "loaded": "Start"
  }
});

frpfsm.loadState({
  name: "Start",
  fn: startState,
  transitions:{
    "readyToPlay": "Play"
    "changeSettings": "Settings"
  }
});

// etc...

Define your states:

startState = function(startingValue) {
  // do other state stuff...

  // return stream that emmits a transition event when the play button is clicked
  return Kefir.fromEvents(document.querySelector('#start'), 'click')
  .map(function() {
    return ["readyToPlay", startingValue]; 
  });
};

Start the machine:

var debug = true;
frpfsm.start("Preload", startingValue, debug);

See example.js for a complete example.

Current Tags

  • 0.0.2                                ...           latest (4 years ago)

2 Versions

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

Copyright 2014 - 2017 © taobao.org |