4dd6ae4f27d8d4e7667c6d24ecf57eb5f4f6f9ed8a9660cd15b0a9b78987f12cea9651ddd36e5033aa3d8b9a583c8fbd4cee87c616f350c4e1b50ecbbc0bf7 2.8 KB

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