Skip to content

Commit

Permalink
BREAKING CHANGE: remove useLegacyPost and add several new features
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 17, 2017
1 parent 9cbb8c7 commit 6dd8531
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 53 deletions.
69 changes: 42 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
}

++currentPre;
if (asyncPresLeft === 0 && currentPre >= numPres) {
return callback(null);
}
next.apply(context, arguments);
},
function(error) {
Expand All @@ -53,8 +56,7 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
done = true;
return callback(error);
}

if (--numAsyncPres === 0) {
if (--asyncPresLeft === 0 && currentPre >= numPres) {
return callback(null);
}
});
Expand Down Expand Up @@ -103,12 +105,12 @@ Kareem.prototype.execPre = function(name, context, args, callback) {
next.apply(null, [null].concat(args));
};

Kareem.prototype.execPreSync = function(name, context) {
Kareem.prototype.execPreSync = function(name, context, args) {
var pres = this._pres[name] || [];
var numPres = pres.length;

for (var i = 0; i < numPres; ++i) {
pres[i].fn.call(context);
pres[i].fn.apply(context, args || []);
}
};

Expand Down Expand Up @@ -197,15 +199,28 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
next();
};

Kareem.prototype.execPostSync = function(name, context) {
Kareem.prototype.execPostSync = function(name, context, args) {
var posts = this._posts[name] || [];
var numPosts = posts.length;

for (var i = 0; i < numPosts; ++i) {
posts[i].call(context);
posts[i].apply(context, args || []);
}
};

Kareem.prototype.createWrapperSync = function(name, fn) {
var kareem = this;
return function syncWrapper() {
kareem.execPreSync(name, this, arguments);

var toReturn = fn.apply(this, arguments);

kareem.execPostSync(name, this, [toReturn]);

return toReturn;
};
}

function _handleWrapError(instance, error, name, context, args, options, callback) {
if (options.useErrorHandlers) {
var _options = { error: error };
Expand All @@ -226,27 +241,20 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
args;
var _this = this;

var useLegacyPost;
if (typeof options === 'object') {
useLegacyPost = options && options.useLegacyPost;
} else {
useLegacyPost = options;
}
options = options || {};

this.execPre(name, context, args, function(error) {
if (error) {
var numCallbackParams = options.numCallbackParams || 0;
var nulls = [];
for (var i = 0; i < numCallbackParams; ++i) {
nulls.push(null);
var errorArgs = options.contextParameter ? [context] : [];
for (var i = errorArgs.length; i < numCallbackParams; ++i) {
errorArgs.push(null);
}
return _handleWrapError(_this, error, name, context, nulls,
return _handleWrapError(_this, error, name, context, errorArgs,
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);
Expand All @@ -258,18 +266,14 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
return _handleWrapError(_this, arguments[0], name, context,
argsWithoutError, 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 ?
return typeof lastArg === 'function' ?
lastArg.apply(context, arguments) :
undefined;
});
Expand All @@ -281,12 +285,13 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
Kareem.prototype.createWrapper = function(name, fn, context, options) {
var _this = this;
return function() {
var _context = context || this;
var args = Array.prototype.slice.call(arguments);
_this.wrap(name, fn, context, args, options);
_this.wrap(name, fn, _context, args, options);
};
};

Kareem.prototype.pre = function(name, isAsync, fn, error) {
Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) {
if (typeof arguments[1] !== 'boolean') {
error = fn;
fn = isAsync;
Expand All @@ -301,13 +306,23 @@ Kareem.prototype.pre = function(name, isAsync, fn, error) {
++pres.numAsync;
}

pres.push({ fn: fn, isAsync: isAsync });
if (unshift) {
pres.unshift({ fn: fn, isAsync: isAsync });
} else {
pres.push({ fn: fn, isAsync: isAsync });
}

return this;
};

Kareem.prototype.post = function(name, fn) {
(this._posts[name] = this._posts[name] || []).push(fn);
Kareem.prototype.post = function(name, fn, unshift) {
this._posts[name] = this._posts[name] || [];

if (unshift) {
this._posts[name].unshift(fn);
} else {
this._posts[name].push(fn);
}
return this;
};

Expand Down
26 changes: 0 additions & 26 deletions test/wrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,30 +318,4 @@ 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 6dd8531

Please sign in to comment.