a893c5ac6cde2d00ec5f03618a607d2ff5d93fa1364a4302c6e09f1447da2300daa8aeba61ebbf916fad3fcc291ff847cfab2153f7866a83659fb0b06c22db 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { MonoTypeOperatorFunction, ObservableInput } from '../types';
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. import { innerFrom } from '../observable/innerFrom';
  5. import { noop } from '../util/noop';
  6. /**
  7. * Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.
  8. *
  9. * The `skipUntil` operator causes the observable stream to skip the emission of values until the passed in observable
  10. * emits the first value. This can be particularly useful in combination with user interactions, responses of HTTP
  11. * requests or waiting for specific times to pass by.
  12. *
  13. * ![](skipUntil.png)
  14. *
  15. * Internally, the `skipUntil` operator subscribes to the passed in `notifier` `ObservableInput` (which gets converted
  16. * to an Observable) in order to recognize the emission of its first value. When `notifier` emits next, the operator
  17. * unsubscribes from it and starts emitting the values of the *source* observable until it completes or errors. It
  18. * will never let the *source* observable emit any values if the `notifier` completes or throws an error without
  19. * emitting a value before.
  20. *
  21. * ## Example
  22. *
  23. * In the following example, all emitted values of the interval observable are skipped until the user clicks anywhere
  24. * within the page
  25. *
  26. * ```ts
  27. * import { interval, fromEvent, skipUntil } from 'rxjs';
  28. *
  29. * const intervalObservable = interval(1000);
  30. * const click = fromEvent(document, 'click');
  31. *
  32. * const emitAfterClick = intervalObservable.pipe(
  33. * skipUntil(click)
  34. * );
  35. * // clicked at 4.6s. output: 5...6...7...8........ or
  36. * // clicked at 7.3s. output: 8...9...10..11.......
  37. * emitAfterClick.subscribe(value => console.log(value));
  38. * ```
  39. *
  40. * @see {@link last}
  41. * @see {@link skip}
  42. * @see {@link skipWhile}
  43. * @see {@link skipLast}
  44. *
  45. * @param notifier An `ObservableInput` that has to emit an item before the source Observable elements begin to
  46. * be mirrored by the resulting Observable.
  47. * @return A function that returns an Observable that skips items from the
  48. * source Observable until the `notifier` Observable emits an item, then emits the
  49. * remaining items.
  50. */
  51. export function skipUntil<T>(notifier: ObservableInput<any>): MonoTypeOperatorFunction<T> {
  52. return operate((source, subscriber) => {
  53. let taking = false;
  54. const skipSubscriber = createOperatorSubscriber(
  55. subscriber,
  56. () => {
  57. skipSubscriber?.unsubscribe();
  58. taking = true;
  59. },
  60. noop
  61. );
  62. innerFrom(notifier).subscribe(skipSubscriber);
  63. source.subscribe(createOperatorSubscriber(subscriber, (value) => taking && subscriber.next(value)));
  64. });
  65. }