Skip to content

Commit

Permalink
Moved the isAsync middleware argument in pre and post from the 3r…
Browse files Browse the repository at this point in the history
…d to 2nd argument for better readability.
  • Loading branch information
bnoguchi committed Jun 3, 2011
1 parent bc188c4 commit e26b1c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -240,24 +240,25 @@ 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
if (err) return doneOne(err);
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) {
Expand Down
12 changes: 10 additions & 2 deletions hooks.js
Expand Up @@ -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 || {};

Expand All @@ -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 || {};

Expand Down
32 changes: 16 additions & 16 deletions test.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
},
Expand All @@ -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');
Expand All @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e26b1c4

Please sign in to comment.