stream-fsm

0.3.1 • Public • Published

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

Package Sidebar

Install

npm i stream-fsm

Weekly Downloads

400

Version

0.3.1

License

MIT

Last publish

Collaborators

  • tmpvar