Add adapter to net2
This commit is contained in:
@@ -935,6 +935,13 @@ export const once = (f: (...args: any) => void) => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a function
|
||||
* @param f - Function to call
|
||||
* @returns Whatever f returns
|
||||
*/
|
||||
export const call = <T>(f: () => T, ...args: unknown[]) => f()
|
||||
|
||||
/**
|
||||
* Memoizes function results based on arguments
|
||||
* @param f - Function to memoize
|
||||
@@ -1110,6 +1117,30 @@ export const pushToMapKey = <K, T>(m: Map<K, T[]>, k: K, v: T) => {
|
||||
m.set(k, a)
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic type-safe event listener function that auto-detects the appropriate methods
|
||||
* for adding and removing event listeners.
|
||||
*
|
||||
* @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[]>(
|
||||
target: {
|
||||
on: (event: EventName, handler: (...args: Args) => any, ...rest: any[]) => any
|
||||
off: (event: EventName, handler: (...args: Args) => any, ...rest: any[]) => any
|
||||
},
|
||||
eventName: EventName,
|
||||
callback: (...args: Args) => void,
|
||||
): (() => void) => {
|
||||
target.on(eventName, callback)
|
||||
|
||||
return () => {
|
||||
target.off(eventName, callback)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches on key in object, with default fallback
|
||||
* @param k - Key to look up
|
||||
|
||||
Reference in New Issue
Block a user