kareem 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +2 -1
- package/index.js +16 -6
- package/package.json +1 -1
- package/test/pre.test.js +30 -0
- package/test/wrap.test.js +26 -0
package/.travis.yml
CHANGED
package/index.js
CHANGED
|
@@ -38,7 +38,7 @@ Kareem.prototype.execPre = function(name, context, callback) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
++currentPre;
|
|
41
|
-
next();
|
|
41
|
+
next.apply(context, arguments);
|
|
42
42
|
},
|
|
43
43
|
function(error) {
|
|
44
44
|
if (error) {
|
|
@@ -54,7 +54,7 @@ Kareem.prototype.execPre = function(name, context, callback) {
|
|
|
54
54
|
}
|
|
55
55
|
});
|
|
56
56
|
} else if (pre.fn.length > 0) {
|
|
57
|
-
|
|
57
|
+
var args = [function(error) {
|
|
58
58
|
if (error) {
|
|
59
59
|
if (done) {
|
|
60
60
|
return;
|
|
@@ -72,8 +72,14 @@ Kareem.prototype.execPre = function(name, context, callback) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
next();
|
|
76
|
-
}
|
|
75
|
+
next.apply(context, arguments);
|
|
76
|
+
}];
|
|
77
|
+
if (arguments.length >= 2) {
|
|
78
|
+
for (var i = 1; i < arguments.length; ++i) {
|
|
79
|
+
args.push(arguments[i]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
pre.fn.apply(context, args);
|
|
77
83
|
} else {
|
|
78
84
|
pre.fn.call(context);
|
|
79
85
|
if (++currentPre >= numPres) {
|
|
@@ -133,7 +139,7 @@ Kareem.prototype.execPost = function(name, context, args, callback) {
|
|
|
133
139
|
next();
|
|
134
140
|
};
|
|
135
141
|
|
|
136
|
-
Kareem.prototype.wrap = function(name, fn, context, args) {
|
|
142
|
+
Kareem.prototype.wrap = function(name, fn, context, args, useLegacyPost) {
|
|
137
143
|
var lastArg = (args.length > 0 ? args[args.length - 1] : null);
|
|
138
144
|
var _this = this;
|
|
139
145
|
|
|
@@ -155,6 +161,10 @@ Kareem.prototype.wrap = function(name, fn, context, args) {
|
|
|
155
161
|
undefined;
|
|
156
162
|
}
|
|
157
163
|
|
|
164
|
+
if (useLegacyPost && typeof lastArg === 'function') {
|
|
165
|
+
lastArg.apply(context, arguments);
|
|
166
|
+
}
|
|
167
|
+
|
|
158
168
|
var argsWithoutError = Array.prototype.slice.call(arguments, 1);
|
|
159
169
|
_this.execPost(name, context, argsWithoutError, function() {
|
|
160
170
|
if (arguments[0]) {
|
|
@@ -163,7 +173,7 @@ Kareem.prototype.wrap = function(name, fn, context, args) {
|
|
|
163
173
|
undefined;
|
|
164
174
|
}
|
|
165
175
|
|
|
166
|
-
return typeof lastArg === 'function' ?
|
|
176
|
+
return typeof lastArg === 'function' && !useLegacyPost ?
|
|
167
177
|
lastArg.apply(context, arguments) :
|
|
168
178
|
undefined;
|
|
169
179
|
});
|
package/package.json
CHANGED
package/test/pre.test.js
CHANGED
|
@@ -193,4 +193,34 @@ describe('execPre', function() {
|
|
|
193
193
|
done();
|
|
194
194
|
});
|
|
195
195
|
});
|
|
196
|
+
|
|
197
|
+
it('allows passing arguments to the next pre', function(done) {
|
|
198
|
+
var execed = {};
|
|
199
|
+
|
|
200
|
+
hooks.pre('cook', function(next) {
|
|
201
|
+
execed.first = true;
|
|
202
|
+
next(null, 'test');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
hooks.pre('cook', function(next, p) {
|
|
206
|
+
execed.second = true;
|
|
207
|
+
assert.equal(p, 'test');
|
|
208
|
+
next();
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
hooks.pre('cook', function(next, p) {
|
|
212
|
+
execed.third = true;
|
|
213
|
+
assert.ok(!p);
|
|
214
|
+
next();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
hooks.execPre('cook', null, function(err) {
|
|
218
|
+
assert.ifError(err);
|
|
219
|
+
assert.equal(3, Object.keys(execed).length);
|
|
220
|
+
assert.ok(execed.first);
|
|
221
|
+
assert.ok(execed.second);
|
|
222
|
+
assert.ok(execed.third);
|
|
223
|
+
done();
|
|
224
|
+
});
|
|
225
|
+
});
|
|
196
226
|
});
|
package/test/wrap.test.js
CHANGED
|
@@ -248,4 +248,30 @@ describe('wrap()', function() {
|
|
|
248
248
|
},
|
|
249
249
|
25);
|
|
250
250
|
});
|
|
251
|
+
|
|
252
|
+
it('can use legacy post behavior', function(done) {
|
|
253
|
+
var called = 0;
|
|
254
|
+
hooks.post('cook', function(callback) {
|
|
255
|
+
++called;
|
|
256
|
+
callback();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
var args = [function(error) {
|
|
260
|
+
assert.equal(called, 0);
|
|
261
|
+
|
|
262
|
+
setTimeout(function() {
|
|
263
|
+
assert.equal(called, 1);
|
|
264
|
+
done();
|
|
265
|
+
}, 0);
|
|
266
|
+
}];
|
|
267
|
+
|
|
268
|
+
hooks.wrap(
|
|
269
|
+
'cook',
|
|
270
|
+
function(callback) {
|
|
271
|
+
callback();
|
|
272
|
+
},
|
|
273
|
+
null,
|
|
274
|
+
args,
|
|
275
|
+
true);
|
|
276
|
+
});
|
|
251
277
|
});
|