UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

86 lines (85 loc) 3.73 kB
import { TDebounceOptions, TScheduleOptions } from "../../utils/utils/schedule"; /** * Hook to create a debounced function that delays execution until both frame and time conditions are met. * * The function will only execute when BOTH conditions are satisfied: * - At least `frameInterval` frames have passed since the last invocation * - At least `frameTimeout` milliseconds have passed since the last invocation * * @template T - The function type to debounce * @param fn - The function to debounce * @param options - Configuration options * @param options.priority - Scheduler priority (default: MEDIUM) * @param options.frameInterval - Number of frames to wait before execution (default: 1) * @param options.frameTimeout - Minimum time in milliseconds to wait before execution (default: 0) * @returns A debounced version of the function with a `cancel()` method to abort pending executions * @see TDebounceOptions * @example * ```tsx * const debouncedSearch = useSchedulerDebounce( * (query: string) => fetchResults(query), * { frameInterval: 2, frameTimeout: 300 } * ); * * // Later: cancel pending execution * debouncedSearch.cancel(); * ``` */ export declare function useSchedulerDebounce<T extends (...args: Parameters<T>) => void>(fn: T, options: TDebounceOptions): ((...args: Parameters<T>) => void) & { cancel: () => void; flush: () => void; isScheduled: () => boolean; }; /** * Hook to create a throttled function that limits execution frequency. * * The function will execute at most once when BOTH conditions are satisfied: * - At least `frameInterval` frames have passed since the last execution * - At least `frameTimeout` milliseconds have passed since the last execution * * Unlike debounce, throttle executes immediately on the first call and then enforces the delay. * * @template T - The function type to throttle * @param fn - The function to throttle * @param options - Configuration options * @param options.priority - Scheduler priority (default: MEDIUM) * @param options.frameInterval - Number of frames to wait between executions (default: 1) * @param options.frameTimeout - Minimum time in milliseconds to wait between executions (default: 0) * @returns A throttled version of the function with a `cancel()` method to abort pending executions * @see TDebounceOptions * @example * ```tsx * const throttledResize = useSchedulerThrottle( * (width: number, height: number) => handleResize(width, height), * { frameInterval: 1, frameTimeout: 100 } * ); * ``` */ export declare function useSchedulerThrottle<T extends (...args: Parameters<T>) => void>(fn: T, options: TDebounceOptions): ((...args: Parameters<T>) => void) & { cancel: () => void; flush: () => void; }; type TSchedulerTaskFn = (...args: unknown[]) => void; /** * Hook to schedule a task for execution after a certain number of frames have passed. * * The scheduled task will execute once the specified frame interval has elapsed. * The task is automatically cancelled when the component unmounts. * * @template T - The function type to schedule * @param fn - The function to schedule for execution * @param options - Configuration options * @param options.priority - Scheduler priority (default: MEDIUM) * @param options.frameInterval - Number of frames to wait before execution (default: 1) * @returns void - The task cleanup is handled automatically on unmount * @see TScheduleOptions * @example * ```tsx * useScheduledTask( * () => updateLayout(), * { frameInterval: 2, priority: 'HIGH' } * ); * ``` */ export declare function useScheduledTask<T extends TSchedulerTaskFn>(fn: T, options: Omit<TScheduleOptions, "once">): void; export {};