node.js - Are there any good patterns for controlling flow based on sequences of events in Node? -


in node it's easy create evented architecture. if have client of streaming api (via socket.io) don't control, , client needs react differently based on varying sequences of events.
example of 2 different sequences:

  • first order placed > 2nd order placed
  • first order placed > first order hit trade > 2nd order placed

so far using transform streams , i'm thinking of creating complicated custom logic buffers desired messages when need , acts upon them, wanted know if there best practices deal kind of situation.

thanks

the idea define set of objects define event sequences , when first event matches, copy sequence , pop elements of array until have no more elements. if encounter event doesn't match, reset current buffer.

var events = [ { sequence : ['event 1', 'event 2', 'event4']   handler: firstsequencehandler }, { sequence : ['event 3', 'event 2']   handler: secondsequencehandler } ]; 

subscribe event stream:

var eventbuffer; var handler; on('data', function(event){   if(eventbuffer){ // have started sequence:     var expectedevent = eventbuffer.pop(); // next expected event     if(expectedevent === event && eventbuffer.length===0)       return handler();     else{       // reset buffer:       eventbuffer = null;       handler = null;     }   }   // check if event in list of events first item.   // if   eventbuffer = event.sequence.slice(0); // copy sequence.   handler = event.handler;   eventbuffer.pop(); // remove first element found it. )); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -