Front end of the Slack clone application.

sampleTime.js 3.6KB

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