diff --git a/lib/bus.js b/lib/bus.js index 325c09e1..8c8afc55 100644 --- a/lib/bus.js +++ b/lib/bus.js @@ -5,16 +5,16 @@ var util = require('util'); function Bus(params) { events.EventEmitter.call(this); - this.db = params.db; + this.node = params.node; } util.inherits(Bus, events.EventEmitter); 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++) { - var mod = this.db.modules[i]; + for(var i in this.node.modules) { + var mod = this.node.modules[i]; events = events.concat(mod.getPublishEvents()); } @@ -29,10 +29,10 @@ Bus.prototype.subscribe = 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++) { - var mod = this.db.modules[i]; + for(var i in this.node.modules) { + var mod = this.node.modules[i]; events = events.concat(mod.getPublishEvents()); } @@ -47,10 +47,10 @@ Bus.prototype.unsubscribe = function(name) { }; 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++) { - var mod = this.db.modules[i]; + for(var i in this.node.modules) { + var mod = this.node.modules[i]; events = events.concat(mod.getPublishEvents()); } diff --git a/lib/node.js b/lib/node.js index 124a4e71..cc6fa3d5 100644 --- a/lib/node.js +++ b/lib/node.js @@ -44,7 +44,7 @@ function Node(config) { util.inherits(Node, EventEmitter); Node.prototype.openBus = function() { - return new Bus({db: this.db}); + return new Bus({node: this}); }; Node.prototype.addModule = function(service) { diff --git a/test/bus.unit.js b/test/bus.unit.js index 44c4c662..7c40b4e9 100644 --- a/test/bus.unit.js +++ b/test/bus.unit.js @@ -18,9 +18,12 @@ describe('Bus', function() { subscribe: subscribeDb } ] - ), - modules: [ - { + ) + }; + var node = { + db: db, + modules: { + module1: { getPublishEvents: sinon.stub().returns([ { 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('test', 'a', 'b', 'c'); subscribeModule.callCount.should.equal(1); @@ -59,9 +62,12 @@ describe('Bus', function() { unsubscribe: unsubscribeDb } ] - ), - modules: [ - { + ) + }; + var node = { + db: db, + modules: { + module1: { getPublishEvents: sinon.stub().returns([ { 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('test', 'a', 'b', 'c'); unsubscribeModule.callCount.should.equal(1); @@ -100,9 +106,12 @@ describe('Bus', function() { unsubscribe: unsubscribeDb } ] - ), - modules: [ - { + ) + }; + var node = { + db: db, + modules: { + module1: { getPublishEvents: sinon.stub().returns([ { name: 'test', @@ -111,10 +120,9 @@ describe('Bus', function() { } ]) } - ] + } }; - - var bus = new Bus({db: db}); + var bus = new Bus({node: node}); bus.close(); unsubscribeDb.callCount.should.equal(1); diff --git a/test/node.unit.js b/test/node.unit.js index 1f07ec0d..3f07475e 100644 --- a/test/node.unit.js +++ b/test/node.unit.js @@ -82,10 +82,8 @@ describe('Bitcore Node', function() { describe('#openBus', function() { it('will create a new bus', function() { var node = new Node({}); - var db = {}; - node.db = db; var bus = node.openBus(); - bus.db.should.equal(db); + bus.node.should.equal(node); }); });