switch.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /**
  5. * Converts a higher-order Observable into a first-order Observable by
  6. * subscribing to only the most recently emitted of those inner Observables.
  7. *
  8. * <span class="informal">Flattens an Observable-of-Observables by dropping the
  9. * previous inner Observable once a new one appears.</span>
  10. *
  11. * <img src="./img/switch.png" width="100%">
  12. *
  13. * `switch` subscribes to an Observable that emits Observables, also known as a
  14. * higher-order Observable. Each time it observes one of these emitted inner
  15. * Observables, the output Observable subscribes to the inner Observable and
  16. * begins emitting the items emitted by that. So far, it behaves
  17. * like {@link mergeAll}. However, when a new inner Observable is emitted,
  18. * `switch` unsubscribes from the earlier-emitted inner Observable and
  19. * subscribes to the new inner Observable and begins emitting items from it. It
  20. * continues to behave like this for subsequent inner Observables.
  21. *
  22. * @example <caption>Rerun an interval Observable on every click event</caption>
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * // Each click event is mapped to an Observable that ticks every second
  25. * var higherOrder = clicks.map((ev) => Rx.Observable.interval(1000));
  26. * var switched = higherOrder.switch();
  27. * // The outcome is that `switched` is essentially a timer that restarts
  28. * // on every click. The interval Observables from older clicks do not merge
  29. * // with the current interval Observable.
  30. * switched.subscribe(x => console.log(x));
  31. *
  32. * @see {@link combineAll}
  33. * @see {@link concatAll}
  34. * @see {@link exhaust}
  35. * @see {@link mergeAll}
  36. * @see {@link switchMap}
  37. * @see {@link switchMapTo}
  38. * @see {@link zipAll}
  39. *
  40. * @return {Observable<T>} An Observable that emits the items emitted by the
  41. * Observable most recently emitted by the source Observable.
  42. * @method switch
  43. * @name switch
  44. * @owner Observable
  45. */
  46. function _switch() {
  47. return operators_1.switchAll()(this);
  48. }
  49. exports._switch = _switch;
  50. //# sourceMappingURL=switch.js.map