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

mergeAll.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { mergeMap } from './mergeMap';
  2. import { identity } from '../util/identity';
  3. import { OperatorFunction, ObservableInput } from '../types';
  4. /**
  5. * Converts a higher-order Observable into a first-order Observable which
  6. * concurrently delivers all values that are emitted on the inner Observables.
  7. *
  8. * <span class="informal">Flattens an Observable-of-Observables.</span>
  9. *
  10. * ![](mergeAll.png)
  11. *
  12. * `mergeAll` subscribes to an Observable that emits Observables, also known as
  13. * a higher-order Observable. Each time it observes one of these emitted inner
  14. * Observables, it subscribes to that and delivers all the values from the
  15. * inner Observable on the output Observable. The output Observable only
  16. * completes once all inner Observables have completed. Any error delivered by
  17. * a inner Observable will be immediately emitted on the output Observable.
  18. *
  19. * ## Examples
  20. * Spawn a new interval Observable for each click event, and blend their outputs as one Observable
  21. * ```ts
  22. * import { fromEvent, interval } from 'rxjs';
  23. * import { map, mergeAll } from 'rxjs/operators';
  24. *
  25. * const clicks = fromEvent(document, 'click');
  26. * const higherOrder = clicks.pipe(map((ev) => interval(1000)));
  27. * const firstOrder = higherOrder.pipe(mergeAll());
  28. * firstOrder.subscribe(x => console.log(x));
  29. * ```
  30. *
  31. * Count from 0 to 9 every second for each click, but only allow 2 concurrent timers
  32. * ```ts
  33. * import { fromEvent, interval } from 'rxjs';
  34. * import { take, map, mergeAll } from 'rxjs/operators';
  35. *
  36. * const clicks = fromEvent(document, 'click');
  37. * const higherOrder = clicks.pipe(
  38. * map((ev) => interval(1000).pipe(take(10))),
  39. * );
  40. * const firstOrder = higherOrder.pipe(mergeAll(2));
  41. * firstOrder.subscribe(x => console.log(x));
  42. * ```
  43. *
  44. * @see {@link combineAll}
  45. * @see {@link concatAll}
  46. * @see {@link exhaust}
  47. * @see {@link merge}
  48. * @see {@link mergeMap}
  49. * @see {@link mergeMapTo}
  50. * @see {@link mergeScan}
  51. * @see {@link switchAll}
  52. * @see {@link switchMap}
  53. * @see {@link zipAll}
  54. *
  55. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of inner
  56. * Observables being subscribed to concurrently.
  57. * @return {Observable} An Observable that emits values coming from all the
  58. * inner Observables emitted by the source Observable.
  59. * @method mergeAll
  60. * @owner Observable
  61. */
  62. export function mergeAll<T>(concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<ObservableInput<T>, T> {
  63. return mergeMap(identity, concurrent);
  64. }