Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

combineAll.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { CombineLatestOperator } from '../observable/combineLatest';
  2. import { Observable } from '../Observable';
  3. import { OperatorFunction, ObservableInput } from '../types';
  4. export function combineAll<T>(): OperatorFunction<ObservableInput<T>, T[]>;
  5. export function combineAll<T>(): OperatorFunction<any, T[]>;
  6. export function combineAll<T, R>(project: (...values: T[]) => R): OperatorFunction<ObservableInput<T>, R>;
  7. export function combineAll<R>(project: (...values: Array<any>) => R): OperatorFunction<any, R>;
  8. /**
  9. * Flattens an Observable-of-Observables by applying {@link combineLatest} when the Observable-of-Observables completes.
  10. *
  11. * ![](combineAll.png)
  12. *
  13. * `combineAll` takes an Observable of Observables, and collects all Observables from it. Once the outer Observable completes,
  14. * it subscribes to all collected Observables and combines their values using the {@link combineLatest}</a> strategy, such that:
  15. *
  16. * * Every time an inner Observable emits, the output Observable emits
  17. * * When the returned observable emits, it emits all of the latest values by:
  18. * * If a `project` function is provided, it is called with each recent value from each inner Observable in whatever order they
  19. * arrived, and the result of the `project` function is what is emitted by the output Observable.
  20. * * If there is no `project` function, an array of all the most recent values is emitted by the output Observable.
  21. *
  22. * ---
  23. *
  24. * ## Examples
  25. *
  26. * ### Map two click events to a finite interval Observable, then apply `combineAll`
  27. *
  28. * ```ts
  29. * import { fromEvent, interval } from 'rxjs';
  30. * import { map, combineAll, take } from 'rxjs/operators';
  31. *
  32. * const clicks = fromEvent(document, 'click');
  33. * const higherOrder = clicks.pipe(
  34. * map(ev =>
  35. * interval(Math.random() * 2000).pipe(take(3))
  36. * ),
  37. * take(2)
  38. * );
  39. * const result = higherOrder.pipe(
  40. * combineAll()
  41. * );
  42. *
  43. * result.subscribe(x => console.log(x));
  44. * ```
  45. *
  46. * @see {@link combineLatest}
  47. * @see {@link mergeAll}
  48. *
  49. * @param {function(...values: Array<any>)} An optional function to map the most recent values from each inner Observable into a new result.
  50. * Takes each of the most recent values from each collected inner Observable as arguments, in order.
  51. * @return {Observable<T>}
  52. * @name combineAll
  53. */
  54. export function combineAll<T, R>(project?: (...values: Array<any>) => R): OperatorFunction<T, R> {
  55. return (source: Observable<T>) => source.lift(new CombineLatestOperator(project));
  56. }