sampleTime.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Subscriber_1 = require('../Subscriber');
  8. var async_1 = require('../scheduler/async');
  9. /**
  10. * Emits the most recently emitted value from the source Observable within
  11. * periodic time intervals.
  12. *
  13. * <span class="informal">Samples the source Observable at periodic time
  14. * intervals, emitting what it samples.</span>
  15. *
  16. * <img src="./img/sampleTime.png" width="100%">
  17. *
  18. * `sampleTime` periodically looks at the source Observable and emits whichever
  19. * value it has most recently emitted since the previous sampling, unless the
  20. * source has not emitted anything since the previous sampling. The sampling
  21. * happens periodically in time every `period` milliseconds (or the time unit
  22. * defined by the optional `scheduler` argument). The sampling starts as soon as
  23. * the output Observable is subscribed.
  24. *
  25. * @example <caption>Every second, emit the most recent click at most once</caption>
  26. * var clicks = Rx.Observable.fromEvent(document, 'click');
  27. * var result = clicks.sampleTime(1000);
  28. * result.subscribe(x => console.log(x));
  29. *
  30. * @see {@link auditTime}
  31. * @see {@link debounceTime}
  32. * @see {@link delay}
  33. * @see {@link sample}
  34. * @see {@link throttleTime}
  35. *
  36. * @param {number} period The sampling period expressed in milliseconds or the
  37. * time unit determined internally by the optional `scheduler`.
  38. * @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for
  39. * managing the timers that handle the sampling.
  40. * @return {Observable<T>} An Observable that emits the results of sampling the
  41. * values emitted by the source Observable at the specified time interval.
  42. * @method sampleTime
  43. * @owner Observable
  44. */
  45. function sampleTime(period, scheduler) {
  46. if (scheduler === void 0) { scheduler = async_1.async; }
  47. return function (source) { return source.lift(new SampleTimeOperator(period, scheduler)); };
  48. }
  49. exports.sampleTime = sampleTime;
  50. var SampleTimeOperator = (function () {
  51. function SampleTimeOperator(period, scheduler) {
  52. this.period = period;
  53. this.scheduler = scheduler;
  54. }
  55. SampleTimeOperator.prototype.call = function (subscriber, source) {
  56. return source.subscribe(new SampleTimeSubscriber(subscriber, this.period, this.scheduler));
  57. };
  58. return SampleTimeOperator;
  59. }());
  60. /**
  61. * We need this JSDoc comment for affecting ESDoc.
  62. * @ignore
  63. * @extends {Ignored}
  64. */
  65. var SampleTimeSubscriber = (function (_super) {
  66. __extends(SampleTimeSubscriber, _super);
  67. function SampleTimeSubscriber(destination, period, scheduler) {
  68. _super.call(this, destination);
  69. this.period = period;
  70. this.scheduler = scheduler;
  71. this.hasValue = false;
  72. this.add(scheduler.schedule(dispatchNotification, period, { subscriber: this, period: period }));
  73. }
  74. SampleTimeSubscriber.prototype._next = function (value) {
  75. this.lastValue = value;
  76. this.hasValue = true;
  77. };
  78. SampleTimeSubscriber.prototype.notifyNext = function () {
  79. if (this.hasValue) {
  80. this.hasValue = false;
  81. this.destination.next(this.lastValue);
  82. }
  83. };
  84. return SampleTimeSubscriber;
  85. }(Subscriber_1.Subscriber));
  86. function dispatchNotification(state) {
  87. var subscriber = state.subscriber, period = state.period;
  88. subscriber.notifyNext();
  89. this.schedule(state, period);
  90. }
  91. //# sourceMappingURL=sampleTime.js.map