Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

takeUntil.ts 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  5. import { innerSubscribe, SimpleInnerSubscriber, SimpleOuterSubscriber } from '../innerSubscribe';
  6. /**
  7. * Emits the values emitted by the source Observable until a `notifier`
  8. * Observable emits a value.
  9. *
  10. * <span class="informal">Lets values pass until a second Observable,
  11. * `notifier`, emits a value. Then, it completes.</span>
  12. *
  13. * ![](takeUntil.png)
  14. *
  15. * `takeUntil` subscribes and begins mirroring the source Observable. It also
  16. * monitors a second Observable, `notifier` that you provide. If the `notifier`
  17. * emits a value, the output Observable stops mirroring the source Observable
  18. * and completes. If the `notifier` doesn't emit any value and completes
  19. * then `takeUntil` will pass all values.
  20. *
  21. * ## Example
  22. * Tick every second until the first click happens
  23. * ```ts
  24. * import { fromEvent, interval } from 'rxjs';
  25. * import { takeUntil } from 'rxjs/operators';
  26. *
  27. * const source = interval(1000);
  28. * const clicks = fromEvent(document, 'click');
  29. * const result = source.pipe(takeUntil(clicks));
  30. * result.subscribe(x => console.log(x));
  31. * ```
  32. *
  33. * @see {@link take}
  34. * @see {@link takeLast}
  35. * @see {@link takeWhile}
  36. * @see {@link skip}
  37. *
  38. * @param {Observable} notifier The Observable whose first emitted value will
  39. * cause the output Observable of `takeUntil` to stop emitting values from the
  40. * source Observable.
  41. * @return {Observable<T>} An Observable that emits the values from the source
  42. * Observable until such time as `notifier` emits its first value.
  43. * @method takeUntil
  44. * @owner Observable
  45. */
  46. export function takeUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
  47. return (source: Observable<T>) => source.lift(new TakeUntilOperator(notifier));
  48. }
  49. class TakeUntilOperator<T> implements Operator<T, T> {
  50. constructor(private notifier: Observable<any>) {
  51. }
  52. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  53. const takeUntilSubscriber = new TakeUntilSubscriber(subscriber);
  54. const notifierSubscription = innerSubscribe(this.notifier, new SimpleInnerSubscriber(takeUntilSubscriber));
  55. if (notifierSubscription && !takeUntilSubscriber.seenValue) {
  56. takeUntilSubscriber.add(notifierSubscription);
  57. return source.subscribe(takeUntilSubscriber);
  58. }
  59. return takeUntilSubscriber;
  60. }
  61. }
  62. /**
  63. * We need this JSDoc comment for affecting ESDoc.
  64. * @ignore
  65. * @extends {Ignored}
  66. */
  67. class TakeUntilSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
  68. seenValue = false;
  69. constructor(destination: Subscriber<any>, ) {
  70. super(destination);
  71. }
  72. notifyNext(): void {
  73. this.seenValue = true;
  74. this.complete();
  75. }
  76. notifyComplete(): void {
  77. // noop
  78. }
  79. }