Skip to content

Commit

Permalink
fixed lifecycle regression where it would start and stop members that…
Browse files Browse the repository at this point in the history
… didnt have registered handlers
  • Loading branch information
nullivex committed Sep 29, 2014
1 parent 78eb9a7 commit 10717ad
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions helpers/Lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
var async = require('async')
var EventEmitter = require('events').EventEmitter

var nullFunc = function(done){done()}



/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
},
Expand All @@ -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)
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "SnailJS <developers@snailjs.org>",
"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",
Expand Down
18 changes: 16 additions & 2 deletions test/Lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ 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()
})
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()
Expand Down Expand Up @@ -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)
})
})

0 comments on commit 10717ad

Please sign in to comment.