-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.js
106 lines (96 loc) · 3.08 KB
/
emitter.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
*
* A simple event emitter with Functional Programming juice.
* Credit: Inspired by the article by Dave Atchley @ https://www.datchley.name/es6-eventemitter/
*/
export const eventEmitter = () => {
let events = new Map();
/**
* Utility functions
* @private
*/
function isValidCallback (fn) {
return typeof fn === 'function';
}
function isValidEvent (event) {
return events.has(event) && events.get(event);
}
/**
* Attaches a callback to an event.
*
* @param {string} event The event name.
* @param {Object} fn The callback function to invoke when event is emitted.
* @returns {number} Returns 1 for success. No fool proofing. GIGO.
*/
function on (event, fn) {
events.has(event) || events.set(event, []);
return events.get(event).push(fn);
}
/**
* Removes a given callback from the provided event's callback queue.
*
* @param {string} event The event name.
* @param {Object} fn The callback function to be removed from the callback queue for the given event.
* @returns {boolean|Object} Returns a Map Object if successful, false otherwise.
*/
function off (event, fn) {
const callbackQueue = isValidEvent(event);
return callbackQueue && callbackQueue.some(callback => {
return isValidCallback(callback) && callback === fn;
}) && events.set(event, callbackQueue
.filter(callback => {
return isValidCallback(callback) && callback !== fn;
}));
}
/**
* Attaches a given callback to an event for a single execution.
*
* @param {string} event The event name.
* @param {Object} fn The callback function to be invoked one time when the event is emitted.
* @returns {number} Returns 1 for success.
*/
function once (event, callback) {
const single = (...args) => {
args.length ? callback(...args) : callback();
return clear(event);
};
return on(event, single);
}
/**
* Invokes the callbacks attached to a single event.
*
* @param {string} event The event name.
* @param {Object} args The arguments passed to the callback.
*/
function emit (event, ...args) {
const callbackQueue = isValidEvent(event);
callbackQueue && callbackQueue.forEach(callback => {
callback(...args);
});
}
/**
* Removes a given event from the listeners completely.
*
* @param {string} event The event name.
* @returns {boolean} Returns true for success, false otherwise.
*/
function clear (event) {
const callbackQueue = isValidEvent(event);
return callbackQueue && events.delete(event);
}
/**
* Clears all the listeners for a given emitter.
*
*/
function destroy () {
return events.clear();
}
return Object.freeze({
on: on,
off: off,
once: once,
emit: emit,
clear: clear,
destroy: destroy
});
};