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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { MonoTypeOperatorFunction, TeardownLogic, ObservableInput } from '../types';
  5. import { Subscription } from '../Subscription';
  6. import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
  7. /**
  8. * Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.
  9. *
  10. * The `skipUntil` operator causes the observable stream to skip the emission of values ​​until the passed in observable emits the first value.
  11. * This can be particularly useful in combination with user interactions, responses of http requests or waiting for specific times to pass by.
  12. *
  13. * ![](skipUntil.png)
  14. *
  15. * Internally the `skipUntil` operator subscribes to the passed in observable (in the following called *notifier*) in order to recognize the emission
  16. * of its first value. When this happens, the operator unsubscribes from the *notifier* and starts emitting the values of the *source*
  17. * observable. It will never let the *source* observable emit any values if the *notifier* completes or throws an error without emitting
  18. * a value before.
  19. *
  20. * ## Example
  21. *
  22. * In the following example, all emitted values ​​of the interval observable are skipped until the user clicks anywhere within the page.
  23. *
  24. * ```ts
  25. * import { interval, fromEvent } from 'rxjs';
  26. * import { skipUntil } from 'rxjs/operators';
  27. *
  28. * const intervalObservable = interval(1000);
  29. * const click = fromEvent(document, 'click');
  30. *
  31. * const emitAfterClick = intervalObservable.pipe(
  32. * skipUntil(click)
  33. * );
  34. * // clicked at 4.6s. output: 5...6...7...8........ or
  35. * // clicked at 7.3s. output: 8...9...10..11.......
  36. * const subscribe = emitAfterClick.subscribe(value => console.log(value));
  37. * ```
  38. *
  39. * @param {Observable} notifier - The second Observable that has to emit an item before the source Observable's elements begin to
  40. * be mirrored by the resulting Observable.
  41. * @return {Observable<T>} An Observable that skips items from the source Observable until the second Observable emits
  42. * an item, then emits the remaining items.
  43. * @method skipUntil
  44. * @owner Observable
  45. */
  46. export function skipUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
  47. return (source: Observable<T>) => source.lift(new SkipUntilOperator(notifier));
  48. }
  49. class SkipUntilOperator<T> implements Operator<T, T> {
  50. constructor(private notifier: Observable<any>) {
  51. }
  52. call(destination: Subscriber<T>, source: any): TeardownLogic {
  53. return source.subscribe(new SkipUntilSubscriber(destination, this.notifier));
  54. }
  55. }
  56. /**
  57. * We need this JSDoc comment for affecting ESDoc.
  58. * @ignore
  59. * @extends {Ignored}
  60. */
  61. class SkipUntilSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
  62. private hasValue: boolean = false;
  63. private innerSubscription?: Subscription;
  64. constructor(destination: Subscriber<R>, notifier: ObservableInput<any>) {
  65. super(destination);
  66. const innerSubscriber = new SimpleInnerSubscriber(this);
  67. this.add(innerSubscriber);
  68. this.innerSubscription = innerSubscriber;
  69. const innerSubscription = innerSubscribe(notifier, innerSubscriber);
  70. // The returned subscription will usually be the subscriber that was
  71. // passed. However, interop subscribers will be wrapped and for
  72. // unsubscriptions to chain correctly, the wrapper needs to be added, too.
  73. if (innerSubscription !== innerSubscriber) {
  74. this.add(innerSubscription);
  75. this.innerSubscription = innerSubscription;
  76. }
  77. }
  78. protected _next(value: T) {
  79. if (this.hasValue) {
  80. super._next(value);
  81. }
  82. }
  83. notifyNext(): void {
  84. this.hasValue = true;
  85. if (this.innerSubscription) {
  86. this.innerSubscription.unsubscribe();
  87. }
  88. }
  89. notifyComplete() {
  90. /* do nothing */
  91. }
  92. }