/
events.js
173 lines (141 loc) · 4.27 KB
/
events.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* events.js
*/
import * as utils from './utils.js'
import { EVENTS, EVENT_ID_PATHS } from './constants.js';
import {ttlCollection} from './utils/ttlCollection.js';
import {config} from './config.js';
const TTL_CONFIG = 'eventHistoryTTL';
let eventTTL = null;
// keep a record of all events fired
const eventsFired = ttlCollection({
monotonic: true,
ttl: () => eventTTL,
})
config.getConfig(TTL_CONFIG, (val) => {
const previous = eventTTL;
val = val?.[TTL_CONFIG];
eventTTL = typeof val === 'number' ? val * 1000 : null;
if (previous !== eventTTL) {
eventsFired.refresh();
}
});
let slice = Array.prototype.slice;
let push = Array.prototype.push;
// define entire events
let allEvents = Object.values(EVENTS);
const idPaths = EVENT_ID_PATHS;
const _public = (function () {
let _handlers = {};
let _public = {};
/**
*
* @param {String} eventString The name of the event.
* @param {Array} args The payload emitted with the event.
* @private
*/
function _dispatch(eventString, args) {
utils.logMessage('Emitting event for: ' + eventString);
let eventPayload = args[0] || {};
let idPath = idPaths[eventString];
let key = eventPayload[idPath];
let event = _handlers[eventString] || { que: [] };
var eventKeys = Object.keys(event);
let callbacks = [];
// record the event:
eventsFired.add({
eventType: eventString,
args: eventPayload,
id: key,
elapsedTime: utils.getPerformanceNow(),
});
/**
* Push each specific callback to the `callbacks` array.
* If the `event` map has a key that matches the value of the
* event payload id path, e.g. `eventPayload[idPath]`, then apply
* each function in the `que` array as an argument to push to the
* `callbacks` array
*/
if (key && eventKeys.includes(key)) {
push.apply(callbacks, event[key].que);
}
/** Push each general callback to the `callbacks` array. */
push.apply(callbacks, event.que);
/** call each of the callbacks */
(callbacks || []).forEach(function (fn) {
if (!fn) return;
try {
fn.apply(null, args);
} catch (e) {
utils.logError('Error executing handler:', 'events.js', e, eventString);
}
});
}
function _checkAvailableEvent(event) {
return allEvents.includes(event)
}
_public.has = _checkAvailableEvent;
_public.on = function (eventString, handler, id) {
// check whether available event or not
if (_checkAvailableEvent(eventString)) {
let event = _handlers[eventString] || { que: [] };
if (id) {
event[id] = event[id] || { que: [] };
event[id].que.push(handler);
} else {
event.que.push(handler);
}
_handlers[eventString] = event;
} else {
utils.logError('Wrong event name : ' + eventString + ' Valid event names :' + allEvents);
}
};
_public.emit = function (event) {
let args = slice.call(arguments, 1);
_dispatch(event, args);
};
_public.off = function (eventString, handler, id) {
let event = _handlers[eventString];
if (utils.isEmpty(event) || (utils.isEmpty(event.que) && utils.isEmpty(event[id]))) {
return;
}
if (id && (utils.isEmpty(event[id]) || utils.isEmpty(event[id].que))) {
return;
}
if (id) {
(event[id].que || []).forEach(function (_handler) {
let que = event[id].que;
if (_handler === handler) {
que.splice(que.indexOf(_handler), 1);
}
});
} else {
(event.que || []).forEach(function (_handler) {
let que = event.que;
if (_handler === handler) {
que.splice(que.indexOf(_handler), 1);
}
});
}
_handlers[eventString] = event;
};
_public.get = function () {
return _handlers;
};
_public.addEvents = function (events) {
allEvents = allEvents.concat(events);
}
/**
* This method can return a copy of all the events fired
* @return {Array} array of events fired
*/
_public.getEvents = function () {
return eventsFired.toArray().map(val => Object.assign({}, val))
};
return _public;
}());
utils._setEventEmitter(_public.emit.bind(_public));
export const {on, off, get, getEvents, emit, addEvents, has} = _public;
export function clearEvents() {
eventsFired.clear();
}