-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (41 loc) · 1.55 KB
/
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
39
40
41
42
43
44
45
/**
* A tiny JS library to create a message channel using Publish/Subscribe pattern
* The events are stored as an object where keys will be the type of actions and values will be array of listeners
* There can be multiple listeners for a single action type
*/
class WalkyTalky {
constructor() {
this.events = {};
}
/**
* Function to subscribe to an event/action
* It checks for the actionType if it already exists, then adds the listener to the list of that particular actionType
* If the actionType doesn't already exists then it creates the same and assigns the listeners to it's list.
* @param {string} actionType
* @param {object} listener
* @memberof WalkyTalky
*/
subscribe(actionType, listener) {
// create the actionType if not yet created
if (!this.events[actionType]) this.events[actionType] = [];
// add the listener
this.events[actionType].push(listener);
}
/**
* Function to publish events/action
* The subscribed event/action will get this message
* This function iterates over the list of listeners and calls each of them with params for a particular actionType
* @param {string} actionType
* @param {object} data
* @memberof WalkyTalky
*/
publish(actionType, data) {
// return if the actionType doesn't exist, or there are no listeners
if (!this.events[actionType] || this.events[actionType].length < 1) return;
// send the event to all listeners
this.events[actionType].forEach((listener) => {
listener(data || {});
});
}
}
export default new WalkyTalky();