d4a363a824a798ea82217869234e4a84ca4e715ef7f3ca69c3f17754cc8aacabee7ac2496b36dbd49321b41a901ab1b619d86f4951ab12b6593766229e8b78 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { OperatorFunction, ObservableInputTuple } from '../types';
  2. import { raceInit } from '../observable/race';
  3. import { operate } from '../util/lift';
  4. import { identity } from '../util/identity';
  5. /**
  6. * Creates an Observable that mirrors the first source Observable to emit a next,
  7. * error or complete notification from the combination of the Observable to which
  8. * the operator is applied and supplied Observables.
  9. *
  10. * ## Example
  11. *
  12. * ```ts
  13. * import { interval, map, raceWith } from 'rxjs';
  14. *
  15. * const obs1 = interval(7000).pipe(map(() => 'slow one'));
  16. * const obs2 = interval(3000).pipe(map(() => 'fast one'));
  17. * const obs3 = interval(5000).pipe(map(() => 'medium one'));
  18. *
  19. * obs1
  20. * .pipe(raceWith(obs2, obs3))
  21. * .subscribe(winner => console.log(winner));
  22. *
  23. * // Outputs
  24. * // a series of 'fast one'
  25. * ```
  26. *
  27. * @param otherSources Sources used to race for which Observable emits first.
  28. * @return A function that returns an Observable that mirrors the output of the
  29. * first Observable to emit an item.
  30. */
  31. export function raceWith<T, A extends readonly unknown[]>(
  32. ...otherSources: [...ObservableInputTuple<A>]
  33. ): OperatorFunction<T, T | A[number]> {
  34. return !otherSources.length
  35. ? identity
  36. : operate((source, subscriber) => {
  37. raceInit<T | A[number]>([source, ...otherSources])(subscriber);
  38. });
  39. }