switchMap.d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Observable, ObservableInput } from 'rxjs';
  2. /**
  3. * Projects each source value to an Observable which is merged in the output
  4. * Observable, emitting values only from the most recently projected Observable.
  5. *
  6. * <span class="informal">Maps each value to an Observable, then flattens all of
  7. * these inner Observables using {@link switch}.</span>
  8. *
  9. * <img src="./img/switchMap.png" width="100%">
  10. *
  11. * Returns an Observable that emits items based on applying a function that you
  12. * supply to each item emitted by the source Observable, where that function
  13. * returns an (so-called "inner") Observable. Each time it observes one of these
  14. * inner Observables, the output Observable begins emitting the items emitted by
  15. * that inner Observable. When a new inner Observable is emitted, `switchMap`
  16. * stops emitting items from the earlier-emitted inner Observable and begins
  17. * emitting items from the new one. It continues to behave like this for
  18. * subsequent inner Observables.
  19. *
  20. * @example <caption>Rerun an interval Observable on every click event</caption>
  21. * var clicks = Rx.Observable.fromEvent(document, 'click');
  22. * var result = clicks.switchMap((ev) => Rx.Observable.interval(1000));
  23. * result.subscribe(x => console.log(x));
  24. *
  25. * @see {@link concatMap}
  26. * @see {@link exhaustMap}
  27. * @see {@link mergeMap}
  28. * @see {@link switch}
  29. * @see {@link switchMapTo}
  30. *
  31. * @param {function(value: T, ?index: number): ObservableInput} project A function
  32. * that, when applied to an item emitted by the source Observable, returns an
  33. * Observable.
  34. * @return {Observable} An Observable that emits the result of applying the
  35. * projection function (and the optional `resultSelector`) to each item emitted
  36. * by the source Observable and taking only the values from the most recently
  37. * projected inner Observable.
  38. * @method switchMap
  39. * @owner Observable
  40. */
  41. export declare function switchMap<T, R>(this: Observable<T>, project: (value: T, index: number) => ObservableInput<R>): Observable<R>;