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

switchMapTo.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { ObservableInput, OperatorFunction } from '../types';
  2. import { switchMap } from './switchMap';
  3. /* tslint:disable:max-line-length */
  4. export function switchMapTo<R>(observable: ObservableInput<R>): OperatorFunction<any, R>;
  5. /** @deprecated resultSelector is no longer supported. Switch to using switchMap with an inner map */
  6. export function switchMapTo<T, R>(observable: ObservableInput<R>, resultSelector: undefined): OperatorFunction<T, R>;
  7. /** @deprecated resultSelector is no longer supported. Switch to using switchMap with an inner map */
  8. export function switchMapTo<T, I, R>(observable: ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
  9. /* tslint:enable:max-line-length */
  10. /**
  11. * Projects each source value to the same Observable which is flattened multiple
  12. * times with {@link switchMap} in the output Observable.
  13. *
  14. * <span class="informal">It's like {@link switchMap}, but maps each value
  15. * always to the same inner Observable.</span>
  16. *
  17. * ![](switchMapTo.png)
  18. *
  19. * Maps each source value to the given Observable `innerObservable` regardless
  20. * of the source value, and then flattens those resulting Observables into one
  21. * single Observable, which is the output Observable. The output Observables
  22. * emits values only from the most recently emitted instance of
  23. * `innerObservable`.
  24. *
  25. * ## Example
  26. * Rerun an interval Observable on every click event
  27. * ```ts
  28. * import { fromEvent, interval } from 'rxjs';
  29. * import { switchMapTo } from 'rxjs/operators';
  30. *
  31. * const clicks = fromEvent(document, 'click');
  32. * const result = clicks.pipe(switchMapTo(interval(1000)));
  33. * result.subscribe(x => console.log(x));
  34. * ```
  35. *
  36. * @see {@link concatMapTo}
  37. * @see {@link switchAll}
  38. * @see {@link switchMap}
  39. * @see {@link mergeMapTo}
  40. *
  41. * @param {ObservableInput} innerObservable An Observable to replace each value from
  42. * the source Observable.
  43. * @return {Observable} An Observable that emits items from the given
  44. * `innerObservable` (and optionally transformed through the deprecated `resultSelector`)
  45. * every time a value is emitted on the source Observable, and taking only the values
  46. * from the most recently projected inner Observable.
  47. * @method switchMapTo
  48. * @owner Observable
  49. */
  50. export function switchMapTo<T, I, R>(
  51. innerObservable: ObservableInput<I>,
  52. resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R
  53. ): OperatorFunction<T, I|R> {
  54. return resultSelector ? switchMap(() => innerObservable, resultSelector) : switchMap(() => innerObservable);
  55. }