813bd167caaf75e9086ef41ccf80110b56d91654ad890cd5c0a222a705b9a8ade439b31b41c778bf65e658e759d6c5061eae8ddf929347ee524f6967030f3a 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { ObservableInputTuple, OperatorFunction } from '../types';
  2. import { merge } from './merge';
  3. /**
  4. * Merge the values from all observables to a single observable result.
  5. *
  6. * Creates an observable, that when subscribed to, subscribes to the source
  7. * observable, and all other sources provided as arguments. All values from
  8. * every source are emitted from the resulting subscription.
  9. *
  10. * When all sources complete, the resulting observable will complete.
  11. *
  12. * When any source errors, the resulting observable will error.
  13. *
  14. * ## Example
  15. *
  16. * Joining all outputs from multiple user input event streams
  17. *
  18. * ```ts
  19. * import { fromEvent, map, mergeWith } from 'rxjs';
  20. *
  21. * const clicks$ = fromEvent(document, 'click').pipe(map(() => 'click'));
  22. * const mousemoves$ = fromEvent(document, 'mousemove').pipe(map(() => 'mousemove'));
  23. * const dblclicks$ = fromEvent(document, 'dblclick').pipe(map(() => 'dblclick'));
  24. *
  25. * mousemoves$
  26. * .pipe(mergeWith(clicks$, dblclicks$))
  27. * .subscribe(x => console.log(x));
  28. *
  29. * // result (assuming user interactions)
  30. * // 'mousemove'
  31. * // 'mousemove'
  32. * // 'mousemove'
  33. * // 'click'
  34. * // 'click'
  35. * // 'dblclick'
  36. * ```
  37. *
  38. * @see {@link merge}
  39. *
  40. * @param otherSources the sources to combine the current source with.
  41. * @return A function that returns an Observable that merges the values from
  42. * all given Observables.
  43. */
  44. export function mergeWith<T, A extends readonly unknown[]>(
  45. ...otherSources: [...ObservableInputTuple<A>]
  46. ): OperatorFunction<T, T | A[number]> {
  47. return merge(...otherSources);
  48. }