UNPKG

@alessiofrittoli/web-utils

Version:
66 lines (64 loc) 1.98 kB
/** * zZzZzZzZzZzZzZzZz. * * @param time The sleep time in milliseconds. * @returns A new Promise which get resolved after the specified time. */ declare const sleep: (time: number) => Promise<void>; /** * The deffered task function definition. * * @template T The task function arguments. */ type DeferredTask<T extends unknown[]> = (...args: T) => unknown | Promise<unknown>; /** * Defer task so main-thread is not blocked in order to quickly paint and respond to user interaction. * * It esponentially decrease INP process timings. * * @template T The task function definition. * @template U The task function arguments. * * @param task The task callable function. * @param args Arguments required by the given `task` function. * * @returns A new Promise which returns the `task` result once fulfilled. * * @example * * ```ts * const longTask = ( event: Event ) => { * ... * } * * button.addEventListener( 'click', event => { * deferTask( longTask, event ) * } ) * ``` */ declare const deferTask: <T extends DeferredTask<U>, U extends unknown[]>(task: T, ...args: U) => Promise<Awaited<ReturnType<T>>>; /** * Defer task handler so main-thread is not blocked in order to quickly paint and respond to user interaction. * * It esponentially decrease INP process timings. * * @template T The task function definition. * @template U The task function arguments. * * @param task The task callable function. * @param args Arguments required by the given `task` function. * * @returns A new handler which returns a new Promise that returns the `task` result once fulfilled. * * @example * * ```ts * const longTask = ( event: Event ) => { * ... * } * * button.addEventListener( 'click', deferCallback( longTask ) ) * ``` */ declare const deferCallback: <T extends DeferredTask<U>, U extends unknown[]>(task: T) => (...args: U) => Promise<Awaited<ReturnType<T>>>; export { type DeferredTask, deferCallback, deferTask, sleep };