Skip to content
streaming finite state machines
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore
README.md
bench.js
package.json
stream-fsm.js
test.js

README.md

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

Something went wrong with that request. Please try again.