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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
'use strict';
function Kareem() {
this._pres = {};
this._posts = {};
}
Kareem.prototype.execPre = function(name, context, callback) {
var pres = this._pres[name] || [];
var numPres = pres.length;
var numAsyncPres = pres.numAsync || 0;
var currentPre = 0;
var asyncPresLeft = numAsyncPres;
var done = false;
if (!numPres) {
return process.nextTick(function() {
callback(null);
});
}
var next = function() {
if (currentPre >= numPres) {
return;
}
var pre = pres[currentPre];
if (pre.isAsync) {
pre.fn.call(
context,
function(error) {
if (error) {
if (done) {
return;
}
done = true;
return callback(error);
}
++currentPre;
next.apply(context, arguments);
},
function(error) {
if (error) {
if (done) {
return;
}
done = true;
return callback(error);
}
if (--numAsyncPres === 0) {
return callback(null);
}
});
} else if (pre.fn.length > 0) {
var args = [function(error) {
if (error) {
if (done) {
return;
}
done = true;
return callback(error);
}
if (++currentPre >= numPres) {
if (asyncPresLeft > 0) {
// Leave parallel hooks to run
return;
} else {
return callback(null);
}
}
next.apply(context, arguments);
}];
if (arguments.length >= 2) {
for (var i = 1; i < arguments.length; ++i) {
args.push(arguments[i]);
}
}
pre.fn.apply(context, args);
} else {
pre.fn.call(context);
if (++currentPre >= numPres) {
if (asyncPresLeft > 0) {
// Leave parallel hooks to run
return;
} else {
return process.nextTick(function() {
callback(null);
});
}
}
next();
}
};
next();
};
Kareem.prototype.execPreSync = function(name, context) {
var pres = this._pres[name] || [];
var numPres = pres.length;
for (var i = 0; i < numPres; ++i) {
pres[i].fn.call(context);
}
};
Kareem.prototype.execPost = function(name, context, args, options, callback) {
if (arguments.length < 5) {
callback = options;
options = null;
}
var posts = this._posts[name] || [];
var numPosts = posts.length;
var currentPost = 0;
var firstError = null;
if (options && options.error) {
firstError = options.error;
}
if (!numPosts) {
return process.nextTick(function() {
callback.apply(null, [firstError].concat(args));
});
}
var next = function() {
var post = posts[currentPost];
if (firstError) {
if (post.length === args.length + 2) {
post.apply(context, [firstError].concat(args).concat(function(error) {
if (error) {
firstError = error;
}
if (++currentPost >= numPosts) {
return callback.call(null, firstError);
}
next();
}));
} else {
if (++currentPost >= numPosts) {
return callback.call(null, firstError);
}
next();
}
} else {
if (post.length === args.length + 2) {
// Skip error handlers if no error
if (++currentPost >= numPosts) {
return callback.apply(null, [null].concat(args));
}
return next();
}
if (post.length === args.length + 1) {
post.apply(context, args.concat(function(error) {
if (error) {
firstError = error;
return next();
}
if (++currentPost >= numPosts) {
return callback.apply(null, [null].concat(args));
}
next();
}));
} else {
post.apply(context, args);
if (++currentPost >= numPosts) {
return callback.apply(null, [null].concat(args));
}
next();
}
}
};
next();
};
Kareem.prototype.execPostSync = function(name, context) {
var posts = this._posts[name] || [];
var numPosts = posts.length;
for (var i = 0; i < numPosts; ++i) {
posts[i].call(context);
}
};
function _handleWrapError(instance, error, name, context, args, options, callback) {
if (options.useErrorHandlers) {
var _options = { error: error };
var newArgs = [];
// Filter out trailing undefineds
for (var i = args.length; i >= 0; --i) {
if (newArgs.length > 0 || args[i] !== void 0) {
newArgs.unshift(args[i]);
}
}
return instance.execPost(name, context, newArgs, _options, function(error) {
return typeof callback === 'function' && callback(error);
});
} else {
return typeof callback === 'function' ?
callback(error) :
undefined;
}
}
Kareem.prototype.wrap = function(name, fn, context, args, options) {
var lastArg = (args.length > 0 ? args[args.length - 1] : null);
var argsWithoutCb = typeof lastArg === 'function' ?
args.slice(0, args.length - 1) :
args;
var _this = this;
var useLegacyPost;
if (typeof options === 'object') {
useLegacyPost = options && options.useLegacyPost;
} else {
useLegacyPost = options;
}
options = options || {};
this.execPre(name, context, function(error) {
if (error) {
return _handleWrapError(_this, error, name, context, argsWithoutCb,
options, lastArg)
}
var end = (typeof lastArg === 'function' ? args.length - 1 : args.length);
fn.apply(context, args.slice(0, end).concat(function() {
var args = arguments;
var argsWithoutError = Array.prototype.slice.call(arguments, 1);
if (arguments[0]) {
// Assume error
return _handleWrapError(_this, arguments[0], name, context,
args, options, lastArg);
} else {
if (useLegacyPost && typeof lastArg === 'function') {
lastArg.apply(context, arguments);
}
_this.execPost(name, context, argsWithoutError, function() {
if (arguments[0]) {
return typeof lastArg === 'function' ?
lastArg(arguments[0]) :
undefined;
}
return typeof lastArg === 'function' && !useLegacyPost ?
lastArg.apply(context, arguments) :
undefined;
});
}
}));
});
};
Kareem.prototype.createWrapper = function(name, fn, context, options) {
var _this = this;
return function() {
var args = Array.prototype.slice.call(arguments);
_this.wrap(name, fn, context, args, options);
};
};
Kareem.prototype.pre = function(name, isAsync, fn, error) {
if (typeof arguments[1] !== 'boolean') {
error = fn;
fn = isAsync;
isAsync = false;
}
this._pres[name] = this._pres[name] || [];
var pres = this._pres[name];
if (isAsync) {
pres.numAsync = pres.numAsync || 0;
++pres.numAsync;
}
pres.push({ fn: fn, isAsync: isAsync });
return this;
};
Kareem.prototype.post = function(name, fn) {
(this._posts[name] = this._posts[name] || []).push(fn);
return this;
};
Kareem.prototype.clone = function() {
var n = new Kareem();
for (var key in this._pres) {
n._pres[key] = this._pres[key].slice();
}
for (var key in this._posts) {
n._posts[key] = this._posts[key].slice();
}
return n;
};
module.exports = Kareem;
|