branch

0.0.0 • Public • Published

A simple EventEmitter example:

va branch = require("branch"),
EventEmitter = require("events").EventEmitter;
 
 
//first give branch 
var chain = branch(EventEmitter);
chain.source([new EventEmitter(), new EventEmitter()]);
 
//called twice - once per each event emitter
chain.on("hello-world", function(event) {
  
});
 
//called against each event emitter
chain.emit("hello-world");

With soda.js

var chain = branch({})

API

.branch(...schemas)

schemas - class, object, or array of methods to use in the chain. For example:

 
//using a class
var chain = branch(EventEmitter);
chain.on("hello-world", function(){});
 
 
//using an object or prototype
chain = branch({
    "helloWorld": function() {
 
    }
});
 
chain.helloWorld();
 
//using an array
chain = branch(['helloWorld']);
chain.helloWorld();

Creates a new chained object

chain.source(sourceArray)

sets the source for the chain

chain.pre(method, factory)

middleware for any method before it's actually called. If method is omitted, then all methods will be run the the middleware function.

var chain = branch(EventEmitter).
pre("on", function(type, callback, next) {
  next("prefix-" + type, callback);
}).
source([new EventEmitter()]);
 
 
//actual listener 
chain.on("hello", function() {
  
});
 
 
chain.emit("prefix-hello", "world!");

chain.add(...items)

adds an item to the chain

Result chain.[CHAIN_METHOD]

created from the prototype, array, or object in the .branch() function.

hain result.async()

Makes the chain asynchronous

var chain = branch(Array);
chain.source([1], [2], [3], [4]);
 
chain.map(function(n) {
  return n + 1;
}).async(function(err, values) {
  console.log(values);
})

chain result.sync()

Makes the chain synchronous - values from returned arrays will be

var chain = branch(Array);
chain.add([1],[2],[3],[4]);
var result = chain.map(function(n) {
  return n + 1;
}).sync();
 
console.log(result); //[2], [3], [4], [5]

/branch/

    Package Sidebar

    Install

    npm i branch

    Weekly Downloads

    34

    Version

    0.0.0

    License

    BSD

    Last publish

    Collaborators

    • architectd
    • crcn