Skip to content

Commit

Permalink
Merge pull request #21 from ph0bos/optimised-event-listener-counts
Browse files Browse the repository at this point in the history
increment version: 0.5.7
  • Loading branch information
ph0bos authored Nov 8, 2016
2 parents 84320b4 + f05258e commit e008582
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions lib/LeaderElection.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,26 @@ util.inherits(LeaderElection, EventEmitter);
function LeaderElection(client, path, id) {
var self = this;

self.watcher = function(){ self._watch() };
self.watcher = function(){ self._watch() };
self.disconnect = function () {
self._disconnect();
self.emit('error', new Error('disconnected from zk'));
};

self.client = client;
self.path = path + '/' + id;
self.znode = null;

self._isGroupLeader = false;

self.client.on('disconnected', function () {
self._disconnect();
self.emit('error', new Error('disconnected from zk'));
});
}

LeaderElection.prototype.start = function (callback) {
var self = this;
self._isWithdrawn = false;

// Make sure we only store one disconnect listener at a time.
self.client.removeListener('disconnected', self.disconnect);
self.client.once('disconnected', self.disconnect);

if (self.znode) {
this._watch();
return callback(new Error('already part of an election'));
Expand Down Expand Up @@ -108,6 +110,7 @@ LeaderElection.prototype.withdraw = function (callback) {
.remove(serviceInstancePath)
.commit(function(err){
self.znode = null;
self.client.removeListener('disconnected', self.disconnect);
callback(err);
});
};
Expand All @@ -134,7 +137,10 @@ LeaderElection.prototype.destroy = function (callback) {
}, function(){
self.client
.getClient()
.remove(self.path, callback);
.remove(self.path, function(err){
self.client.removeListener('disconnected', self.disconnect);
callback(err);
});
});
})
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zoologist",
"version": "0.5.6",
"version": "0.5.7",
"description": "A Curator-esque framework for ZooKeeper",
"main": "index.js",
"keywords": [
Expand Down

0 comments on commit e008582

Please sign in to comment.