Add bus to subcribe to events.

This commit is contained in:
Braydon Fuller 2015-07-29 13:36:23 -04:00
parent bee86257ca
commit f2fdfed7e9
3 changed files with 53 additions and 5 deletions

43
lib/bus.js Normal file
View File

@ -0,0 +1,43 @@
'use strict';
var events = require('events');
var util = require('util');
function Bus(params) {
events.EventEmitter.call(this);
this.db = params.db;
}
util.inherits(Bus, events.EventEmitter);
Bus.prototype.subscribe = function(name) {
for (var i = 0; i < this.db.modules; i++) {
var module = this.db.modules[i];
var events = module.getEvents();
for (var j = 0; i < events.length; j++) {
var eventName = events[0];
var subscribeHandler = events[2];
var params = arguments.slice(1);
if (name === eventName) {
subscribeHandler.apply(events[1], params);
}
}
}
};
Bus.prototype.unsubscribe = function(name) {
for (var i = 0; i < this.db.modules; i++) {
var module = this.db.modules[i];
var events = module.getEvents();
for (var j = 0; i < events.length; j++) {
var eventName = events[0];
var unsubscribeHandler = events[3];
var params = arguments.slice(1);
if (name === eventName) {
unsubscribeHandler.apply(events[1], params);
}
}
}
};
module.exports = Bus;

View File

@ -16,6 +16,7 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var genesis = require('./genesis.json'); var genesis = require('./genesis.json');
var daemon = require('./daemon'); var daemon = require('./daemon');
var Bus = require('./bus');
function Node(config) { function Node(config) {
BaseNode.call(this, config); BaseNode.call(this, config);
@ -24,6 +25,10 @@ function Node(config) {
util.inherits(Node, BaseNode); util.inherits(Node, BaseNode);
Node.prototype.openBus = function() {
return new Bus({db: this.db});
};
Node.prototype._loadConfiguration = function(config) { Node.prototype._loadConfiguration = function(config) {
var self = this; var self = this;
this._loadBitcoinConf(config); this._loadBitcoinConf(config);

View File

@ -38,14 +38,14 @@
"bitcoind" "bitcoind"
], ],
"dependencies": { "dependencies": {
"async": "1.3.0",
"bindings": "^1.2.1", "bindings": "^1.2.1",
"mkdirp": "0.5.0",
"nan": "1.3.0",
"tiny": "0.0.10",
"chainlib": "^0.1.1", "chainlib": "^0.1.1",
"errno": "^0.1.2", "errno": "^0.1.2",
"async": "1.3.0", "memdown": "^1.0.0",
"memdown": "^1.0.0" "mkdirp": "0.5.0",
"nan": "1.3.0",
"tiny": "0.0.10"
}, },
"devDependencies": { "devDependencies": {
"benchmark": "1.0.0", "benchmark": "1.0.0",