sample.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** PURE_IMPORTS_START .._OuterSubscriber,.._util_subscribeToResult 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 { OuterSubscriber } from '../OuterSubscriber';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. /**
  12. * Emits the most recently emitted value from the source Observable whenever
  13. * another Observable, the `notifier`, emits.
  14. *
  15. * <span class="informal">It's like {@link sampleTime}, but samples whenever
  16. * the `notifier` Observable emits something.</span>
  17. *
  18. * <img src="./img/sample.png" width="100%">
  19. *
  20. * Whenever the `notifier` Observable emits a value or completes, `sample`
  21. * looks at the source Observable and emits whichever value it has most recently
  22. * emitted since the previous sampling, unless the source has not emitted
  23. * anything since the previous sampling. The `notifier` is subscribed to as soon
  24. * as the output Observable is subscribed.
  25. *
  26. * @example <caption>On every click, sample the most recent "seconds" timer</caption>
  27. * var seconds = Rx.Observable.interval(1000);
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var result = seconds.sample(clicks);
  30. * result.subscribe(x => console.log(x));
  31. *
  32. * @see {@link audit}
  33. * @see {@link debounce}
  34. * @see {@link sampleTime}
  35. * @see {@link throttle}
  36. *
  37. * @param {Observable<any>} notifier The Observable to use for sampling the
  38. * source Observable.
  39. * @return {Observable<T>} An Observable that emits the results of sampling the
  40. * values emitted by the source Observable whenever the notifier Observable
  41. * emits value or completes.
  42. * @method sample
  43. * @owner Observable
  44. */
  45. export function sample(notifier) {
  46. return function (source) { return source.lift(new SampleOperator(notifier)); };
  47. }
  48. var SampleOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  49. function SampleOperator(notifier) {
  50. this.notifier = notifier;
  51. }
  52. SampleOperator.prototype.call = function (subscriber, source) {
  53. var sampleSubscriber = new SampleSubscriber(subscriber);
  54. var subscription = source.subscribe(sampleSubscriber);
  55. subscription.add(subscribeToResult(sampleSubscriber, this.notifier));
  56. return subscription;
  57. };
  58. return SampleOperator;
  59. }());
  60. /**
  61. * We need this JSDoc comment for affecting ESDoc.
  62. * @ignore
  63. * @extends {Ignored}
  64. */
  65. var SampleSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  66. __extends(SampleSubscriber, _super);
  67. function SampleSubscriber() {
  68. _super.apply(this, arguments);
  69. this.hasValue = false;
  70. }
  71. SampleSubscriber.prototype._next = function (value) {
  72. this.value = value;
  73. this.hasValue = true;
  74. };
  75. SampleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  76. this.emitValue();
  77. };
  78. SampleSubscriber.prototype.notifyComplete = function () {
  79. this.emitValue();
  80. };
  81. SampleSubscriber.prototype.emitValue = function () {
  82. if (this.hasValue) {
  83. this.hasValue = false;
  84. this.destination.next(this.value);
  85. }
  86. };
  87. return SampleSubscriber;
  88. }(OuterSubscriber));
  89. //# sourceMappingURL=sample.js.map