-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.node.js
More file actions
375 lines (375 loc) · 10.2 KB
/
Copy pathevents.node.js
File metadata and controls
375 lines (375 loc) · 10.2 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
class WillNull {
val;
static NullErr = Error('Null values that may cause errors.');
constructor(val) {
this.val = val;
}
setValue(val) {
this.val = val;
}
isNull() {
return this.val === null;
}
expect(message) {
if (this.val !== null) {
return this.val;
}
throw Error(`${message}`);
}
unwrap() {
if (this.val !== null) {
return this.val;
}
throw WillNull.NullErr;
}
}
function Maybe(val) {
return new WillNull(val);
}
function Null() {
return new WillNull(null);
}
class Linked {
prev = Null();
next = Null();
raw = Null();
handler = Null();
connect(linked) {
linked.next.setValue(this.next.expect('Do not operate Linked alone'));
this.next.expect('Do not operate Linked alone')
.prev.setValue(linked);
this.next.setValue(linked);
linked.prev.setValue(this);
return linked;
}
disconnect() {
const prevErr = 'get prev fail at disconnect';
const nextErr = 'get next fail at disconnect';
this.prev.expect(prevErr)
.next
.setValue(this.next.expect(nextErr));
this.next.expect(nextErr)
.prev.setValue(this.prev.expect(prevErr));
this.prev.setValue(null);
this.next.setValue(null);
return this;
}
record(listener, raw) {
this.handler.setValue(listener);
if (raw) {
this.raw.setValue(raw);
}
}
}
class IndexedLinked {
thisArg;
rawRecord = new Map();
record = new Map();
Entry = new Linked();
End = new Linked();
_pointer = this.Entry;
constructor(thisArg = () => ({})) {
this.thisArg = thisArg;
this.Entry.next.setValue(this.End);
this.End.prev.setValue(this.Entry);
}
size() {
return this.record.size;
}
_recordNode(k, v) {
if (this.record.has(k)) {
return false;
}
this.record.set(k, v);
return true;
}
_recordRawNode(k, v) {
if (this.rawRecord.has(k)) {
return false;
}
this.rawRecord.set(k, v);
this._recordNode(v.handler.unwrap(), v);
return true;
}
_creator(listener) {
let node = new Linked();
node.record(listener);
return node;
}
_onceCreator(listener) {
let node = new Linked();
let self = this;
const wrapper = (...args) => {
try {
node.raw.expect('The function used to wrap is empty')
.apply(self.thisArg(), args);
}
finally {
self.deleteNode(node);
}
};
node.record(wrapper, listener);
return node;
}
put(listener) {
const node = this._creator(listener);
if (this._recordNode(listener, node)) {
this._pointer.connect(node);
this._pointer = node;
}
}
prepend(listener) {
const node = this._creator(listener);
if (this._recordNode(listener, node)) {
this.Entry.connect(node);
}
}
once(listener) {
const node = this._onceCreator(listener);
try {
if (this._recordRawNode(node.raw.unwrap(), node)) {
this._pointer.connect(node);
this._pointer = node;
}
}
finally { }
}
prependOnce(listener) {
const node = this._onceCreator(listener);
try {
if (this._recordRawNode(node.raw.unwrap(), node)) {
this.Entry.connect(node);
}
}
finally { }
}
deleteNode(node) {
try {
if (this._pointer === node) {
this._pointer = node.prev.unwrap();
}
if (!node.raw.isNull()) {
this.rawRecord.delete(node.raw.unwrap());
}
this.record.delete(node.handler.unwrap());
node.disconnect();
return true;
}
catch (_) {
return false;
}
}
delete(listener) {
if (this.rawRecord.has(listener)) {
const linked = Maybe(this.rawRecord.get(listener) || null).expect("Don't modify Linekd outside of IndexedLinked");
this.deleteNode(linked);
return true;
}
if (this.record.has(listener)) {
const linked = Maybe(this.record.get(listener) || null)
.expect("Don't modify Linekd outside of IndexedLinked");
this.deleteNode(linked);
return true;
}
return false;
}
free() {
let toDel = this.Entry.next.expect('Free error');
while (toDel !== this.End) {
const toDelNext = toDel.next.expect('Free error');
this.deleteNode(toDel);
toDel = toDelNext;
}
this._pointer = this.Entry;
}
}
exports.EventEmitter = class EventEmitter {
maxListeners = -1;
_events = {};
captureRejections = false;
thisArg = {};
setMaxListeners(size) {
this.maxListeners = size;
return this;
}
getMaxListeners() {
return this.maxListeners;
}
_thisGetter = () => this.thisArg;
_getEventLinked(type) {
let linked;
if (!(linked = this._events[type])) {
linked = this._events[type] = new IndexedLinked(this._thisGetter);
}
return linked;
}
_canAddNew(size) {
return this.maxListeners !== -1 && size === this.maxListeners;
}
_addListener(type, handler, prepend = false, once = false) {
this._callWatcherMethod('add', { type, handler, prepend, once });
const linked = this._getEventLinked(type);
if (this._canAddNew(linked.size())) {
this._emitError(RangeError('Listeners is full and cannot join a new listener, please use setMaxListeners to resize'));
return;
}
if (prepend) {
if (once) {
linked.prependOnce(handler);
}
else {
linked.prepend(handler);
}
return;
}
if (once) {
linked.once(handler);
}
else {
linked.put(handler);
}
}
addListener(type, handler) {
this._addListener(type, handler);
return this;
}
on(type, handler) {
this._addListener(type, handler);
return this;
}
prependListener(type, handler) {
this._addListener(type, handler, true);
return this;
}
_removeListener(type, handler) {
this._callWatcherMethod('remove', { type, handler });
const eventLinked = this._getEventLinked(type);
eventLinked.delete(handler);
return this;
}
removeListener(type, handler) {
return this._removeListener(type, handler);
}
off(type, handler) {
return this._removeListener(type, handler);
}
removeAllListeners(type) {
this._callWatcherMethod('removeAll', { type });
this._getEventLinked(type).free();
}
_emit(type, nullContext = false, ...args) {
this._callWatcherMethod('emit', { type, args });
const l = this._getEventLinked(type);
const ctx = this.thisArg;
this.thisArg = nullContext
? undefined
: ctx;
let cur = l.Entry.next.expect('EventEmitter$_emit');
while (cur !== l.End) {
const nextNode = cur.next.expect('EventEmitter$_emit');
try {
const returned = cur.handler.unwrap().apply(this.thisArg, args);
if (this.captureRejections && returned instanceof Promise) {
returned.catch(reason => this._emitError(reason));
}
}
catch (err) {
if (type === 'error') {
throw err;
}
else {
this._emitError(err);
}
}
cur = nextNode;
}
this.thisArg = ctx;
}
_emitError(err) {
const size = this.listenerCount('error');
if (size > 0) {
try {
this._emit('error', true, err);
}
catch (err) {
throw err;
}
return;
}
throw err;
}
emit(type, ...args) {
this._emit(type, false, ...args);
}
emitNone(type, ...args) {
this._emit(type, true, ...args);
}
once(type, handler) {
this._addListener(type, handler, false, true);
return this;
}
prependOnceListener(type, handler) {
this._addListener(type, handler, true, true);
return this;
}
listenerCount(type) {
return this._getEventLinked(type).size();
}
listeners(type) {
const ev = this._getEventLinked(type);
const listeners = [];
let cur = ev.Entry.next.unwrap();
while (cur !== ev.End) {
listeners.push(cur.handler.unwrap());
}
return listeners;
}
rawListeners(type) {
const ev = this._getEventLinked(type);
const listeners = [];
let cur = ev.Entry.next.unwrap();
while (cur !== ev.End) {
try {
listeners.push(cur.raw.unwrap());
}
catch (_) {
listeners.push(cur.handler.unwrap());
}
}
return listeners;
}
eventNames() {
return Reflect.ownKeys(this._events);
}
constructor(opt) {
if (opt) {
this.captureRejections = opt.captureRejections || false;
this.thisArg = opt.thisArg || {};
this._enableWatcher = opt.enableWatcher || false;
}
}
_watcher;
_enableWatcher;
connectWatcher(watcher) {
if (!this._enableWatcher) {
return;
}
this._watcher = watcher;
}
disconnectWatcher() {
if (this._watcher) {
this._watcher = null;
}
}
_callWatcherMethod(name, arg) {
if (!this._watcher) {
return;
}
try {
this._watcher[name]?.call(this, arg);
}
catch (er) {
this._emitError(er);
}
}
}