-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
213 lines (185 loc) · 6.17 KB
/
mod.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
// Copyright (c) 2019 Denolibs authors. All rights reserved. MIT license.
interface ListenerFunction extends Function {
listener?: Function;
}
class EventEmitter {
public static defaultMaxListeners: number = 10;
private maxListeners: number | undefined;
private events: Map<string | symbol, Function[]>;
public constructor() {
this.events = new Map();
}
private _addListener(
eventName: string | symbol,
listener: Function,
prepend: boolean
): this {
this.emit("newListener", eventName, listener);
if (this.events.has(eventName)) {
const listeners = this.events.get(eventName) as Function[];
if (prepend) {
listeners.unshift(listener);
} else {
listeners.push(listener);
}
} else {
this.events.set(eventName, [listener]);
}
const max = this.getMaxListeners();
if (max > 0 && this.listenerCount(eventName) > max) {
const warning = new Error(
`Possible EventEmitter memory leak detected.
${this.listenerCount(eventName)} ${eventName.toString()} listeners.
Use emitter.setMaxListeners() to increase limit`
);
warning.name = "MaxListenersExceededWarning";
console.warn(warning);
}
return this;
}
public addListener(eventName: string | symbol, listener: Function): this {
return this._addListener(eventName, listener, false);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public emit(eventName: string | symbol, ...args: any[]): boolean {
if (this.events.has(eventName)) {
const listeners = (this.events.get(eventName) as Function[]).slice(); // We copy with slice() so array is not mutated during emit
for (const listener of listeners) {
try {
listener.apply(this, args);
} catch (err) {
this.emit("error", err);
}
}
return true;
} else if (eventName === "error") {
const errMsg = args.length > 0 ? args[0] : Error("Unhandled error.");
throw errMsg;
}
return false;
}
public eventNames(): [string | symbol] {
return Array.from(this.events.keys()) as [string | symbol];
}
public getMaxListeners(): number {
return this.maxListeners || EventEmitter.defaultMaxListeners;
}
public listenerCount(eventName: string | symbol): number {
if (this.events.has(eventName)) {
return (this.events.get(eventName) as Function[]).length;
} else {
return 0;
}
}
private _listeners(
target: EventEmitter,
eventName: string | symbol,
unwrap: boolean
): Function[] {
if (!target.events.has(eventName)) {
return [];
}
const eventListeners: ListenerFunction[] = target.events.get(
eventName
) as Function[];
return unwrap
? this.unwrapListeners(eventListeners)
: eventListeners.slice(0);
}
private unwrapListeners(arr: ListenerFunction[]): Function[] {
let unwrappedListeners: Function[] = new Array(arr.length) as Function[];
for (let i = 0; i < arr.length; i++) {
unwrappedListeners[i] = arr[i]["listener"] || arr[i];
}
return unwrappedListeners;
}
public listeners(eventName: string | symbol): Function[] {
return this._listeners(this, eventName, true);
}
public rawListeners(eventName: string | symbol): Function[] {
return this._listeners(this, eventName, false);
}
public off(eventName: string | symbol, listener: Function): this {
return this.removeListener(eventName, listener);
}
public on(eventName: string | symbol, listener: Function): this {
return this.addListener(eventName, listener);
}
public once(eventName: string | symbol, listener: Function): this {
const wrapped: Function = this.onceWrap(eventName, listener);
this.on(eventName, wrapped);
return this;
}
// Wrapped function that calls EventEmitter.removeListener(eventName, self) on execution.
private onceWrap(eventName: string | symbol, listener: Function): Function {
const wrapper: ListenerFunction = function (
this: {
eventName: string | symbol;
listener: Function;
rawListener: Function;
context: EventEmitter;
},
...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
): void {
this.context.removeListener(this.eventName, this.rawListener);
this.listener.apply(this.context, args);
};
const wrapperContext = {
eventName: eventName,
listener: listener,
rawListener: wrapper,
context: this
};
const wrapped = wrapper.bind(wrapperContext);
wrapperContext.rawListener = wrapped;
wrapped.listener = listener;
return wrapped;
}
public prependListener(eventName: string | symbol, listener: Function): this {
return this._addListener(eventName, listener, true);
}
public prependOnceListener(
eventName: string | symbol,
listener: Function
): this {
const wrapped: Function = this.onceWrap(eventName, listener);
this.prependListener(eventName, wrapped);
return this;
}
public removeAllListeners(eventName?: string | symbol): this {
if (this.events === undefined) {
return this;
}
if (eventName && this.events.has(eventName)) {
const listeners = (this.events.get(eventName) as Function[]).slice(); // Create a copy; We use it AFTER it's deleted.
this.events.delete(eventName);
for (const listener of listeners) {
this.emit("removeListener", eventName, listener);
}
} else {
const eventList: [string | symbol] = this.eventNames();
eventList.map((value: string | symbol) => {
this.removeAllListeners(value);
});
}
return this;
}
public removeListener(eventName: string | symbol, listener: Function): this {
if (this.events.has(eventName)) {
const arr: Function[] = this.events.get(eventName) as Function[];
if (arr.indexOf(listener) !== -1) {
arr.splice(arr.indexOf(listener), 1);
this.emit("removeListener", eventName, listener);
if (arr.length === 0) {
this.events.delete(eventName);
}
}
}
return this;
}
public setMaxListeners(n: number): this {
this.maxListeners = n;
return this;
}
}
export default EventEmitter;