/
MessageBusUtils.js
44 lines (40 loc) · 1.07 KB
/
MessageBusUtils.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
42
43
44
import { Hub } from '@aws-amplify/core';
const busListeners = {};
/** Adds a listener to under the UNIQUE name, to the channel
* If a listener with the name already exists, it will be removed
* before this one is added
* @param channel
* @param name
* @param callback
*/
export function registerListener(channel, name, callback) {
const previousListener = busListeners[name];
if (!!previousListener) {
Hub.remove(channel, previousListener);
}
busListeners[name] = callback;
Hub.listen(channel, busListeners[name]);
}
export function hasListener(channel, name){
const previousListener = busListeners[name];
return !!previousListener;
}
/**
* Removes a listener with the UNIQUE name, from the channel.
* @param channel
* @param name
*/
export function removeListener(channel, name) {
const listener = busListeners[name];
if (!!listener) {
Hub.remove(channel, listener);
}
}
/**
* Pushes a message out ot the listeners of the channel
* @param channel
* @param message
*/
export function pushMessage(channel, message) {
Hub.dispatch(channel, message);
}