Permalink
Browse files

Class#hook now called lazily when you call Class#pre or Class#post. N…

…o longer need to invoke Class#hook yourself.
  • Loading branch information...
1 parent 9e5b33d commit 28d2531f5ed9fe71e57f1d22cae6cf999120e911 @bnoguchi committed May 22, 2011
Showing with 71 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 Makefile
  3. +14 −1 hooks.js
  4. +3 −2 package.json
  5. +52 −0 test.js
View
1 .gitignore
@@ -1 +1,2 @@
**.swp
+node_modules
View
2 Makefile
@@ -1,5 +1,5 @@
test:
- @NODE_ENV=test expresso \
+ @NODE_ENV=test ./node_modules/expresso/bin/expresso \
$(TESTFLAGS) \
./test.js
View
15 hooks.js
@@ -116,15 +116,28 @@ module.exports = {
pre: function (name, fn, isAsync) {
var proto = this.prototype || this
, pres = proto._pres = proto._pres || {};
+
+ // Lazy hook setup
+ if ('undefined' === typeof proto[name].numAsyncPres) {
+ this.hook(name, proto[name]);
+ }
+
if (fn.isAsync = isAsync) {
proto[name].numAsyncPres++;
}
+
(pres[name] = pres[name] || []).push(fn);
return this;
},
- post: function (name, fn) {
+ post: function (name, fn, isAsync) {
var proto = this.prototype || this
, posts = proto._posts = proto._posts || {};
+
+ // Lazy hook setup
+ if ('undefined' === typeof proto[name].numAsyncPres) {
+ this.hook(name, proto[name]);
+ }
+
(posts[name] = posts[name] || []).push(fn);
return this;
},
View
5 package.json
@@ -17,10 +17,11 @@
"test": "make test"
},
"dependencies": {
- "underscore": ">=1.1.4"
},
"devDependencies": {
- "expresso": ">= 0.7.3"
+ "expresso": ">=0.7.6",
+ "should": ">=0.2.1",
+ "underscore": ">=1.1.4"
},
"engines": {
"node": ">=0.4.x"
View
52 test.js
@@ -385,6 +385,27 @@ module.exports = {
a.set('hello', 'world');
},
+ 'invoking a method twice should run its async middleware twice': function () {
+ var counter = 0;
+ var A = function () {};
+ _.extend(A, hooks);
+ A.hook('set', function (path, val) {
+ this[path] = val;
+ if (path === 'hello') counter.should.equal(1);
+ if (path === 'foo') counter.should.equal(2);
+ });
+ A.pre('set', function (next, done, path, val) {
+ setTimeout(function () {
+ counter++;
+ done();
+ }, 1000);
+ next();
+ }, true);
+ var a = new A();
+ a.set('hello', 'world');
+ a.set('foo', 'bar');
+ },
+
'should be able to remove a particular pre': function () {
var A = function () {}
, preTwo;
@@ -428,5 +449,36 @@ module.exports = {
a.value.should.equal(1);
should.strictEqual(undefined, a.preValueOne);
should.strictEqual(undefined, a.preValueTwo);
+ },
+
+ '#pre should lazily make a method hookable': function () {
+ var A = function () {};
+ _.extend(A, hooks);
+ A.prototype.save = function () {
+ this.value = 1;
+ };
+ A.pre('save', function (next) {
+ this.preValue = 2;
+ next();
+ });
+ var a = new A();
+ a.save();
+ a.value.should.equal(1);
+ a.preValue.should.equal(2);
+ },
+
+ '#post should lazily make a method hookable': function () {
+ var A = function () {};
+ _.extend(A, hooks);
+ A.prototype.save = function () {
+ this.value = 1;
+ };
+ A.post('save', function (next) {
+ this.value = 2;
+ next();
+ });
+ var a = new A();
+ a.save();
+ a.value.should.equal(2);
}
};

0 comments on commit 28d2531

Please sign in to comment.