sample.js 3.2 KB

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