Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved the isAsync middleware argument in `pre` and `post` from the 3r…

…d to 2nd argument for better readability.
  • Loading branch information...
commit e26b1c419215f33877d863ecb2fd12f5602beaf7 1 parent bc188c4
@bnoguchi authored
Showing with 31 additions and 22 deletions.
  1. +5 −4 README.md
  2. +10 −2 hooks.js
  3. +16 −16 test.js
View
9 README.md
@@ -240,7 +240,8 @@ We illustrate via the parallel validation example mentioned above:
// This only gets run once the two `done`s are both invoked via preOne and preTwo.
});
- Document.pre('save', function preOne (next, doneOne, callback) {
+ // true marks this as parallel middleware
+ Document.pre('save', true, function preOne (next, doneOne, callback) {
remoteServiceOne.validate(this.serialize(), function (err, isValid) {
// The code in here will probably be run after the `next` below this block
// and could possibly be run after the console.log("Hola") in `preTwo
@@ -248,16 +249,16 @@ We illustrate via the parallel validation example mentioned above:
if (isValid) doneOne();
});
next(); // Pass control to the next middleware
- }, true); // true marks this as parallel middleware
+ });
// We will suppose that we need 2 different remote services to validate our document
- Document.pre('save', function preTwo (next, doneTwo, callback) {
+ Document.pre('save', true, function preTwo (next, doneTwo, callback) {
remoteServiceTwo.validate(this.serialize(), function (err, isValid) {
if (err) return doneTwo(err);
if (isValid) doneTwo();
});
next();
- }, true);
+ });
// While preOne and preTwo are parallel, preThree is a serial pre middleware
Document.pre('save', function preThree (next, callback) {
View
12 hooks.js
@@ -100,7 +100,11 @@ module.exports = {
return this;
},
- pre: function (name, fn, isAsync) {
+ pre: function (name, isAsync, fn) {
+ if (arguments.length === 2) {
+ fn = isAsync;
+ isAsync = false;
+ }
var proto = this.prototype || this
, pres = proto._pres = proto._pres || {};
@@ -113,7 +117,11 @@ module.exports = {
(pres[name] = pres[name] || []).push(fn);
return this;
},
- post: function (name, fn, isAsync) {
+ post: function (name, isAsync, fn) {
+ if (arguments.length === 2) {
+ fn = isAsync;
+ isAsync = false;
+ }
var proto = this.prototype || this
, posts = proto._posts = proto._posts || {};
View
32 test.js
@@ -103,9 +103,9 @@ module.exports = {
}, function (err) {
counter++;
});
- A.pre('save', function (next, done) {
+ A.pre('save', true, function (next, done) {
next(new Error());
- }, true);
+ });
var a = new A();
a.save();
counter.should.equal(1);
@@ -199,9 +199,9 @@ module.exports = {
if (err instanceof Error) counter++;
this.value = 1;
});
- A.pre('save', function (next, done) {
+ A.pre('save', true, function (next, done) {
next(new Error());
- }, true);
+ });
var a = new A();
a.save();
counter.should.equal(1);
@@ -362,26 +362,26 @@ module.exports = {
counter++;
next();
});
- A.pre('set', function (next, done, path, val) {
+ A.pre('set', true, function (next, done, path, val) {
counter++;
setTimeout(function () {
counter++;
done();
}, 1000);
next();
- }, true);
+ });
A.pre('set', function (next, path, val) {
counter++;
next();
});
- A.pre('set', function (next, done, path, val) {
+ A.pre('set', true, function (next, done, path, val) {
counter++;
setTimeout(function () {
counter++;
done();
}, 500);
next();
- }, true);
+ });
var a = new A();
a.set('hello', 'world');
},
@@ -395,13 +395,13 @@ module.exports = {
if (path === 'hello') counter.should.equal(1);
if (path === 'foo') counter.should.equal(2);
});
- A.pre('set', function (next, done, path, val) {
+ A.pre('set', true, function (next, done, path, val) {
setTimeout(function () {
counter++;
done();
}, 1000);
next();
- }, true);
+ });
var a = new A();
a.set('hello', 'world');
a.set('foo', 'bar');
@@ -416,15 +416,15 @@ module.exports = {
console.log("UH OH, YOU SHOULD NOT BE SEEING THIS");
this.acked = true;
});
- A.pre('ack', function (next, done) {
+ A.pre('ack', true, function (next, done) {
next();
done();
done();
- }, true);
- A.pre('ack', function (next, done) {
+ });
+ A.pre('ack', true, function (next, done) {
next();
// Notice that done() is not invoked here
- }, true);
+ });
var a = new A();
a.ack();
setTimeout( function () {
@@ -464,13 +464,13 @@ module.exports = {
this[path] = val;
fn(null);
});
- A.pre('set', function (next, done, path, val, fn) {
+ A.pre('set', true, function (next, done, path, val, fn) {
setTimeout(function () {
counter++;
done(new Error);
}, 1000);
next();
- }, true);
+ });
var a = new A();
a.set('hello', 'world', function (err) {
err.should.be.an.instanceof(Error);
Please sign in to comment.
Something went wrong with that request. Please try again.