提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

  1. //#region src/index.d.ts
  2. interface DebounceOptions {
  3. /**
  4. Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1).
  5. Meaning immediately, instead of waiting for `wait` milliseconds.
  6. @default false
  7. */
  8. readonly leading?: boolean;
  9. /**
  10. Call the `fn` on trailing edge with last used arguments. Result of call is from previous call.
  11. @default true
  12. */
  13. readonly trailing?: boolean;
  14. }
  15. type DebouncedReturn<ArgumentsT extends unknown[], ReturnT> = ((...args: ArgumentsT) => Promise<ReturnT>) & {
  16. /**
  17. * Cancel pending function call
  18. */
  19. cancel: () => void;
  20. /**
  21. * Immediately invoke pending function call
  22. */
  23. flush: () => Promise<ReturnT> | undefined;
  24. /**
  25. * Get pending function call
  26. */
  27. isPending: () => boolean;
  28. };
  29. /**
  30. Debounce functions
  31. @param fn - Promise-returning/async function to debounce.
  32. @param wait - Milliseconds to wait before calling `fn`. Default value is 25ms
  33. @returns A function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
  34. @example
  35. ```
  36. import { debounce } from 'perfect-debounce';
  37. const expensiveCall = async input => input;
  38. const debouncedFn = debounce(expensiveCall, 200);
  39. for (const number of [1, 2, 3]) {
  40. console.log(await debouncedFn(number));
  41. }
  42. //=> 1
  43. //=> 2
  44. //=> 3
  45. ```
  46. */
  47. declare function debounce<ArgumentsT extends unknown[], ReturnT>(fn: (...args: ArgumentsT) => PromiseLike<ReturnT> | ReturnT, wait?: number, options?: DebounceOptions): DebouncedReturn<ArgumentsT, ReturnT>;
  48. //#endregion
  49. export { DebounceOptions, debounce };