-
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
29a04ba
commit 7d637fa
Showing
5 changed files
with
452 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,114 @@ | ||
'use strict'; | ||
|
||
class AsyncEmitter { | ||
constructor() { | ||
this.events = new Map(); | ||
} | ||
|
||
// Get or create event | ||
// name <string> event name | ||
// Returns: { on <Function>, once <Function> } | ||
event(name) { | ||
let event = this.events.get(name); | ||
if (event) return event; | ||
const on = new Set(); | ||
const once = new Map(); | ||
event = { on, once }; | ||
this.events.set(name, event); | ||
return event; | ||
} | ||
|
||
// Add listener | ||
// name <string> event name | ||
// fn <Function> listener | ||
on(name, fn) { | ||
const event = this.event(name); | ||
event.on.add(fn); | ||
} | ||
|
||
// Add listener | ||
// name <string> event name | ||
// fn <Function> listener | ||
// Returns: <Promise> | <undefined> | ||
once(name, fn) { | ||
if (fn === undefined) { | ||
return new Promise(resolve => { | ||
this.once(name, resolve); | ||
}); | ||
} | ||
const wrapper = (...args) => { | ||
this.remove(name, fn, true); | ||
return fn(...args); | ||
}; | ||
const { once } = this.event(name); | ||
once.set(fn, wrapper); | ||
return undefined; | ||
} | ||
|
||
// Emit event | ||
// name <string> event name | ||
// args <any[]> | ||
// Returns: <Promise> | <undefined> | ||
async emit(name, ...args) { | ||
const event = this.events.get(name); | ||
if (!event) return undefined; | ||
const { on, once } = event; | ||
const listeners = [...on.values(), ...once.values()]; | ||
const promises = listeners.map(fn => fn(...args)); | ||
return Promise.all(promises); | ||
} | ||
|
||
// Remove event listener | ||
// name <string> event name | ||
// fn <Function> listener to remove | ||
// onceOnly <boolean> remove once only | ||
remove(name, fn, onceOnly = false) { | ||
const { events } = this; | ||
const event = events.get(name); | ||
if (!event) return; | ||
const { on, once } = event; | ||
if (!onceOnly) 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(); | ||
return; | ||
} | ||
const event = events.get(name); | ||
if (event) 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.keys()]; | ||
} | ||
|
||
// 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.