import funHooks from 'fun-hooks';
let createHook = funcHooks({
useProxy: false,
ready: funHook.ASYNC | funcHooks.QUEUE
});
// Sync (before, after)
function sum(a, b) {
return a + b;
}
let hookedSum = createHook('sync', sum);
// before
hookedSum.before(function(next, a, b) {
a = a + 1
next(a, b);
});
// after
hookedSum.after(function(next, result) {
next(result + 1);
});
let result = hookedSum(1, 1);
// hookedSum(1, 1) => hookedSum.before(1, 1) => sum(2, 1) => hookedSum.after(3) => 4
// Async (before, after)
function increment(a, b, callback) {
callback(a + 1, b + 1);
}
let hookedIncrement = createHook('async', increment);
hookedIncrement.before(function(next, a, b) {
a = a + 1;
next(a, b);
});
hookedIncrement.after(function(next, a, b) {
next(a, b + 1);
});
hookedIncrement(1, 1, function(a, b) {
console.log(a, b); // 3, 3
});