import { AnyFunction } from "../types/global.js"; //#region src/modules/function.d.ts /** * 防抖函数接口,包含取消、刷新和待定状态查询方法 * @public */ interface DebouncedFunction { /** 调用防抖后的函数 */ (this: unknown, ...args: unknown[]): unknown | undefined; /** 取消延迟的函数调用 */ cancel: () => void; /** 查询是否有待执行的定时器 */ pending: () => boolean; /** 立即执行待处理的函数调用 */ flush: () => unknown; } /** * 节流函数接口,包含取消和刷新方法 * @public */ interface ThrottledFunction { /** 调用节流后的函数 */ (this: unknown, ...args: unknown[]): void; /** 取消延迟的函数调用 */ cancel: () => void; /** 立即执行待处理的函数调用 */ flush: () => unknown; } /** * 创建一个防抖函数,该函数会从上一次被调用后,延迟 wait 毫秒后调用 func 方法。debounced函数提供一个cancel方法, * 以及flush方法立即调用。 * options选项中,可以设置options.leading 与|或 options.trailing 决定延迟前后如何触发 * 如果leading和trailing都为false,则函数不会立刻触发,也不会延迟后触发,而是需要我们手动触发 * @param func - 要防抖的函数 * @param wait - 延迟时间(毫秒) * @param options - 配置选项 * @returns - 防抖函数 * @public */ declare function debounce(func: AnyFunction, wait?: number, options?: { leading?: boolean; trailing?: boolean; }): DebouncedFunction; /** * 创建一个节流函数,每 wait 毫秒最多仅调用一次 func 。 * 附带一个cancel方法取消延迟的func调用,以及一个flash立刻调用。 * 提供 options 以指示是否应在 wait 超时的前缘和/或后沿调用 func 。 func 使用提供给节流函数的最后一个参数进行调用。 * 对节流函数的后续调用将返回上次 func 调用的结果。 * @param func - 要节流的函数 * @param wait - 间隔时间(毫秒) * @param options - 配置选项 * @returns - 节流函数 * @public */ declare function throttle(func: AnyFunction, wait?: number, options?: { leading: boolean; trailing: boolean; }): ThrottledFunction; /** * 轮询状态信息接口 * @public */ interface PollingStatus { /** 轮询配置选项 */ options: PollingOptions; /** 当前轮询状态 */ status: 'running' | 'stopped'; /** 重试次数 */ retryCount: number; /** 已执行次数 */ executeCount: number; /** 最近一次执行结果 */ lastResult: T | undefined; /** 最近一次执行错误 */ lastError: unknown; } /** * 轮询控制器接口 * @public */ interface PollingController { /** 启动轮询 */ start: () => void; /** 停止轮询 */ stop: () => void; /** 获取轮询状态 */ status: () => PollingStatus; } interface PollingOptions { task: () => Promise; stopCondition?: (result: T) => boolean; errorAction?: (error: unknown) => void; onProgress?: (result: T) => void; quitOnError?: boolean; interval?: number; maxRetries?: number; immediate?: boolean; maxExecutions?: number; } /** * 创建轮询控制器 * @template T 轮询任务返回值的类型 * @param options - 轮询配置选项 * @returns 轮询控制器对象,包含以下方法: * - `start()` - 启动轮询 * - `stop()` - 停止轮询 * - `status()` - 获取轮询状态 * * @example * const poller = createPolling(\{ * task: fetchData, * stopCondition: (data) =\> data.status === 'done', * interval: 2000 * \}); * poller.start(); * @public */ declare function createPolling(options: PollingOptions): PollingController; /** * 重试配置选项接口 * @public */ interface RetryOptions { maxRetries?: number; delay?: number; shouldRetry?: (error: unknown) => boolean; } /** * 创建一个支持重试的函数包装器 * @param fn - 需要重试的函数(支持同步和异步) * @param options - 重试配置 * @returns - 包装后的函数 * * @example * // 基本用法 * const fetchWithRetry = withRetry(fetchData, \{ maxRetries: 3 \}); * * // 带延迟重试 * const fetchWithRetry = withRetry(fetchData, \{ maxRetries: 3, delay: 1000 \}); * * // 自定义重试条件 * const fetchWithRetry = withRetry(fetchData, \{ * @public * maxRetries: 3, * shouldRetry: (error) =\> error.statusCode !== 404 * \}); */ declare function withRetry(fn: T, options?: RetryOptions): (...args: Parameters) => Promise>; //#endregion export { DebouncedFunction, PollingController, PollingOptions, PollingStatus, RetryOptions, ThrottledFunction, createPolling, debounce, throttle, withRetry };