filterchain
Perform work before and/or after an operation.
Disclaimer
This is not meant to be used as async flow control, but is merely a way to dynamically upgrade the functionality of getters and setters.
This also means that errors will not stop the chain of events here, you must specifically cancel the current layer in order to avoid performing the core
action.
Learn by example
The basic operation of filterchain goes something like this:
var chain = ; ;
and here is the outer api
// Create a chain with no layers and no core functionvar chain = filterchain;
Where chain
is a function that accepts an optional data
and a optional callback
with the arguments errors
and data
. The callback
is called after the filter chain has been executed.
data
can be any javascript value
// Excerpt to show what the chain callback looks like.// `errors` is either null (no errors) or an array
And layers
is an array of functions
var chain = ;
next
is a function with the signature next(data[, bubbleFunction])
cancel
is a function with the signature cancel([error])
And core
is the function that will be run after the capture phase but before the bubble phase. The core
method should accept data
and fn
.
var chain = ; ; ;
What (else) does it do?
In a sense, filter chains are similar to onions. Passing data into the outer husk causes it to flow down through each layer toward the core. Each function (aka: layer) along the path as a chance to either manipulate or validate the data before forwarding it onto the next layer or canceling it.
Manipulate and forward data
var chain = ; ;
Cancel + bubble
Cancelling causes the flow of the chain to be reversed immediately.
var chain = ; ;
Post process data
Passing a function as the second argument to next
will cause the filter chain to call that method during the bubble phase
var fc = ;var chain = fc; ;
The first argument to done
is an error and the second is the data that will be bubbled back out to the outer husk of the filter chain.
Compose filter chains
var inner = var outer = ;
Contrived Use Cases
User creation example
var createUser = ; ; ;
Calculated attriutes in backbone
note: this is purely conceptual
var Rectangle = BackboneModel; var a = x: 10 y : 4 ;console; // outputs '40'
Install
Node.js
npm install filterchain
Browser
works with a plain ol' script tag and access it via window.createChain