Get rid of typed emitter

This commit is contained in:
Jon Staab
2025-04-02 08:54:50 -07:00
parent 1cbcb0ae4a
commit 35f75bb38e
16 changed files with 124 additions and 173 deletions
+6 -7
View File
@@ -1118,21 +1118,20 @@ export const pushToMapKey = <K, T>(m: Map<K, T[]>, k: K, v: T) => {
}
/**
* A generic type-safe event listener function that auto-detects the appropriate methods
* for adding and removing event listeners.
* A generic type-safe event listener function that works with event emitters.
*
* @param target - The event target object with add/remove listener methods
* @param eventName - The name of the event to listen for
* @param callback - The callback function to execute when the event occurs
* @returns A function that removes the event listener when called
*/
export const on = <EventName extends string, Args extends any[]>(
export const on = <EventMap extends Record<string | symbol, any[]>, E extends keyof EventMap>(
target: {
on: (event: EventName, handler: (...args: Args) => any, ...rest: any[]) => any
off: (event: EventName, handler: (...args: Args) => any, ...rest: any[]) => any
on(event: E, listener: (...args: EventMap[E]) => any): any
off(event: E, listener: (...args: EventMap[E]) => any): any
},
eventName: EventName,
callback: (...args: Args) => void,
eventName: E,
callback: (...args: EventMap[E]) => void,
): (() => void) => {
target.on(eventName, callback)