Permalink
Browse files

Now, `next(null)` -- when the target method has a node-like function …

…(err, ...) calback as its last argument -- proceeds down the middleware chain without mutating the hook arguments.
  • Loading branch information...
1 parent d74cb01 commit 513870cfa442bd125a930dbcb02f93a94d358d98 @bnoguchi committed Jun 7, 2011
Showing with 53 additions and 2 deletions.
  1. +2 −1 hooks.js
  2. +51 −1 test.js
View
@@ -36,7 +36,8 @@ module.exports = {
var _args = Array.prototype.slice.call(arguments)
, currPre
, preArgs;
- if (_args.length) hookArgs = _args;
+ if (_args.length && !(arguments[0] === null && typeof lastArg === 'function'))
+ hookArgs = _args;
if (++_current < _total) {
currPre = pres[_current]
if (currPre.isAsync && currPre.length < 2)
View
52 test.js
@@ -191,7 +191,24 @@ module.exports = {
a.save();
a.value.should.equal(3);
},
- 'should default to the hook method as the error handler': function () {
+ "should fall back first to the hook method's last argument as the error handler if it is a function of arity 1 or 2": function () {
+ var A = function () {};
+ _.extend(A, hooks);
+ var counter = 0;
+ A.hook('save', function (callback) {
+ this.value = 1;
+ });
+ A.pre('save', true, function (next, done) {
+ next(new Error());
+ });
+ var a = new A();
+ a.save( function (err) {
+ if (err instanceof Error) counter++;
+ });
+ counter.should.equal(1);
+ should.deepEqual(undefined, a.value);
+ },
+ 'should fall back last to the hook method as the error handler': function () {
var A = function () {};
_.extend(A, hooks);
var counter = 0;
@@ -207,6 +224,38 @@ module.exports = {
counter.should.equal(1);
assert.equal(typeof a.value, 'undefined');
},
+ "should proceed without mutating arguments if `next(null)` is called in a serial pre, and the last argument of the target method is a callback with node-like signature function (err, obj) {...} or function (err) {...}": function () {
+ var A = function () {};
+ _.extend(A, hooks);
+ var counter = 0;
+ A.prototype.save = function (callback) {
+ this.value = 1;
+ callback();
+ };
+ A.pre('save', function (next) {
+ next(null);
+ });
+ var a = new A();
+ a.save( function (err) {
+ if (err instanceof Error) counter++;
+ else counter--;
+ });
+ counter.should.equal(-1);
+ a.value.should.eql(1);
+ },
+ "should proceed with mutating arguments if `next(null)` is callback in a serial pre, and the last argument of the target method is not a function": function () {
+ var A = function () {};
+ _.extend(A, hooks);
+ A.prototype.set = function (v) {
+ this.value = v;
+ };
+ A.pre('set', function (next) {
+ next(null);
+ });
+ var a = new A();
+ a.set(1);
+ should.strictEqual(null, a.value);
+ },
'should not run any posts if a pre fails': function () {
var A = function () {};
_.extend(A, hooks);
@@ -224,6 +273,7 @@ module.exports = {
a.save();
a.value.should.equal(1);
},
+
"can pass the hook's arguments verbatim to pres": function () {
var A = function () {};
_.extend(A, hooks);

0 comments on commit 513870c

Please sign in to comment.