-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhooks.js
45 lines (35 loc) · 991 Bytes
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Hooks
// (c)copyright 2015 by Gerald Wodni <[email protected]>
"use strict";
var _ = require( "underscore" );
module.exports = function( k ) {
var postHooks = [];
var exitHooks = [];
var hooks = {};
function addHook( hook, callback ) {
if( !_.has( hooks, hook ) )
hooks[ hook ] = []
hooks[ hook ].push( callback );
}
function executeHooks( hook ) {
if( !_.has( hooks, hook ) )
return;
var args = [];
Array.prototype.push.apply( args, arguments );
args.shift(); // remove hook argument
hooks[ hook ].forEach( function( callback ) {
callback.apply( this, args );
});
}
function routePostHooks() {
k.app.use( function( req, res, next ) {
executeHooks( "post", req, res );
next();
});
}
return {
add: addHook,
execute: executeHooks,
routePostHooks: routePostHooks
}
};