-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsyncEmitter #427
base: master
Are you sure you want to change the base?
AsyncEmitter #427
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||||
'use strict'; | ||||||||
|
||||||||
class AsyncEmitter { | ||||||||
constructor() { | ||||||||
this.events = new Map(); | ||||||||
} | ||||||||
|
||||||||
// Get or create event | ||||||||
// name <string> event name | ||||||||
// Returns: { on: <Set>, once: <Set> } } | ||||||||
event(name) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to use |
||||||||
const { events } = this; | ||||||||
const event = events.get(name); | ||||||||
if (event) return event; | ||||||||
const res = { on: new Set(), once: new Set() }; | ||||||||
events.set(name, res); | ||||||||
return res; | ||||||||
} | ||||||||
|
||||||||
// Add listener | ||||||||
// name <string> event name | ||||||||
// fn <Function> listener | ||||||||
on(name, fn) { | ||||||||
this.event(name).on.add(fn); | ||||||||
} | ||||||||
|
||||||||
// Add listener | ||||||||
// name <string> event name | ||||||||
// fn <Function> listener | ||||||||
// Returns: <Promise> | <null> | ||||||||
once(name, fn) { | ||||||||
if (fn === undefined) { | ||||||||
return new Promise(resolve => { | ||||||||
this.once(name, resolve); | ||||||||
tshemsedinov marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
}); | ||||||||
} | ||||||||
this.event(name).once.add(fn); | ||||||||
return Promise.resolve(); | ||||||||
} | ||||||||
|
||||||||
// Emit event | ||||||||
// name <string> event name | ||||||||
// args <any[]> | ||||||||
// Returns: <Promise> | ||||||||
emit(name, ...args) { | ||||||||
const { events } = this; | ||||||||
const event = events.get(name); | ||||||||
if (!event) return Promise.resolve(); | ||||||||
const { on, once } = event; | ||||||||
const promises = [...on, ...once].map(fn => fn(...args)); | ||||||||
once.clear(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to move this one line up, because some of the listeners may create additional |
||||||||
if (on.size === 0) events.delete(name); | ||||||||
return Promise.all(promises); | ||||||||
} | ||||||||
|
||||||||
// Remove event listener | ||||||||
// name <string> event name | ||||||||
// fn <Function> listener to remove | ||||||||
remove(name, fn) { | ||||||||
const { events } = this; | ||||||||
const event = events.get(name); | ||||||||
if (!event) return; | ||||||||
const { on, once } = event; | ||||||||
on.delete(fn); | ||||||||
once.delete(fn); | ||||||||
if (on.size === 0 && once.size === 0) { | ||||||||
events.delete(name); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// Remove all listeners or by name | ||||||||
// name <string> event name | ||||||||
clear(name) { | ||||||||
const { events } = this; | ||||||||
if (!name) events.clear(); | ||||||||
else events.delete(name); | ||||||||
} | ||||||||
|
||||||||
// Get listeners count by event name | ||||||||
// name <string> event name | ||||||||
// Returns: <number> | ||||||||
count(name) { | ||||||||
const event = this.events.get(name); | ||||||||
if (!event) return 0; | ||||||||
const { on, once } = event; | ||||||||
return on.size + once.size; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: can be simplified to: return event ? event.on.size + event.once.size : 0; |
||||||||
} | ||||||||
|
||||||||
// Get listeners array by event name | ||||||||
// name <string> event name | ||||||||
// Returns: <Function[]> | ||||||||
listeners(name) { | ||||||||
const event = this.events.get(name); | ||||||||
if (!event) return []; | ||||||||
const { on, once } = event; | ||||||||
return [...on, ...once]; | ||||||||
} | ||||||||
|
||||||||
// Get event names array | ||||||||
// Returns: <string[]> names | ||||||||
names() { | ||||||||
lundibundi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return [...this.events.keys()]; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
module.exports = { AsyncEmitter }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.