useDebounce.ts 840 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. export interface DebounceAndThrottleOptions {
  2. // 立即执行
  3. immediate?: boolean;
  4. // 是否为debounce
  5. debounce?: boolean;
  6. // 只执行一次
  7. once?: boolean;
  8. }
  9. export type CancelFn = () => void;
  10. export type DebounceAndThrottleProcedure<T extends unknown[]> = (...args: T) => unknown;
  11. export type DebounceAndThrottleProcedureResult<T extends unknown[]> = [
  12. DebounceAndThrottleProcedure<T>,
  13. CancelFn
  14. ];
  15. import {
  16. // throttle,
  17. useThrottle,
  18. } from './useThrottle';
  19. /**
  20. * @description: Applicable in components
  21. */
  22. export function useDebounce<T extends unknown[]>(
  23. handle: DebounceAndThrottleProcedure<T>,
  24. wait: number,
  25. options: DebounceAndThrottleOptions = {}
  26. ): DebounceAndThrottleProcedureResult<T> {
  27. return useThrottle(
  28. handle,
  29. wait,
  30. Object.assign(options, {
  31. debounce: true,
  32. })
  33. );
  34. }