Bus module fix.

This commit is contained in:
Braydon Fuller 2015-08-28 16:16:51 -04:00
parent 462ba233ed
commit 43ab4586e5
4 changed files with 36 additions and 30 deletions

View File

@ -5,16 +5,16 @@ var util = require('util');
function Bus(params) { function Bus(params) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.db = params.db; this.node = params.node;
} }
util.inherits(Bus, events.EventEmitter); util.inherits(Bus, events.EventEmitter);
Bus.prototype.subscribe = function(name) { Bus.prototype.subscribe = function(name) {
var events = this.db.getPublishEvents(); var events = this.node.db.getPublishEvents();
for(var i = 0; i < this.db.modules.length; i++) { for(var i in this.node.modules) {
var mod = this.db.modules[i]; var mod = this.node.modules[i];
events = events.concat(mod.getPublishEvents()); events = events.concat(mod.getPublishEvents());
} }
@ -29,10 +29,10 @@ Bus.prototype.subscribe = function(name) {
}; };
Bus.prototype.unsubscribe = function(name) { Bus.prototype.unsubscribe = function(name) {
var events = this.db.getPublishEvents(); var events = this.node.db.getPublishEvents();
for(var i = 0; i < this.db.modules.length; i++) { for(var i in this.node.modules) {
var mod = this.db.modules[i]; var mod = this.node.modules[i];
events = events.concat(mod.getPublishEvents()); events = events.concat(mod.getPublishEvents());
} }
@ -47,10 +47,10 @@ Bus.prototype.unsubscribe = function(name) {
}; };
Bus.prototype.close = function() { Bus.prototype.close = function() {
var events = this.db.getPublishEvents(); var events = this.node.db.getPublishEvents();
for(var i = 0; i < this.db.modules.length; i++) { for(var i in this.node.modules) {
var mod = this.db.modules[i]; var mod = this.node.modules[i];
events = events.concat(mod.getPublishEvents()); events = events.concat(mod.getPublishEvents());
} }

View File

@ -44,7 +44,7 @@ function Node(config) {
util.inherits(Node, EventEmitter); util.inherits(Node, EventEmitter);
Node.prototype.openBus = function() { Node.prototype.openBus = function() {
return new Bus({db: this.db}); return new Bus({node: this});
}; };
Node.prototype.addModule = function(service) { Node.prototype.addModule = function(service) {

View File

@ -18,9 +18,12 @@ describe('Bus', function() {
subscribe: subscribeDb subscribe: subscribeDb
} }
] ]
), )
modules: [ };
{ var node = {
db: db,
modules: {
module1: {
getPublishEvents: sinon.stub().returns([ getPublishEvents: sinon.stub().returns([
{ {
name: 'test', name: 'test',
@ -29,9 +32,9 @@ describe('Bus', function() {
} }
]) ])
} }
] }
}; };
var bus = new Bus({db: db}); var bus = new Bus({node: node});
bus.subscribe('dbtest', 'a', 'b', 'c'); bus.subscribe('dbtest', 'a', 'b', 'c');
bus.subscribe('test', 'a', 'b', 'c'); bus.subscribe('test', 'a', 'b', 'c');
subscribeModule.callCount.should.equal(1); subscribeModule.callCount.should.equal(1);
@ -59,9 +62,12 @@ describe('Bus', function() {
unsubscribe: unsubscribeDb unsubscribe: unsubscribeDb
} }
] ]
), )
modules: [ };
{ var node = {
db: db,
modules: {
module1: {
getPublishEvents: sinon.stub().returns([ getPublishEvents: sinon.stub().returns([
{ {
name: 'test', name: 'test',
@ -70,9 +76,9 @@ describe('Bus', function() {
} }
]) ])
} }
] }
}; };
var bus = new Bus({db: db}); var bus = new Bus({node: node});
bus.unsubscribe('dbtest', 'a', 'b', 'c'); bus.unsubscribe('dbtest', 'a', 'b', 'c');
bus.unsubscribe('test', 'a', 'b', 'c'); bus.unsubscribe('test', 'a', 'b', 'c');
unsubscribeModule.callCount.should.equal(1); unsubscribeModule.callCount.should.equal(1);
@ -100,9 +106,12 @@ describe('Bus', function() {
unsubscribe: unsubscribeDb unsubscribe: unsubscribeDb
} }
] ]
), )
modules: [ };
{ var node = {
db: db,
modules: {
module1: {
getPublishEvents: sinon.stub().returns([ getPublishEvents: sinon.stub().returns([
{ {
name: 'test', name: 'test',
@ -111,10 +120,9 @@ describe('Bus', function() {
} }
]) ])
} }
] }
}; };
var bus = new Bus({node: node});
var bus = new Bus({db: db});
bus.close(); bus.close();
unsubscribeDb.callCount.should.equal(1); unsubscribeDb.callCount.should.equal(1);

View File

@ -82,10 +82,8 @@ describe('Bitcore Node', function() {
describe('#openBus', function() { describe('#openBus', function() {
it('will create a new bus', function() { it('will create a new bus', function() {
var node = new Node({}); var node = new Node({});
var db = {};
node.db = db;
var bus = node.openBus(); var bus = node.openBus();
bus.db.should.equal(db); bus.node.should.equal(node);
}); });
}); });