391294ae8ca7f6d8236c59d6bc50e1c695e029732567fbe1576dab9b83a636bfdb694b66e13f486de51c7c12db134576286f03ddd22c68a010ca0d6068b5ac 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { ObservableInputTuple, OperatorFunction, Cons } from '../types';
  2. import { combineLatest } from './combineLatest';
  3. /**
  4. * Create an observable that combines the latest values from all passed observables and the source
  5. * into arrays and emits them.
  6. *
  7. * Returns an observable, that when subscribed to, will subscribe to the source observable and all
  8. * sources provided as arguments. Once all sources emit at least one value, all of the latest values
  9. * will be emitted as an array. After that, every time any source emits a value, all of the latest values
  10. * will be emitted as an array.
  11. *
  12. * This is a useful operator for eagerly calculating values based off of changed inputs.
  13. *
  14. * ## Example
  15. *
  16. * Simple concatenation of values from two inputs
  17. *
  18. * ```ts
  19. * import { fromEvent, combineLatestWith, map } from 'rxjs';
  20. *
  21. * // Setup: Add two inputs to the page
  22. * const input1 = document.createElement('input');
  23. * document.body.appendChild(input1);
  24. * const input2 = document.createElement('input');
  25. * document.body.appendChild(input2);
  26. *
  27. * // Get streams of changes
  28. * const input1Changes$ = fromEvent(input1, 'change');
  29. * const input2Changes$ = fromEvent(input2, 'change');
  30. *
  31. * // Combine the changes by adding them together
  32. * input1Changes$.pipe(
  33. * combineLatestWith(input2Changes$),
  34. * map(([e1, e2]) => (<HTMLInputElement>e1.target).value + ' - ' + (<HTMLInputElement>e2.target).value)
  35. * )
  36. * .subscribe(x => console.log(x));
  37. * ```
  38. *
  39. * @param otherSources the other sources to subscribe to.
  40. * @return A function that returns an Observable that emits the latest
  41. * emissions from both source and provided Observables.
  42. */
  43. export function combineLatestWith<T, A extends readonly unknown[]>(
  44. ...otherSources: [...ObservableInputTuple<A>]
  45. ): OperatorFunction<T, Cons<T, A>> {
  46. return combineLatest(...otherSources);
  47. }