-
Notifications
You must be signed in to change notification settings - Fork 5
/
messenger.js
41 lines (38 loc) · 1.33 KB
/
messenger.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
39
40
41
const Fiber = Npm.require('fibers');
RPS._messenger = {
channels: {},
observers: {},
addObserver: function (observerKey, channel) {
if (!RPS._messenger.channels[channel]) {
RPS._messenger.channels[channel] = {};
}
RPS._messenger.channels[channel][observerKey] = true;
RPS._messenger.observers[observerKey] = channel;
RPS._sub(channel);
},
removeObserver: function (observerKey) {
const channel = RPS._messenger.observers[observerKey];
if (channel) {
delete RPS._messenger.channels[channel][observerKey];
if (_.isEmpty(RPS._messenger.channels[channel])) {
RPS._unsub(channel);
delete RPS._messenger.channels[channel];
}
}
delete RPS._messenger.observers[observerKey];
},
onMessage: function (channel, message, runWithFiber) {
_.each(RPS._messenger.channels[channel], function (flag, observerKey) {
const observer = RPS._observers[observerKey];
if (observer) {
if (runWithFiber) {
Fiber(function () {
observer.onMessage(message);
}).run();
} else {
observer.onMessage(message);
}
}
});
}
};