-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListenerList.ts
247 lines (196 loc) · 8.58 KB
/
ListenerList.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/** A listener/event handler class - manages a list of listener functions and allows events to be sent to the listeners
* @author TeamworkGuy2
* @param <E> the event type
* @param <L> the listener function signature
*/
class ListenerList<E, L extends (...args: any[]) => void> implements Events.ListenerList<E, L> {
private listeners: L[];
/** keeps track of the number of times that each listeners function can be called before it should be removed, -1 indicates infinite calls */
private listenerCallsUntilRemoval: number[];
private fireEventsSuccessCallback: ((res: any[]) => void) | null;
private fireEventsFailureCallback: ((err: any) => void) | null;
private listenerAddedCallback: ((listener: L) => void) | null;
private listenerRemovedCallback: ((listener: L) => void) | null;
constructor() {
// same as reset() to appease typescript 'strict'
this.listeners = [];
this.listenerCallsUntilRemoval = [];
this.fireEventsSuccessCallback = null;
this.fireEventsFailureCallback = null;
this.listenerAddedCallback = null;
this.listenerRemovedCallback = null;
}
public reset() {
this.listeners = [];
this.listenerCallsUntilRemoval = [];
this.fireEventsSuccessCallback = null;
this.fireEventsFailureCallback = null;
this.listenerAddedCallback = null;
this.listenerRemovedCallback = null;
}
public getListeners(): L[] {
return this.listeners;
}
public getFireEventsSuccessCallback(): ((res: any[]) => void) | null {
return this.fireEventsSuccessCallback;
}
public setFireEventsSuccessCallback(cb: (res: any[]) => void) {
ListenerList.checkCallback(cb, "fire events success");
this.fireEventsSuccessCallback = cb;
}
public getFireEventsFailureCallback(): ((err: any) => void) | null {
return this.fireEventsFailureCallback;
}
public setFireEventsFailureCallback(cb: (err: any) => void) {
ListenerList.checkCallback(cb, "fire events failure");
this.fireEventsFailureCallback = cb;
}
public getListenerAddedCallback(): ((listener: L) => void) | null {
return this.listenerAddedCallback;
}
public setListenerAddedCallback(cb: (listener: L) => void) {
ListenerList.checkCallback(cb, "fire events success");
this.listenerAddedCallback = cb;
}
public getListenerRemovedCallback(): ((listener: L) => void) | null {
return this.listenerRemovedCallback;
}
public setListenerRemovedCallback(cb: (listener: L) => void) {
ListenerList.checkCallback(cb, "fire events success");
this.listenerRemovedCallback = cb;
}
/** Add a listener function that is called whenever a new customer is added to the bid via the UI
* @param listener a listener function that is passed the new customer added to the bid
*/
public addListener(listener: L) {
this.addNTimeListener(-1, listener);
}
public addOneTimeListener(listener: L) {
this.addNTimeListener(1, listener);
}
/** Add a listener which is removed after a certain number of calls
* @param removeAfterNCalls the number of times to call this listener before removing it, -1 indicates infinite
* @param listener the listener function
*/
public addNTimeListener(removeAfterNCalls: number, listener: L) {
if (typeof listener !== "function") {
throw new Error("cannot add listener " + listener);
}
if (!Number.isInteger(removeAfterNCalls)) {
throw new Error("cannot add listener with non-integer number of calls before removal '" + removeAfterNCalls + "'");
}
this.listeners.push(listener);
this.listenerCallsUntilRemoval.push(removeAfterNCalls);
if (this.listenerAddedCallback) {
this.listenerAddedCallback(listener);
}
}
/** Remove a listener function from being called whenever a new customer is added to a bid via the UI
* @param listener a listener function that was previously registered with this ListenerList via 'addListener(listener)'
*/
public removeListener(listener: L) {
if (typeof listener !== "function") {
throw new Error("cannot remove listener " + listener);
}
var index = this.listeners.indexOf(listener);
if (index > -1 && index < this.listeners.length) {
this.removeListenerAt(index);
}
}
public removeListenerAt(index: number) {
var listener = this.listeners[index];
ListenerList.fastRemoveIndex(this.listeners, index);
ListenerList.fastRemoveIndex(this.listenerCallsUntilRemoval, index);
if (this.listenerRemovedCallback) {
this.listenerRemovedCallback(listener);
}
}
/**
* @param event: the event to pass to the event listener functions
* @param customListenerCaller: a function call that takes a listener and event and should fire that event to the listener,
* overrides this handler default behavior 'listener.apply(thisArg, args)'
* @param customListenerCallsDoneCb: if provided, a function to call when all the listeners have been called, in place of 'this.fireEventsSuccessCallback'
*/
public fireEvent(event: E, customListenerCaller?: (listener: L, args: [E], index: number, total: number) => any, customListenerCallsDoneCb?: (event: E) => void) {
var that = this;
var errorOccurred = false;
var useCustomCaller = (typeof customListenerCaller === "function");
function callListenerProxy(listener: L, thisArg: any, args: [E], k: number, size: number) {
var res: any = null;
try {
if (useCustomCaller) {
res = (<any>customListenerCaller)(listener, args, k, size);
}
else {
res = listener.apply(thisArg, args);
}
} catch (err) {
errorOccurred = true;
if (typeof that.fireEventsFailureCallback === "function") {
that.fireEventsFailureCallback(err);
}
else {
throw new Error(<any>err);
}
}
return res;
}
var params: [E] = [event];
var results: any[] = [];
for (var listenerCount = that.listeners.length, i = listenerCount - 1; i > -1; i--) {
var listener = that.listeners[i];
var remainingCallCount = that.listenerCallsUntilRemoval[i];
if (remainingCallCount > 0) {
that.listenerCallsUntilRemoval[i]--;
remainingCallCount--;
}
var res = null;
if (typeof listener === "function") {
res = callListenerProxy(listener, undefined, params, i, listenerCount);
}
results.push(res);
if (remainingCallCount === 0) {
that.removeListenerAt(i);
}
}
if (typeof customListenerCallsDoneCb === "function") {
customListenerCallsDoneCb(event);
}
else {
if (!errorOccurred && typeof that.fireEventsSuccessCallback === "function") {
that.fireEventsSuccessCallback(results);
}
}
}
/** Check if a function argument is a non-null function */
private static checkCallback(cb: any, msg: string) {
if (typeof cb !== "function") {
throw new Error(msg + " callback '" + cb + "' must be a function");
}
}
// ==== Copied from 'ts-mortar' Arrays.ts ====
/** Remove a value at a specific index from an array without creating a new array of splicing the array.
* NOTE: the returned order of the array's elements is not defined.
* @param ary: the array of values to remove the index value from
* @param index: the index of the value to remove from the array
* @return 'ary' of values with the specified index removed
*/
private static fastRemoveIndex<E>(ary: E[], index: number): E[] {
var aryLen = ary != null ? ary.length : 0;
if (aryLen === 0) {
return ary;
}
if (aryLen > 1) {
ary[index] = ary[aryLen - 1];
}
ary.pop();
return ary;
}
public static newInst<E1>(): ListenerList<E1, (event: E1) => void>;
public static newInst<E1, L1 extends (...args: any[]) => void>(): ListenerList<E1, L1>;
public static newInst<E1, L1 extends (...args: any[]) => void>() {
return new ListenerList<E1, L1>();
}
}
export = ListenerList;