You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/pubsub.js

74 lines
1.5 KiB
JavaScript

'use strict';
var EventEmitter = require('events');
var nconf = require('nconf');
var real;
6 years ago
var noCluster;
var singleHost;
function get() {
if (real) {
return real;
}
var pubsub;
if (nconf.get('isCluster') === 'false') {
6 years ago
if (noCluster) {
real = noCluster;
return real;
}
noCluster = new EventEmitter();
noCluster.publish = noCluster.emit.bind(noCluster);
pubsub = noCluster;
} else if (nconf.get('singleHostCluster')) {
6 years ago
if (singleHost) {
real = singleHost;
return real;
}
singleHost = new EventEmitter();
6 years ago
if (!process.send) {
6 years ago
singleHost.publish = singleHost.emit.bind(singleHost);
6 years ago
} else {
6 years ago
singleHost.publish = function (event, data) {
process.send({
action: 'pubsub',
event: event,
data: data,
});
6 years ago
};
process.on('message', function (message) {
if (message && typeof message === 'object' && message.action === 'pubsub') {
6 years ago
singleHost.emit(message.event, message.data);
6 years ago
}
});
}
6 years ago
pubsub = singleHost;
} else if (nconf.get('redis')) {
pubsub = require('./database/redis/pubsub');
} else if (nconf.get('mongo')) {
pubsub = require('./database/mongo/pubsub');
} else if (nconf.get('postgres')) {
pubsub = require('./database/postgres/pubsub');
}
real = pubsub;
return pubsub;
}
module.exports = {
publish: function (event, data) {
get().publish(event, data);
},
on: function (event, callback) {
get().on(event, callback);
},
removeAllListeners: function (event) {
get().removeAllListeners(event);
},
6 years ago
reset: function () {
real = null;
},
};