Comparing version 1.0.3 to 1.0.6
52
index.js
@@ -8,3 +8,3 @@ /** | ||
var Emitter = function(pubClient, subClient){ | ||
var Emitter = function (pubClient, subClient) { | ||
var _this = this; | ||
@@ -17,14 +17,14 @@ EventEmitter.call(this); | ||
subClient.on('message', function(channel, msg){ | ||
subClient.on('message', function (channel, msg) { | ||
var count = _this.listenerCount(channel); | ||
if(count){ | ||
if (count) { | ||
var args; | ||
try{ | ||
try { | ||
args = JSON.parse(msg); | ||
}catch(err){ | ||
} catch (err) { | ||
console.error('Parsing event message', err); | ||
} | ||
if(args[0] !== _this.uuid){ | ||
if (args[0] !== _this.uuid) { | ||
args[0] = channel; | ||
@@ -39,3 +39,5 @@ _this.emit.apply(_this, args); | ||
Emitter.prototype.on = function(){ | ||
// KLUDGE, it is not possible to listen local and global for the same event in | ||
// different parts of the code. | ||
Emitter.prototype.on = function (evt, listener, isGlobal) { | ||
var _this = this; | ||
@@ -45,14 +47,17 @@ var args = Array.prototype.slice.call(arguments); | ||
return new Promise(function(resolve, reject){ | ||
_this.subClient.subscribe(args[0], function(err){ | ||
if(err){ | ||
reject(err); | ||
}else{ | ||
resolve(); | ||
} | ||
}); | ||
}) | ||
if (isGlobal) { | ||
return new Promise(function (resolve, reject) { | ||
_this.subClient.subscribe(args[0], function (err) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}) | ||
} | ||
return Promise.resolve(void 0); | ||
} | ||
Emitter.prototype.distEmit = function(evt){ | ||
Emitter.prototype.distEmit = function (evt) { | ||
var _this = this; | ||
@@ -65,7 +70,7 @@ var args = Array.prototype.slice.call(arguments); | ||
// Emit to other nodes | ||
return new Promise(function(resolve, reject){ | ||
_this.pubClient.publish(evt, JSON.stringify(args), function(err){ | ||
if(err){ | ||
return new Promise(function (resolve, reject) { | ||
_this.pubClient.publish(evt, JSON.stringify(args), function (err) { | ||
if (err) { | ||
reject(err); | ||
}else{ | ||
} else { | ||
resolve(); | ||
@@ -77,3 +82,3 @@ } | ||
Emitter.prototype.off = Emitter.prototype.removeListener = function(evt){ | ||
Emitter.prototype.off = Emitter.prototype.removeListener = function (evt, listener) { | ||
var _this = this; | ||
@@ -83,3 +88,4 @@ var args = Array.prototype.slice.call(arguments); | ||
if(!_this.listenerCount(evt)){ | ||
// TODO: we should take into consideration isGlobal. | ||
if (!_this.listenerCount(evt)) { | ||
_this.subClient.unsubscribe(evt); | ||
@@ -86,0 +92,0 @@ } |
{ | ||
"name": "disturbed", | ||
"version": "1.0.3", | ||
"version": "1.0.6", | ||
"description": "A distributed event emitter, both for client and nodejs servers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,3 +27,3 @@ | ||
it('should emit to a local listener', function (done) { | ||
it.only('should emit to a local listener', function (done) { | ||
var eventEmitter = new disturbed(pubClient, subClient) | ||
@@ -39,5 +39,6 @@ var counter = 0; | ||
done(); | ||
}, true).then(function(){ | ||
console.log("eoriwpoeriwe"); | ||
eventEmitter.distEmit('test', 1, 2, 3); | ||
}); | ||
eventEmitter.distEmit('test', 1, 2, 3); | ||
}); | ||
@@ -58,3 +59,3 @@ | ||
expect(counter1).equal(1); | ||
}), | ||
}, true), | ||
eventEmitter2.on('test', function (a, b, c) { | ||
@@ -69,3 +70,3 @@ counter2++; | ||
} | ||
}) | ||
}, true) | ||
).then(function () { | ||
@@ -72,0 +73,0 @@ eventEmitter1.distEmit('test', 1, 2, 3); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6868
151