forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
155 lines (128 loc) · 3.89 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
/**
* events.js
*/
import * as utils from './utils.js'
import CONSTANTS from './constants.json';
var slice = Array.prototype.slice;
var push = Array.prototype.push;
// define entire events
// var allEvents = ['bidRequested','bidResponse','bidWon','bidTimeout'];
var allEvents = utils._map(CONSTANTS.EVENTS, function (v) {
return v;
});
var idPaths = CONSTANTS.EVENT_ID_PATHS;
// keep a record of all events fired
var eventsFired = [];
const _public = (function () {
var _handlers = {};
var _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);
var eventPayload = args[0] || {};
var idPath = idPaths[eventString];
var key = eventPayload[idPath];
var event = _handlers[eventString] || { que: [] };
var eventKeys = utils._map(event, function (v, k) {
return k;
});
var callbacks = [];
// record the event:
eventsFired.push({
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 && utils.contains(eventKeys, 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 */
utils._each(callbacks, function (fn) {
if (!fn) return;
try {
fn.apply(null, args);
} catch (e) {
utils.logError('Error executing handler:', 'events.js', e);
}
});
}
function _checkAvailableEvent(event) {
return utils.contains(allEvents, event);
}
_public.on = function (eventString, handler, id) {
// check whether available event or not
if (_checkAvailableEvent(eventString)) {
var 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) {
var args = slice.call(arguments, 1);
_dispatch(event, args);
};
_public.off = function (eventString, handler, id) {
var 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) {
utils._each(event[id].que, function (_handler) {
var que = event[id].que;
if (_handler === handler) {
que.splice(que.indexOf(_handler), 1);
}
});
} else {
utils._each(event.que, function (_handler) {
var que = event.que;
if (_handler === handler) {
que.splice(que.indexOf(_handler), 1);
}
});
}
_handlers[eventString] = event;
};
_public.get = function () {
return _handlers;
};
/**
* This method can return a copy of all the events fired
* @return {Array} array of events fired
*/
_public.getEvents = function () {
var arrayCopy = [];
utils._each(eventsFired, function (value) {
var newProp = Object.assign({}, value);
arrayCopy.push(newProp);
});
return arrayCopy;
};
return _public;
}());
utils._setEventEmitter(_public.emit.bind(_public));
export const {on, off, get, getEvents, emit} = _public;