sample.js 2.6 KB

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