-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor EventEmitter to new contract
Refs: #267
- Loading branch information
1 parent
d90f5ea
commit 63fde2f
Showing
3 changed files
with
74 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,85 @@ | ||
'use strict'; | ||
|
||
class EventEmitter { | ||
constructor() { | ||
constructor(options = {}) { | ||
this.events = new Map(); | ||
this.maxListenersCount = 10; | ||
this.maxListeners = options.maxListeners || 10; | ||
} | ||
|
||
getMaxListeners() { | ||
return this.maxListenersCount; | ||
} | ||
|
||
listenerCount(name) { | ||
const event = this.events.get(name); | ||
if (event) return event.size; | ||
return 0; | ||
emit(name, ...args) { | ||
const listeners = this.events.get(name); | ||
if (!listeners) return null; | ||
const promises = listeners.map((fn) => fn(...args)); | ||
return Promise.all(promises); | ||
} | ||
|
||
on(name, fn) { | ||
const event = this.events.get(name); | ||
if (event) { | ||
event.add(fn); | ||
const tooManyListeners = event.size > this.maxListenersCount; | ||
if (tooManyListeners) { | ||
const name = 'MaxListenersExceededWarning'; | ||
const warn = 'Possible EventEmitter memory leak detected'; | ||
const max = `Current maxListenersCount is ${this.maxListenersCount}`; | ||
const hint = 'Hint: avoid adding listeners in loops'; | ||
console.warn(`${name}: ${warn}. ${max}. ${hint}`); | ||
} | ||
} else { | ||
this.events.set(name, new Set([fn])); | ||
const listeners = this.events.get(name); | ||
if (!listeners) return void this.events.set(name, [fn]); | ||
if (listeners.includes(fn)) { | ||
console.warn(`Duplicate listeners detected: ${fn.name}`); | ||
} | ||
listeners.push(fn); | ||
const tooManyListeners = listeners.size > this.maxListeners; | ||
if (tooManyListeners) { | ||
const name = 'MaxListenersExceededWarning'; | ||
const warn = 'Possible EventEmitter memory leak detected'; | ||
const max = `Current maxListenersCount is ${this.maxListeners}`; | ||
const hint = 'Hint: avoid adding listeners in loops'; | ||
console.warn(`${name}: ${warn}. ${max}. ${hint}`); | ||
} | ||
} | ||
|
||
once(name, fn) { | ||
const dispose = (...args) => { | ||
this.remove(name, dispose); | ||
this.off(name, dispose); | ||
return void fn(...args); | ||
}; | ||
this.on(name, dispose); | ||
} | ||
|
||
emit(name, ...args) { | ||
const event = this.events.get(name); | ||
if (!event) return; | ||
for (const fn of event.values()) { | ||
fn(...args); | ||
off(name, fn) { | ||
const listeners = this.events.get(name); | ||
if (!listeners) return; | ||
const index = listeners.indexOf(fn); | ||
listeners.splice(index, 1); | ||
if (listeners.length === 0) { | ||
this.events.delete(name); | ||
} | ||
} | ||
|
||
remove(name, fn) { | ||
const event = this.events.get(name); | ||
if (!event) return; | ||
event.delete(fn); | ||
toPromise(name) { | ||
return new Promise((resolve) => { | ||
this.once(name, resolve); | ||
}); | ||
} | ||
|
||
toIterator(name) { | ||
const stub = () => {}; | ||
this.events.on(name, stub); | ||
return { | ||
async next() {}, | ||
}; | ||
} | ||
|
||
clear(name) { | ||
if (!name) return void this.events.clear(); | ||
this.events.delete(name); | ||
} | ||
} | ||
|
||
const once = (emitter, name) => | ||
new Promise((resolve) => { | ||
emitter.once(name, resolve); | ||
}); | ||
listeners(name) { | ||
return this.events.get(name) || []; | ||
} | ||
|
||
listenerCount(name) { | ||
const listeners = this.events.get(name); | ||
if (listeners) return listeners.length; | ||
return 0; | ||
} | ||
|
||
eventNames() { | ||
return Array.from(this.events.keys()); | ||
} | ||
} | ||
|
||
module.exports = { EventEmitter, once }; | ||
module.exports = { EventEmitter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters