timeout.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.timeout = exports.TimeoutError = void 0;
  4. var async_1 = require("../scheduler/async");
  5. var isDate_1 = require("../util/isDate");
  6. var lift_1 = require("../util/lift");
  7. var innerFrom_1 = require("../observable/innerFrom");
  8. var createErrorClass_1 = require("../util/createErrorClass");
  9. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  10. var executeSchedule_1 = require("../util/executeSchedule");
  11. exports.TimeoutError = createErrorClass_1.createErrorClass(function (_super) {
  12. return function TimeoutErrorImpl(info) {
  13. if (info === void 0) { info = null; }
  14. _super(this);
  15. this.message = 'Timeout has occurred';
  16. this.name = 'TimeoutError';
  17. this.info = info;
  18. };
  19. });
  20. function timeout(config, schedulerArg) {
  21. var _a = (isDate_1.isValidDate(config) ? { first: config } : typeof config === 'number' ? { each: config } : config), first = _a.first, each = _a.each, _b = _a.with, _with = _b === void 0 ? timeoutErrorFactory : _b, _c = _a.scheduler, scheduler = _c === void 0 ? schedulerArg !== null && schedulerArg !== void 0 ? schedulerArg : async_1.asyncScheduler : _c, _d = _a.meta, meta = _d === void 0 ? null : _d;
  22. if (first == null && each == null) {
  23. throw new TypeError('No timeout provided.');
  24. }
  25. return lift_1.operate(function (source, subscriber) {
  26. var originalSourceSubscription;
  27. var timerSubscription;
  28. var lastValue = null;
  29. var seen = 0;
  30. var startTimer = function (delay) {
  31. timerSubscription = executeSchedule_1.executeSchedule(subscriber, scheduler, function () {
  32. try {
  33. originalSourceSubscription.unsubscribe();
  34. innerFrom_1.innerFrom(_with({
  35. meta: meta,
  36. lastValue: lastValue,
  37. seen: seen,
  38. })).subscribe(subscriber);
  39. }
  40. catch (err) {
  41. subscriber.error(err);
  42. }
  43. }, delay);
  44. };
  45. originalSourceSubscription = source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  46. timerSubscription === null || timerSubscription === void 0 ? void 0 : timerSubscription.unsubscribe();
  47. seen++;
  48. subscriber.next((lastValue = value));
  49. each > 0 && startTimer(each);
  50. }, undefined, undefined, function () {
  51. if (!(timerSubscription === null || timerSubscription === void 0 ? void 0 : timerSubscription.closed)) {
  52. timerSubscription === null || timerSubscription === void 0 ? void 0 : timerSubscription.unsubscribe();
  53. }
  54. lastValue = null;
  55. }));
  56. !seen && startTimer(first != null ? (typeof first === 'number' ? first : +first - scheduler.now()) : each);
  57. });
  58. }
  59. exports.timeout = timeout;
  60. function timeoutErrorFactory(info) {
  61. throw new exports.TimeoutError(info);
  62. }
  63. //# sourceMappingURL=timeout.js.map