forked from MediaBrowser/Emby.ApiClient.Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
58 lines (40 loc) · 1.11 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
function getCallbacks(obj, name) {
if (!obj) {
throw new Error("obj cannot be null!");
}
obj._callbacks = obj._callbacks || {};
let list = obj._callbacks[name];
if (!list) {
obj._callbacks[name] = [];
list = obj._callbacks[name];
}
return list;
}
export default {
on(obj, eventName, fn) {
const list = getCallbacks(obj, eventName);
list.push(fn);
},
off(obj, eventName, fn) {
const list = getCallbacks(obj, eventName);
const i = list.indexOf(fn);
if (i !== -1) {
list.splice(i, 1);
}
},
trigger(obj, eventName) {
const eventObject = {
type: eventName
};
const eventArgs = [];
eventArgs.push(eventObject);
const additionalArgs = arguments[2] || [];
for (let i = 0, length = additionalArgs.length; i < length; i++) {
eventArgs.push(additionalArgs[i]);
}
const callbacks = getCallbacks(obj, eventName).slice(0);
callbacks.forEach(c => {
c.apply(obj, eventArgs);
});
}
};