Skip to content

Commit

Permalink
support legacy post hook behavior in wrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 27, 2015
1 parent adcd201 commit 23fa74c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Expand Up @@ -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;

Expand All @@ -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]) {
Expand All @@ -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;
});
Expand Down
26 changes: 26 additions & 0 deletions test/wrap.test.js
Expand Up @@ -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);
});
});

0 comments on commit 23fa74c

Please sign in to comment.