npm install branch
A simple EventEmitter example:
``javascript
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
`javascript`
var chain = branch({})
#### .branch(...schemas)
schemas - class, object, or array of methods to use in the chain. For example:
`javascript
//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.
`javascript
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
`javascript
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
`javascript
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]
``