kareem 2.0.4 → 2.1.0
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/index.js +38 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -253,18 +253,19 @@ function _handleWrapError(instance, error, name, context, args, options, callbac
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
256
|
-
|
|
257
|
-
|
|
256
|
+
const lastArg = (args.length > 0 ? args[args.length - 1] : null);
|
|
257
|
+
const argsWithoutCb = typeof lastArg === 'function' ?
|
|
258
258
|
args.slice(0, args.length - 1) :
|
|
259
259
|
args;
|
|
260
|
-
|
|
260
|
+
const _this = this;
|
|
261
261
|
|
|
262
262
|
options = options || {};
|
|
263
|
+
const checkForPromise = options.checkForPromise;
|
|
263
264
|
|
|
264
265
|
this.execPre(name, context, args, function(error) {
|
|
265
266
|
if (error) {
|
|
266
|
-
|
|
267
|
-
|
|
267
|
+
const numCallbackParams = options.numCallbackParams || 0;
|
|
268
|
+
const errorArgs = options.contextParameter ? [context] : [];
|
|
268
269
|
for (var i = errorArgs.length; i < numCallbackParams; ++i) {
|
|
269
270
|
errorArgs.push(null);
|
|
270
271
|
}
|
|
@@ -272,12 +273,29 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
272
273
|
options, lastArg);
|
|
273
274
|
}
|
|
274
275
|
|
|
275
|
-
|
|
276
|
-
|
|
276
|
+
const end = (typeof lastArg === 'function' ? args.length - 1 : args.length);
|
|
277
|
+
const numParameters = fn.length;
|
|
278
|
+
const ret = fn.apply(context, args.slice(0, end).concat(_cb));
|
|
279
|
+
|
|
280
|
+
if (checkForPromise) {
|
|
281
|
+
if (ret != null && typeof ret.then === 'function') {
|
|
282
|
+
// Thenable, use it
|
|
283
|
+
return ret.then(
|
|
284
|
+
res => _cb(null, res),
|
|
285
|
+
err => _cb(err)
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// If `fn()` doesn't have a callback argument and doesn't return a
|
|
290
|
+
// promise, assume it is sync
|
|
291
|
+
if (numParameters < end + 1) {
|
|
292
|
+
return _cb(null, ret);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
277
295
|
|
|
278
296
|
function _cb() {
|
|
279
|
-
|
|
280
|
-
|
|
297
|
+
const args = arguments;
|
|
298
|
+
const argsWithoutError = Array.prototype.slice.call(arguments, 1);
|
|
281
299
|
if (options.nullResultByDefault && argsWithoutError.length === 0) {
|
|
282
300
|
argsWithoutError.push(null);
|
|
283
301
|
}
|
|
@@ -302,8 +320,19 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
|
|
|
302
320
|
});
|
|
303
321
|
};
|
|
304
322
|
|
|
323
|
+
Kareem.prototype.hasHooks = function(name) {
|
|
324
|
+
return this._pres[name] != null || this._posts[name] != null;
|
|
325
|
+
};
|
|
326
|
+
|
|
305
327
|
Kareem.prototype.createWrapper = function(name, fn, context, options) {
|
|
306
328
|
var _this = this;
|
|
329
|
+
if (!this.hasHooks(name)) {
|
|
330
|
+
// Fast path: if there's no hooks for this function, just return the
|
|
331
|
+
// function wrapped in a nextTick()
|
|
332
|
+
return function() {
|
|
333
|
+
process.nextTick(() => fn.apply(this, arguments));
|
|
334
|
+
};
|
|
335
|
+
}
|
|
307
336
|
return function() {
|
|
308
337
|
var _context = context || this;
|
|
309
338
|
var args = Array.prototype.slice.call(arguments);
|