forked from bnoguchi/hooks-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.alt.js
134 lines (125 loc) · 4.01 KB
/
hooks.alt.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Hooks are useful if we want to add a method that automatically has `pre` and `post` hooks.
* For example, it would be convenient to have `pre` and `post` hooks for `save`.
* _.extend(Model, mixins.hooks);
* Model.hook('save', function () {
* console.log('saving');
* });
* Model.pre('save', function (next, done) {
* console.log('about to save');
* next();
* });
* Model.post('save', function (next, done) {
* console.log('saved');
* next();
* });
*
* var m = new Model();
* m.save();
* // about to save
* // saving
* // saved
*/
// TODO Add in pre and post skipping options
module.exports = {
/**
* Declares a new hook to which you can add pres and posts
* @param {String} name of the function
* @param {Function} the method
* @param {Function} the error handler callback
*/
hook: function (name, fn, err) {
if (arguments.length === 1 && typeof name === 'object') {
for (var k in name) { // `name` is a hash of hookName->hookFn
this.hook(k, name[k]);
}
return;
}
if (!err) err = fn;
var proto = this.prototype || this
, pres = proto._pres = proto._pres || {}
, posts = proto._posts = proto._posts || {};
pres[name] = pres[name] || [];
posts[name] = posts[name] || [];
function noop () {}
proto[name] = function () {
var self = this
, pres = this._pres[name]
, posts = this._posts[name]
, numAsyncPres = 0
, hookArgs = [].slice.call(arguments)
, preChain = pres.map( function (pre, i) {
var wrapper = function () {
if (arguments[0] instanceof Error)
return err(arguments[0]);
if (numAsyncPres) {
// arguments[1] === asyncComplete
if (arguments.length)
hookArgs = [].slice.call(arguments, 2);
pre.apply(self,
[ preChain[i+1] || allPresInvoked,
asyncComplete
].concat(hookArgs)
);
} else {
if (arguments.length)
hookArgs = [].slice.call(arguments);
pre.apply(self,
[ preChain[i+1] || allPresDone ].concat(hookArgs));
}
}; // end wrapper = function () {...
if (wrapper.isAsync = pre.isAsync)
numAsyncPres++;
return wrapper;
}); // end posts.map(...)
function allPresInvoked () {
if (arguments[0] instanceof Error)
err(arguments[0]);
}
function allPresDone () {
if (arguments[0] instanceof Error)
return err(arguments[0]);
if (arguments.length)
hookArgs = [].slice.call(arguments);
fn.apply(self, hookArgs);
var postChain = posts.map( function (post, i) {
var wrapper = function () {
if (arguments[0] instanceof Error)
return err(arguments[0]);
if (arguments.length)
hookArgs = [].slice.call(arguments);
post.apply(self,
[ postChain[i+1] || noop].concat(hookArgs));
}; // end wrapper = function () {...
return wrapper;
}); // end posts.map(...)
if (postChain.length) postChain[0]();
}
if (numAsyncPres) {
complete = numAsyncPres;
function asyncComplete () {
if (arguments[0] instanceof Error)
return err(arguments[0]);
--complete || allPresDone.call(this);
}
}
(preChain[0] || allPresDone)();
};
return this;
},
pre: function (name, fn, isAsync) {
var proto = this.prototype
, pres = proto._pres = proto._pres || {};
if (fn.isAsync = isAsync) {
this.prototype[name].numAsyncPres++;
}
(pres[name] = pres[name] || []).push(fn);
return this;
},
post: function (name, fn, isAsync) {
var proto = this.prototype
, posts = proto._posts = proto._posts || {};
(posts[name] = posts[name] || []).push(fn);
return this;
}
};