-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-dispatch.js
More file actions
34 lines (29 loc) · 863 Bytes
/
Copy pathevent-dispatch.js
File metadata and controls
34 lines (29 loc) · 863 Bytes
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
/**
* @desc Emits custom events of your chosing
*
* @return {EventDispatch}
*/
var EventDispatch = function () {
/**
* @desc Dispatch a custom event
* @param {string} name - event name
* @param {Callback Function} event - contains the data passed to the event
*/
this.dispatch = function (name, event) {
let callbacks = this[name];
if (callbacks) callbacks.forEach(callback => callback(event));
return this;
};
/**
* @desc Listen to a specific event
* @param {string} name - event name
* @param {function} callback - callback function
*/
this.on = function (name, callback) {
let callbacks = this[name];
if (!callbacks) this[name] = [callback];
else callbacks.push(callback);
return this;
};
}
exports.EventDispatch = EventDispatch;