Mooring
Born from a desire to have an API / hooks system that:
- Has a small API surfice
- Is easy to understand
- Has a data flow that is immutable
- Lets pre hooks manipulate the data going into a hooked method
- Lets post hooks manipulate the data coming out from a hooked method
- Not have the hooks stored on the prototype but rather in a seperate object
Credit to the kareem & grappling-hook projects for their insight and the inspiration they provided (also some code ;).)
API Spec
before hooks
Before hooks are called before a given function executes. They receve in their method sig all the params the hooked method receves and can augment them.
It runs before hooks with a synchronous API
// Here, false marks the hook as synchronous hooks; var cook = hooks; // When you call the now-hooked method, the hooks will be called ;
It also runs before hooks with an asynchronous API
Asynchronous is the default
hooks; var cook = hooks; // When you call the now-hooked method, the hooks will be called ;
It can run multipe before hooks
var count1 = 0; var count2 = 0; hooks; hooks; var cook = hooks; ;
after hooks
It runs after hooks with a synchronous API
// Here, false marks the hook as synchronous hooks; var cook = hooks; // When you call the now-hooked method, the hooks will be called ;
It also runs after hooks with an asynchronous API
Asynchronous is the default
hooks; var cook = hooks; // When you call the now-hooked method, the hooks will be called ;
It can run multipe after hooks
var count1 = 0; var count2 = 0; hooks; hooks; var cook = hooks; ;
call hooks
It lets you call a hook without first defining it
// Here, false marks the hook as synchronous hooks; // first param is the hook name, next is an array of arguments, next is a flag for is the call immutable and lastly is your hooked method/code hooks;