mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-28 20:40:39 +00:00
Type Emitter
This commit is contained in:
parent
76187eb0cd
commit
b4a48d57b8
1 changed files with 8 additions and 6 deletions
|
|
@ -1,5 +1,11 @@
|
|||
export default class Emitter {
|
||||
on(event, callback) {
|
||||
_listeners: { [event: string]: Array<(...args: any[]) => void> };
|
||||
|
||||
constructor() {
|
||||
this._listeners = {};
|
||||
}
|
||||
|
||||
on(event: string, callback: (...args: any[]) => void) {
|
||||
const eventListeners = this._listeners[event] || [];
|
||||
eventListeners.push(callback);
|
||||
this._listeners[event] = eventListeners;
|
||||
|
|
@ -9,14 +15,10 @@ export default class Emitter {
|
|||
return unsubscribe;
|
||||
}
|
||||
|
||||
trigger(event, ...args) {
|
||||
trigger(event: string, ...args: any[]) {
|
||||
const callbacks = this._listeners[event];
|
||||
if (callbacks) {
|
||||
callbacks.forEach(cb => cb(...args));
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this._listeners = {};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue