-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
38 lines (32 loc) · 816 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const Promise = require('bluebird');
let Resolver = function() {
let channels = {};
function getSubscribers(channel) {
channels[channel] = channels[channel] || [];
return channels[channel];
}
function setSubscriber(channel, callback) {
getSubscribers(channel);
channels[channel].push(callback);
}
return {
publish: function(channel, config, data) {
config = config || {
promiseMethod: 'all'
};
let promises = [],
channels = getSubscribers(channel);
if(channels.length > 0) {
for (var i = 0; i < channels.length; i++) {
promises.push(channels[i](data));
}
return Promise[config.promiseMethod](promises);
}
return Promise.resolve();
},
subscribe: function(channel, callback) {
setSubscriber(channel, callback);
}
}
}
module.exports = Resolver;