c5f31347e06f52f9441e9fa1b2f9d7b5c4cce00abd185ee3f1dbf67a64ce382bd33a89f67b1d93cc776448cfe8a4f9f9d8fc1822f23b1c1ca9b62d73a9d947 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. declare type ThrottleFunction = (this: unknown, ...args: unknown[]) => void;
  2. export declare type ThrottleType = 'fixRate' | 'debounce';
  3. export interface ThrottleController {
  4. clear(): void;
  5. debounceNextCall(debounceDelay: number): void;
  6. }
  7. /**
  8. * @public
  9. * @param {(Function)} fn
  10. * @param {number} [delay=0] Unit: ms.
  11. * @param {boolean} [debounce=false]
  12. * true: If call interval less than `delay`, only the last call works.
  13. * false: If call interval less than `delay, call works on fixed rate.
  14. * @return {(Function)} throttled fn.
  15. */
  16. export declare function throttle<T extends ThrottleFunction>(fn: T, delay?: number, debounce?: boolean): T & ThrottleController;
  17. /**
  18. * Create throttle method or update throttle rate.
  19. *
  20. * @example
  21. * ComponentView.prototype.render = function () {
  22. * ...
  23. * throttle.createOrUpdate(
  24. * this,
  25. * '_dispatchAction',
  26. * this.model.get('throttle'),
  27. * 'fixRate'
  28. * );
  29. * };
  30. * ComponentView.prototype.remove = function () {
  31. * throttle.clear(this, '_dispatchAction');
  32. * };
  33. * ComponentView.prototype.dispose = function () {
  34. * throttle.clear(this, '_dispatchAction');
  35. * };
  36. *
  37. */
  38. export declare function createOrUpdate<T, S extends keyof T, P = T[S]>(obj: T, fnAttr: S, rate: number, throttleType: ThrottleType): P extends ThrottleFunction ? P & ThrottleController : never;
  39. /**
  40. * Clear throttle. Example see throttle.createOrUpdate.
  41. */
  42. export declare function clear<T, S extends keyof T>(obj: T, fnAttr: S): void;
  43. export {};