stalker-pattern

0.1.1 • Public • Published

stalker-pattern

stalker-pattern NPM package information

stalker-pattern travis-CI build stalker-pattern coverage status

Stalker-pattern is a Javascript pattern with a promise-like syntax streaming but in which the whole stream is re-triggerable like an event. Additionally it also supports synchronous triggering.


API

A basic notion of Promises will help you easily understand this pattern.

Basics

var Stalker = require('stalker-pattern');
 
var myStalker = new Stalker(function(trigger){
  trigger("hi!");
});
 
myStalker.follow(function(result){
  console.log(result); //prints "hi!"
});
 

Chaining stalkers

var myStalker = new Stalker(function(trigger){
 
  trigger(new Stalker(function(t){
    t("More complex");
 
    setTimeout(function(){
      t("Async");
    })
  }));
 
}).follow(function(result){
 
  return new Stalker(function(t2){
    t2(result+" hi!");
  });
 
}).follow(function(result){
 
  // prints "More complex hi!",
  // and then on next tick prints "Async hi!"
  console.log(result);
 
});
 

Piping only a few occurrences

new Stalker(function(trigger){
 
  trigger(1);
  trigger(2);
  trigger(3);
  trigger(4);
 
}).from(1).to(3).follow(function(number){
 
  console.log(number); //prints 1,2,3
  return number;
 
}).the(2).follow(function(number){
 
  console.log(number); //prints 2
  return number;
 
}).first().follow(function(number){
 
  console.log(number); //prints 2
 
});
 
//stack prints:
//1, 2, 3, 2, 2
 

Note: In the case above "1","2","3" all fire in the first follow() before "2" is propagated down to the 2nd follow(). This is the default behaviour of Stalker for syncrounous triggering

Package Sidebar

Install

npm i stalker-pattern

Weekly Downloads

3

Version

0.1.1

License

MIT

Last publish

Collaborators

  • carlosouro