index.d.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. declare namespace debounceFn {
  2. interface Options {
  3. /**
  4. Time to wait until the `input` function is called.
  5. @default 0
  6. */
  7. readonly wait?: number;
  8. /**
  9. Trigger the function on the leading edge of the `wait` interval.
  10. For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
  11. @default false
  12. */
  13. readonly before?: boolean;
  14. /**
  15. Trigger the function on the trailing edge of the `wait` interval.
  16. @default true
  17. */
  18. readonly after?: boolean;
  19. }
  20. interface BeforeOptions extends Options {
  21. readonly before: true;
  22. }
  23. interface NoBeforeNoAfterOptions extends Options {
  24. readonly after: false;
  25. readonly before?: false;
  26. }
  27. interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> {
  28. (...arguments: ArgumentsType): ReturnType;
  29. cancel(): void;
  30. }
  31. }
  32. /**
  33. [Debounce](https://davidwalsh.name/javascript-debounce-function) a function.
  34. @param input - Function to debounce.
  35. @returns A debounced function that delays calling the `input` function until after `wait` milliseconds have elapsed since the last time the debounced function was called.
  36. It comes with a `.cancel()` method to cancel any scheduled `input` function calls.
  37. @example
  38. ```
  39. import debounceFn = require('debounce-fn');
  40. window.onresize = debounceFn(() => {
  41. // Do something on window resize
  42. }, {wait: 100});
  43. ```
  44. */
  45. declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
  46. input: (...arguments: ArgumentsType) => ReturnType,
  47. options: debounceFn.BeforeOptions
  48. ): debounceFn.DebouncedFunction<ArgumentsType, ReturnType>;
  49. declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
  50. input: (...arguments: ArgumentsType) => ReturnType,
  51. options: debounceFn.NoBeforeNoAfterOptions
  52. ): debounceFn.DebouncedFunction<ArgumentsType, undefined>;
  53. declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
  54. input: (...arguments: ArgumentsType) => ReturnType,
  55. options?: debounceFn.Options
  56. ): debounceFn.DebouncedFunction<ArgumentsType, ReturnType | undefined>;
  57. export = debounceFn;