throttle.js 2.3 KB

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