UNPKG

@nasriya/atomix

Version:

Composable helper functions for building reliable systems

91 lines (90 loc) 4 kB
import { RandomOptions } from "./docs"; declare class CommonUtils { /** * Generate a random text * @param length The length of the text. Minimum of `4` * @param [options] Options for generating the text * @returns The generated text * @since v1.0.0 */ generateRandom(length: number, options?: RandomOptions): string; /** * Pauses execution for a specified number of milliseconds. * * This function returns a promise that resolves after the given duration, * allowing for asynchronous code to wait before proceeding. * * @param ms - The number of milliseconds to sleep. * @returns A promise that resolves after the specified delay. * @since v1.0.0 */ sleep(ms: number): Promise<void>; /** * Debounces a function, delaying its invocation until the specified delay * period has passed since the last call. * * The debounced function returns a promise that resolves with the result * of the original function. If the debounced function is called again * before the delay period has passed, the previous promise is rejected * with an error and a new promise is returned. * * The debounced function also has a `cancel` method that can be called to * cancel the current pending invocation. If called, the promise is rejected * with an error and the timer is cleared. * * @param fn - The function to debounce. * @param delay - The number of milliseconds to delay the invocation of the function. * @returns A debounced version of the given function. * @since v1.0.0 */ debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => Promise<ReturnType<T>>; /** * Returns a debounced version of the given function that does not return a promise. * * When the given function is called, it will wait the specified delay before * actually calling the function. If the function is called multiple times * within that delay, it will only call the function once after the delay * has passed. * * @param fn - The function to debounce. * @param delay - The number of milliseconds to delay calling the function. * @returns A debounced version of the given function. * @since v1.0.8 */ debounceSync<T extends (...args: any[]) => any>(fn: T, delay: number, options?: { onDone?: (result: ReturnType<T>) => void; onError?: (err: unknown) => void; }): (...args: Parameters<T>) => void; /** * Returns a throttled version of the given function. * * The throttled function will only invoke the original function at most * once in the specified delay period. If the function returns a promise, * it will maintain the promise chain and resolve with the promise's value. * If the throttled function is called again before the delay has passed, * it will return the pending promise or resolve immediately if there is none. * * @param fn - The function to throttle. * @param delay - The number of milliseconds to throttle invocations to. * @returns A throttled version of the given function that returns a promise. * @since v1.0.0 */ throttle<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => Promise<ReturnType<T>> | void; /** * A no-operation function useful as a placeholder callback. * @since v1.0.0 */ noop(): void; /** * Wraps a function such that it will only be called once. * * All subsequent calls will return the cached result of the first call. * * @param fn - The function to wrap. * @returns A wrapped version of the function that will only be called once. * @since v1.0.0 */ once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>; } declare const commonUtils: CommonUtils; export default commonUtils;