timeoutWith.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var rxjs_1 = require("rxjs");
  4. var operators_1 = require("rxjs/operators");
  5. /* tslint:enable:max-line-length */
  6. /**
  7. *
  8. * Errors if Observable does not emit a value in given time span, in case of which
  9. * subscribes to the second Observable.
  10. *
  11. * <span class="informal">It's a version of `timeout` operator that let's you specify fallback Observable.</span>
  12. *
  13. * <img src="./img/timeoutWith.png" width="100%">
  14. *
  15. * `timeoutWith` is a variation of `timeout` operator. It behaves exactly the same,
  16. * still accepting as a first argument either a number or a Date, which control - respectively -
  17. * when values of source Observable should be emitted or when it should complete.
  18. *
  19. * The only difference is that it accepts a second, required parameter. This parameter
  20. * should be an Observable which will be subscribed when source Observable fails any timeout check.
  21. * So whenever regular `timeout` would emit an error, `timeoutWith` will instead start re-emitting
  22. * values from second Observable. Note that this fallback Observable is not checked for timeouts
  23. * itself, so it can emit values and complete at arbitrary points in time. From the moment of a second
  24. * subscription, Observable returned from `timeoutWith` simply mirrors fallback stream. When that
  25. * stream completes, it completes as well.
  26. *
  27. * Scheduler, which in case of `timeout` is provided as as second argument, can be still provided
  28. * here - as a third, optional parameter. It still is used to schedule timeout checks and -
  29. * as a consequence - when second Observable will be subscribed, since subscription happens
  30. * immediately after failing check.
  31. *
  32. * @example <caption>Add fallback observable</caption>
  33. * const seconds = Rx.Observable.interval(1000);
  34. * const minutes = Rx.Observable.interval(60 * 1000);
  35. *
  36. * seconds.timeoutWith(900, minutes)
  37. * .subscribe(
  38. * value => console.log(value), // After 900ms, will start emitting `minutes`,
  39. * // since first value of `seconds` will not arrive fast enough.
  40. * err => console.log(err) // Would be called after 900ms in case of `timeout`,
  41. * // but here will never be called.
  42. * );
  43. *
  44. * @param {number|Date} due Number specifying period within which Observable must emit values
  45. * or Date specifying before when Observable should complete
  46. * @param {Observable<T>} withObservable Observable which will be subscribed if source fails timeout check.
  47. * @param {Scheduler} [scheduler] Scheduler controlling when timeout checks occur.
  48. * @return {Observable<T>} Observable that mirrors behaviour of source or, when timeout check fails, of an Observable
  49. * passed as a second parameter.
  50. * @method timeoutWith
  51. * @owner Observable
  52. */
  53. function timeoutWith(due, withObservable, scheduler) {
  54. if (scheduler === void 0) { scheduler = rxjs_1.asyncScheduler; }
  55. return operators_1.timeoutWith(due, withObservable, scheduler)(this);
  56. }
  57. exports.timeoutWith = timeoutWith;
  58. //# sourceMappingURL=timeoutWith.js.map