-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Denys Otrishko <[email protected]> Co-Authored-By: Mykola Bilochub <[email protected]> Co-Authored-By: Dmytro Nechai <[email protected]>
- Loading branch information
1 parent
85509e9
commit 885ef81
Showing
5 changed files
with
443 additions
and
0 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
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
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) { | ||
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); | ||
}); | ||
} | ||
this.event(name).once.add(fn); | ||
return null; | ||
} | ||
|
||
// Emit event | ||
// name <string> event name | ||
// args <any[]> | ||
// Returns: <Promise> | <null> | ||
emit(name, ...args) { | ||
const { events } = this; | ||
const event = events.get(name); | ||
if (!event) return null; | ||
const { on, once } = event; | ||
const promises = [...on.values(), ...once.values()].map(fn => fn(...args)); | ||
once.clear(); | ||
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; | ||
} | ||
|
||
// 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.values(), ...once.values()]; | ||
} | ||
|
||
// Get event names array | ||
// Returns: <string[]> names | ||
names() { | ||
return [...this.events.keys()]; | ||
} | ||
} | ||
|
||
module.exports = { AsyncEmitter }; |
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
Oops, something went wrong.