56902739abcd9bd3c3d72a6d3ee6e9393091daba258231557ada40c5314e8a0aac42ab78b102e5af0e13a8dbf94ff330b080630b96dd0c3971b9d0732e3b3e 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { ObservableInputTuple, OperatorFunction } from '../types';
  2. import { concat } from './concat';
  3. /**
  4. * Emits all of the values from the source observable, then, once it completes, subscribes
  5. * to each observable source provided, one at a time, emitting all of their values, and not subscribing
  6. * to the next one until it completes.
  7. *
  8. * `concat(a$, b$, c$)` is the same as `a$.pipe(concatWith(b$, c$))`.
  9. *
  10. * ## Example
  11. *
  12. * Listen for one mouse click, then listen for all mouse moves.
  13. *
  14. * ```ts
  15. * import { fromEvent, map, take, concatWith } from 'rxjs';
  16. *
  17. * const clicks$ = fromEvent(document, 'click');
  18. * const moves$ = fromEvent(document, 'mousemove');
  19. *
  20. * clicks$.pipe(
  21. * map(() => 'click'),
  22. * take(1),
  23. * concatWith(
  24. * moves$.pipe(
  25. * map(() => 'move')
  26. * )
  27. * )
  28. * )
  29. * .subscribe(x => console.log(x));
  30. *
  31. * // 'click'
  32. * // 'move'
  33. * // 'move'
  34. * // 'move'
  35. * // ...
  36. * ```
  37. *
  38. * @param otherSources Other observable sources to subscribe to, in sequence, after the original source is complete.
  39. * @return A function that returns an Observable that concatenates
  40. * subscriptions to the source and provided Observables subscribing to the next
  41. * only once the current subscription completes.
  42. */
  43. export function concatWith<T, A extends readonly unknown[]>(
  44. ...otherSources: [...ObservableInputTuple<A>]
  45. ): OperatorFunction<T, T | A[number]> {
  46. return concat(...otherSources);
  47. }