bitcore-node-zcash/lib/bus.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-29 10:36:23 -07:00
'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) {
2015-07-29 14:22:34 -07:00
for (var i = 0; i < this.db.modules.length; i++) {
var mod = this.db.modules[i];
var events = mod.getPublishEvents();
for (var j = 0; j < events.length; j++) {
var event = events[j];
var params = Array.prototype.slice.call(arguments).slice(1);
params.unshift(this);
if (name === event.name) {
event.subscribe.apply(event.scope, params);
2015-07-29 10:36:23 -07:00
}
}
}
};
Bus.prototype.unsubscribe = function(name) {
2015-07-29 14:22:34 -07:00
for (var i = 0; i < this.db.modules.length; i++) {
var mod = this.db.modules[i];
var events = mod.getPublishEvents();
for (var j = 0; j < events.length; j++) {
var event = events[j];
var params = Array.prototype.slice.call(arguments).slice(1);
params.unshift(this);
if (name === event.name) {
event.unsubscribe.apply(event.scope, params);
2015-07-29 10:36:23 -07:00
}
}
}
};
2015-08-03 13:35:38 -07:00
Bus.prototype.close = function() {
// Unsubscribe from all events
for (var i = 0; i < this.db.modules.length; i++) {
var mod = this.db.modules[i];
var events = mod.getPublishEvents();
for (var j = 0; j < events.length; j++) {
var event = events[j];
event.unsubscribe.call(event.scope, this);
}
}
2015-08-03 13:35:38 -07:00
};
2015-07-29 10:36:23 -07:00
module.exports = Bus;