stream-fsm

streaming finite state machine

npm install stream-fsm
4 downloads in the last day
7 downloads in the last week
39 downloads in the last month

stream-fsm

A streaming finite state machine

Install

npm install stream-fsm

Use


var fsm = require('stream-fsm');

var out = {};
var stream = fsm({
  // init is the default state
  init : fsm.want(5, function(data) {
    out.start = data;
    this.change('next');
  }),
  next : fsm.want(4, function(data) {
    out.next = data;
    this.change('last');
  }),
  last : fsm.want(4, function(data) {
    out.last = data;
    this.done();
  })
}, function() {

  console.log(out);

  // outputs: { start: 'start', next: 'next', last: 'last' }

});

stream.write('startnextlast');

Passthrough


var stream = fsm({
  init : function(d) {

    // queue data to be written on writable portion of the stream
    this.queue(d);

    // this is required for tracking
    // return the number of bytes consumed
    // Note: fsm.want does this already
    return d.length;
  }
});

Other methods

stream.fsm.cache() - returns the data that is currently held in the cache stream.fsm.mode() - returns current mode of the fsm

License

MIT

npm loves you