From 10717ad77662d86d555288973abe726f38d8b17b Mon Sep 17 00:00:00 2001 From: Bryan Tong Date: Mon, 29 Sep 2014 14:28:42 -0600 Subject: [PATCH] fixed lifecycle regression where it would start and stop members that didnt have registered handlers --- README.md | 3 +++ helpers/Lifecycle.js | 8 ++++---- package.json | 2 +- test/Lifecycle.test.js | 18 ++++++++++++++++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 31d154b..f3c7ad5 100644 --- a/README.md +++ b/README.md @@ -466,6 +466,9 @@ stopped ## Changelog +### 0.8.1 +* Lifecycle will only call items with registered start or shutdown handlers + ### 0.8.0 * Lifecycle helper now takes an optional name * Lifecycle helper is now an event emitter (useful for loggin) diff --git a/helpers/Lifecycle.js b/helpers/Lifecycle.js index 4719db3..5de49f3 100644 --- a/helpers/Lifecycle.js +++ b/helpers/Lifecycle.js @@ -2,8 +2,6 @@ var async = require('async') var EventEmitter = require('events').EventEmitter -var nullFunc = function(done){done()} - /** @@ -45,8 +43,8 @@ Lifecycle.prototype.add = function(title,start,stop){ var item = { index: this.nextIndex(), title: title, - start: start || nullFunc, - stop: stop || nullFunc + start: start, + stop: stop } this.emit('add',item) this.items.push(item) @@ -85,6 +83,7 @@ Lifecycle.prototype.start = function(done){ async.eachSeries( that.items, function(item,next){ + if('function' !== typeof item.start) return next() that.emit('start',item) item.start(next) }, @@ -109,6 +108,7 @@ Lifecycle.prototype.stop = function(done){ async.eachSeries( that.items, function(item,next){ + if('function' !== typeof item.stop) return next() that.emit('stop',item) item.stop(next) }, diff --git a/package.json b/package.json index b796c15..2a1655f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "SnailJS ", "name": "infant", "description": "Child process, and cluster helper for Node.js", - "version": "0.8.0", + "version": "0.8.1", "homepage": "http://snailjs.org/", "repository": { "type": "git", diff --git a/test/Lifecycle.test.js b/test/Lifecycle.test.js index 52096b3..3c81bfd 100644 --- a/test/Lifecycle.test.js +++ b/test/Lifecycle.test.js @@ -88,7 +88,7 @@ describe('helpers/Lifecycle',function(){ }) }) it('should emit a start event',function(done){ - inst.add('test1') + inst.add('test1',function(next){next()}) inst.once('start',function(item){ expect(item.title).to.equal('test1') done() @@ -96,7 +96,7 @@ describe('helpers/Lifecycle',function(){ inst.start(function(err){if(err) done(err)}) }) it('should emit a stop event',function(done){ - inst.add('test1') + inst.add('test1',null,function(next){next()}) inst.once('stop',function(item){ expect(item.title).to.equal('test1') done() @@ -132,4 +132,18 @@ describe('helpers/Lifecycle',function(){ inst.add('test1') inst.remove('test1') }) + it('should not start items without start functions',function(done){ + inst.add('dont start',null,function(next){next()}) + inst.once('start',function(){ + done('should not have started') + }) + inst.start(done) + }) + it('should not stop items without stop functions',function(done){ + inst.add('dont stop',function(next){next()}) + inst.once('stop',function(){ + done('should not have stopped') + }) + inst.stop(done) + }) })