-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
229 lines (194 loc) · 6.09 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
export interface WrappedFunction extends Function {
listener: Function;
}
class EventEmitter {
private events: Map<string | symbol, Array<Function>> = new Map();
private maxListeners?: number;
#defaultMaxListeners = 10;
get defaultMaxListeners() {
return this.#defaultMaxListeners;
}
set defaultMaxListeners(n) {
if (Number.isInteger(n) || n < 0) {
const error = new RangeError(
'The value of "defaultMaxListeners" is out of range. It must be a non-negative integer. Received ' +
n +
".",
);
throw error;
}
this.#defaultMaxListeners = n;
}
addListener(eventName: string | symbol, listener: Function) {
return this.on(eventName, listener);
}
emit(eventName: string | symbol, ...args: unknown[]) {
const listeners = this.events.get(eventName);
if (listeners === undefined) {
if (eventName === "error") {
const error = args[0];
if (error instanceof Error) throw error;
throw new Error("Unhandled error.");
}
return false;
}
const copyListeners = [...listeners];
for (const listener of copyListeners) {
listener.apply(this, args);
}
return true;
}
setMaxListeners(n: number) {
if (!Number.isInteger(n) || n < 0) {
throw new RangeError(
'The value of "n" is out of range. It must be a non-negative integer. Received ' +
n +
".",
);
}
this.maxListeners = n;
return this;
}
getMaxListeners() {
if (this.maxListeners === undefined) {
return this.defaultMaxListeners;
}
return this.maxListeners;
}
listenerCount(eventName: string | symbol) {
const events = this.events.get(eventName);
return events === undefined ? 0 : events.length;
}
eventNames() {
return Reflect.ownKeys(this.events);
}
listeners(eventName: string | symbol) {
const listeners = this.events.get(eventName);
return listeners === undefined ? [] : listeners;
}
off(eventName: string | symbol, listener: Function) {
return this.removeListener(eventName, listener);
}
on(eventName: string | symbol, listener: Function, prepend?: boolean): this {
if (this.events.has(eventName) === false) {
this.events.set(eventName, []);
}
const events = this.events.get(eventName) as any;
if (prepend) {
events.unshift(listener);
} else {
events.push(listener);
}
// newListener
if (eventName !== "newListener" && this.events.has("newListener")) {
this.emit("newListener", eventName, listener);
}
// warn
const maxListener = this.getMaxListeners();
const eventLength = events.length;
if (maxListener > 0 && eventLength > maxListener && !events.warned) {
events.warned = true;
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;
}
removeAllListeners(eventName: string | symbol) {
const events = this.events;
// Not listening for removeListener, no need to emit
if (!events.has("removeListener")) {
if (arguments.length === 0) {
this.events = new Map();
} else if (events.has(eventName)) {
events.delete(eventName);
}
return this;
}
// Emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (const key of events.keys()) {
if (key === "removeListener") continue;
this.removeAllListeners(key);
}
this.removeAllListeners("removeListener");
this.events = new Map();
return this;
}
const listeners = events.get(eventName);
if (listeners !== undefined) {
listeners.map((listener) => {
this.removeListener(eventName, listener);
});
}
return this;
}
removeListener(eventName: string | symbol, listener: Function) {
const events = this.events;
if (events.size === 0) return this;
const list = events.get(eventName);
if (list === undefined) return this;
const index = list.findIndex((item) =>
item === listener || (item as WrappedFunction).listener === listener
);
if (index === -1) return this;
list.splice(index, 1);
if (list.length === 0) this.events.delete(eventName);
if (events.has("removeListener")) {
this.emit("removeListener", eventName, listener);
}
return this;
}
once(eventName: string | symbol, listener: Function): this {
this.on(eventName, this.onceWrap(eventName, listener));
return this;
}
private onceWrap(
eventName: string | symbol,
listener: Function,
): WrappedFunction {
const wrapper = function (
this: {
eventName: string | symbol;
listener: Function;
wrapedListener: Function;
context: EventEmitter;
},
...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
): void {
this.context.removeListener(this.eventName, this.wrapedListener);
this.listener.apply(this.context, args);
};
const wrapperContext = {
eventName: eventName,
listener: listener,
wrapedListener: (wrapper as unknown) as WrappedFunction,
context: this,
};
const wrapped = (wrapper.bind(
wrapperContext,
) as unknown) as WrappedFunction;
wrapperContext.wrapedListener = wrapped;
wrapped.listener = listener;
return wrapped as WrappedFunction;
}
prependListener(eventName: string | symbol, listener: Function) {
return this.on(eventName, listener, true);
}
prependOnceListener(eventName: string | symbol, listener: Function) {
this.prependListener(eventName, this.onceWrap(eventName, listener));
return this;
}
rawListeners(eventName: string | symbol) {
const events = this.events;
if (events === undefined) return [];
const listeners = events.get(eventName);
if (listeners === undefined) return [];
return [...listeners];
}
}
export default EventEmitter;