revenge-bundle-next/lib/utils/src/callback.ts
2026-07-31 02:38:45 +07:00

36 lines
915 B
TypeScript

import type { AnyFunction } from '@revenge-mod/utils/types'
export function debounce<F extends (...args: any[]) => any>(
func: F,
timeout: number,
) {
let timer: Parameters<typeof clearTimeout>[0]
return (...args: Parameters<F>) =>
new Promise(rs => {
clearTimeout(timer)
timer = setTimeout(() => {
rs(func(...args))
}, timeout)
})
}
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: AnyFunction) => {
// @ts-expect-error
;(HermesInternal.useEngineQueue()
? // @ts-expect-error
HermesInternal.enqueueJob
: (globalThis.queueMicrotask ??
globalThis.setImmediate ??
globalThis.setTimeout ??
now))(cb, 0)
}
export const noop = () => {}