-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added failsafe in case producer is busy
- Loading branch information
Showing
2 changed files
with
63 additions
and
7 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,26 +1,68 @@ | ||
import { EventsRecord } from "./EventEmittingComponent"; | ||
|
||
type ListenersHolder<Events extends EventsRecord> = { | ||
// eslint-disable-next-line putout/putout | ||
[key in keyof Events]?: ((...args: Events[key]) => void)[]; | ||
[key in keyof Events]?: { | ||
id: number; | ||
listener: (...args: Events[key]) => void; | ||
}[]; | ||
}; | ||
|
||
export class EventEmitter<Events extends EventsRecord> { | ||
private readonly listeners: ListenersHolder<Events> = {}; | ||
|
||
private counter = 0; | ||
|
||
// Fields used for offSelf() | ||
private currentListenerId: number | undefined = undefined; | ||
|
||
private currentListenerEventName: keyof Events | undefined = undefined; | ||
|
||
public emit(event: keyof Events, ...parameters: Events[typeof event]) { | ||
const listeners = this.listeners[event]; | ||
if(listeners !== undefined) { | ||
if (listeners !== undefined) { | ||
this.currentListenerEventName = event; | ||
|
||
listeners.forEach((listener) => { | ||
listener(...parameters); | ||
this.currentListenerId = listener.id; | ||
|
||
listener.listener(...parameters); | ||
|
||
this.currentListenerId = undefined; | ||
}); | ||
this.currentListenerEventName = undefined; | ||
} | ||
} | ||
|
||
public on<Key extends keyof Events>( | ||
event: Key, | ||
listener: (...args: Events[Key]) => void | ||
) { | ||
(this.listeners[event] ??= []).push(listener); | ||
): number { | ||
// eslint-disable-next-line no-multi-assign | ||
const id = (this.counter += 1); | ||
(this.listeners[event] ??= []).push({ | ||
id, | ||
listener, | ||
}); | ||
return id; | ||
} | ||
|
||
// eslint-disable-next-line no-warning-comments | ||
// TODO Improve to be thread-safe | ||
public offSelf() { | ||
if ( | ||
this.currentListenerEventName !== undefined && | ||
this.currentListenerId !== undefined | ||
) { | ||
this.off(this.currentListenerEventName, this.currentListenerId); | ||
} | ||
} | ||
|
||
public off<Key extends keyof Events>(event: Key, id: number) { | ||
const listeners = this.listeners[event]; | ||
if (listeners !== undefined) { | ||
this.listeners[event] = listeners.filter( | ||
(listener) => listener.id !== id | ||
); | ||
} | ||
} | ||
} |
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