From 23fa74c0bb2c7cc8173dec4a1191c7b4ad3f31b7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 27 Apr 2015 10:38:19 -0300 Subject: [PATCH] support legacy post hook behavior in wrap() --- index.js | 8 ++++++-- test/wrap.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index cfe7863..d12df73 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ Kareem.prototype.execPost = function(name, context, args, callback) { next(); }; -Kareem.prototype.wrap = function(name, fn, context, args) { +Kareem.prototype.wrap = function(name, fn, context, args, useLegacyPost) { var lastArg = (args.length > 0 ? args[args.length - 1] : null); var _this = this; @@ -155,6 +155,10 @@ Kareem.prototype.wrap = function(name, fn, context, args) { undefined; } + if (useLegacyPost && typeof lastArg === 'function') { + lastArg.apply(context, arguments); + } + var argsWithoutError = Array.prototype.slice.call(arguments, 1); _this.execPost(name, context, argsWithoutError, function() { if (arguments[0]) { @@ -163,7 +167,7 @@ Kareem.prototype.wrap = function(name, fn, context, args) { undefined; } - return typeof lastArg === 'function' ? + return typeof lastArg === 'function' && !useLegacyPost ? lastArg.apply(context, arguments) : undefined; }); diff --git a/test/wrap.test.js b/test/wrap.test.js index ada4553..3303961 100644 --- a/test/wrap.test.js +++ b/test/wrap.test.js @@ -248,4 +248,30 @@ describe('wrap()', function() { }, 25); }); + + it('can use legacy post behavior', function(done) { + var called = 0; + hooks.post('cook', function(callback) { + ++called; + callback(); + }); + + var args = [function(error) { + assert.equal(called, 0); + + setTimeout(function() { + assert.equal(called, 1); + done(); + }, 0); + }]; + + hooks.wrap( + 'cook', + function(callback) { + callback(); + }, + null, + args, + true); + }); }); \ No newline at end of file