throttle.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { SubscribableOrPromise } from '../Observable';
  2. import { MonoTypeOperatorFunction } from '../interfaces';
  3. export interface ThrottleConfig {
  4. leading?: boolean;
  5. trailing?: boolean;
  6. }
  7. export declare const defaultThrottleConfig: ThrottleConfig;
  8. /**
  9. * Emits a value from the source Observable, then ignores subsequent source
  10. * values for a duration determined by another Observable, then repeats this
  11. * process.
  12. *
  13. * <span class="informal">It's like {@link throttleTime}, but the silencing
  14. * duration is determined by a second Observable.</span>
  15. *
  16. * <img src="./img/throttle.png" width="100%">
  17. *
  18. * `throttle` emits the source Observable values on the output Observable
  19. * when its internal timer is disabled, and ignores source values when the timer
  20. * is enabled. Initially, the timer is disabled. As soon as the first source
  21. * value arrives, it is forwarded to the output Observable, and then the timer
  22. * is enabled by calling the `durationSelector` function with the source value,
  23. * which returns the "duration" Observable. When the duration Observable emits a
  24. * value or completes, the timer is disabled, and this process repeats for the
  25. * next source value.
  26. *
  27. * @example <caption>Emit clicks at a rate of at most one click per second</caption>
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var result = clicks.throttle(ev => Rx.Observable.interval(1000));
  30. * result.subscribe(x => console.log(x));
  31. *
  32. * @see {@link audit}
  33. * @see {@link debounce}
  34. * @see {@link delayWhen}
  35. * @see {@link sample}
  36. * @see {@link throttleTime}
  37. *
  38. * @param {function(value: T): SubscribableOrPromise} durationSelector A function
  39. * that receives a value from the source Observable, for computing the silencing
  40. * duration for each source value, returned as an Observable or a Promise.
  41. * @param {Object} config a configuration object to define `leading` and `trailing` behavior. Defaults
  42. * to `{ leading: true, trailing: false }`.
  43. * @return {Observable<T>} An Observable that performs the throttle operation to
  44. * limit the rate of emissions from the source.
  45. * @method throttle
  46. * @owner Observable
  47. */
  48. export declare function throttle<T>(durationSelector: (value: T) => SubscribableOrPromise<number>, config?: ThrottleConfig): MonoTypeOperatorFunction<T>;