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);
|
2015-08-28 13:16:51 -07:00
|
|
|
this.node = params.node;
|
2015-07-29 10:36:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(Bus, events.EventEmitter);
|
|
|
|
|
|
|
|
Bus.prototype.subscribe = function(name) {
|
2015-08-31 06:00:00 -07:00
|
|
|
var events = [];
|
2015-08-06 13:19:36 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
for(var i in this.node.services) {
|
|
|
|
var service = this.node.services[i];
|
|
|
|
events = events.concat(service.getPublishEvents());
|
2015-08-06 13:19:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-31 06:00:00 -07:00
|
|
|
var events = [];
|
2015-08-06 13:19:36 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
for(var i in this.node.services) {
|
|
|
|
var service = this.node.services[i];
|
|
|
|
events = events.concat(service.getPublishEvents());
|
2015-08-06 13:19:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-31 06:00:00 -07:00
|
|
|
var events = [];
|
2015-08-06 13:19:36 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
for(var i in this.node.services) {
|
|
|
|
var service = this.node.services[i];
|
|
|
|
events = events.concat(service.getPublishEvents());
|
2015-08-06 13:19:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|