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-08-06 13:19:36 -07:00
|
|
|
var events = this.db.getPublishEvents();
|
|
|
|
|
|
|
|
for(var i = 0; i < this.db.modules.length; i++) {
|
2015-07-29 14:22:34 -07:00
|
|
|
var mod = this.db.modules[i];
|
2015-08-06 13:19:36 -07:00
|
|
|
events = events.concat(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-08-06 13:19:36 -07:00
|
|
|
var events = this.db.getPublishEvents();
|
|
|
|
|
|
|
|
for(var i = 0; i < this.db.modules.length; i++) {
|
2015-07-29 14:22:34 -07:00
|
|
|
var mod = this.db.modules[i];
|
2015-08-06 13:19:36 -07:00
|
|
|
events = events.concat(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() {
|
2015-08-06 13:19:36 -07:00
|
|
|
var events = this.db.getPublishEvents();
|
|
|
|
|
|
|
|
for(var i = 0; i < this.db.modules.length; i++) {
|
2015-08-04 13:34:53 -07:00
|
|
|
var mod = this.db.modules[i];
|
2015-08-06 13:19:36 -07:00
|
|
|
events = events.concat(mod.getPublishEvents());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe from all events
|
|
|
|
for (var j = 0; j < events.length; j++) {
|
|
|
|
var event = events[j];
|
|
|
|
event.unsubscribe.call(event.scope, this);
|
2015-08-04 13:34:53 -07:00
|
|
|
}
|
2015-08-03 13:35:38 -07:00
|
|
|
};
|
|
|
|
|
2015-07-29 10:36:23 -07:00
|
|
|
module.exports = Bus;
|