Add general off handling

This commit is contained in:
2026-08-07 02:11:40 +00:00
parent a948671251
commit 8b47dd2d8b
2 changed files with 23 additions and 1 deletions
+7 -1
View File
@@ -117,11 +117,17 @@ export class EventEmitter<T extends EventMap> {
* @param type - The event type.
* @param listener - The listener function.
*/
off<K extends keyof T>(type: K, listener: Listener<T[K]>): void {
off<K extends keyof T>(type: K, listener?: Listener<T[K]>): void {
// Get the listeners for the event type.
const listeners = this.#listeners.get(type);
if (!listeners) return;
// If no listener is provided, remove all listeners for the event type.
if (!listener) {
this.#listeners.delete(type);
return;
}
// Find the listener entry.
const listenerEntry = Array.from(listeners).find((entry) => entry.listener === listener || entry.wrappedListener === listener);