revenge-bundle-next/lib/utils/src/callback.ts

32 lines
795 B
TypeScript
Raw Normal View History

2025-06-03 16:24:58 +07:00
export function debounce<F extends (...args: any[]) => any>(
func: F,
timeout: number,
) {
2025-04-15 14:22:41 +07:00
let timer: Parameters<typeof clearTimeout>[0]
return (...args: Parameters<F>) =>
new Promise(rs => {
clearTimeout(timer)
timer = setTimeout(() => {
rs(func(...args))
}, timeout)
2025-04-15 14:22:41 +07:00
})
}
const now = (cb: () => any) => {
cb()
}
/**
* A function that runs the callback as soon as possible.
* @param cb The callback to run.
*/
export const asap = (cb: (...args: any[]) => any) => {
// @ts-expect-error
;(HermesInternal.useEngineQueue()
? // @ts-expect-error
HermesInternal.enqueueJob
: (globalThis.setImmediate ?? globalThis.setTimeout ?? now))(cb, 0)
}
2025-06-14 17:54:04 +07:00
export const noop = () => {}