UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

112 lines (110 loc) 4.68 kB
import { __asyncGenerator, __await, __extends, __generator } from "tslib"; import { AsyncIterableX } from '../asynciterablex.js'; import { sleep } from '../_sleep.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; import { isObject } from '../../util/isiterable.js'; import { safeRace } from '../../util/safeRace.js'; import { returnAsyncIterator } from '../../util/returniterator.js'; /** @ignore */ var TimeoutError = /** @class */ (function (_super) { __extends(TimeoutError, _super); function TimeoutError(message) { if (message === void 0) { message = 'Timeout has occurred'; } var _this = _super.call(this, message) || this; Object.setPrototypeOf(_this, TimeoutError.prototype); Error.captureStackTrace(_this, _this.constructor); _this.name = 'TimeoutError'; return _this; } Object.defineProperty(TimeoutError.prototype, Symbol.toStringTag, { get: function () { return 'TimeoutError'; }, enumerable: false, configurable: true }); return TimeoutError; }(Error)); export { TimeoutError }; Object.defineProperty(TimeoutError, Symbol.hasInstance, { writable: true, configurable: true, value: function (x) { return (isObject(x) && (x.constructor.name === 'TimeoutError' || x[Symbol.toStringTag] === 'TimeoutError')); }, }); var VALUE_TYPE = 'value'; var ERROR_TYPE = 'error'; /** @ignore */ var TimeoutAsyncIterable = /** @class */ (function (_super) { __extends(TimeoutAsyncIterable, _super); function TimeoutAsyncIterable(source, dueTime) { var _this = _super.call(this) || this; _this._source = source; _this._dueTime = dueTime; return _this; } TimeoutAsyncIterable.prototype[Symbol.asyncIterator] = function (signal) { return __asyncGenerator(this, arguments, function _a() { var it, _b, type, value; return __generator(this, function (_c) { switch (_c.label) { case 0: throwIfAborted(signal); it = wrapWithAbort(this._source, signal)[Symbol.asyncIterator](); _c.label = 1; case 1: _c.trys.push([1, , 7, 9]); _c.label = 2; case 2: if (!1) return [3 /*break*/, 6]; return [4 /*yield*/, __await(safeRace([ it.next().then(function (val) { return { type: VALUE_TYPE, value: val }; }), sleep(this._dueTime, signal).then(function () { return { type: ERROR_TYPE }; }), ]))]; case 3: _b = _c.sent(), type = _b.type, value = _b.value; if (type === ERROR_TYPE) { throw new TimeoutError(); } if (!value || value.done) { return [3 /*break*/, 6]; } return [4 /*yield*/, __await(value.value)]; case 4: return [4 /*yield*/, _c.sent()]; case 5: _c.sent(); return [3 /*break*/, 2]; case 6: return [3 /*break*/, 9]; case 7: return [4 /*yield*/, __await(returnAsyncIterator(it))]; case 8: _c.sent(); return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); }; return TimeoutAsyncIterable; }(AsyncIterableX)); export { TimeoutAsyncIterable }; /** * Applies a timeout policy for each element in the async-iterable sequence. * If the next element isn't received within the specified timeout duration starting from its predecessor, a TimeoutError is thrown. * * @template TSource The type of the elements in the source sequence. * @param {number} dueTime Maximum duration in milliseconds between values before a timeout occurs. * @returns {MonoTypeOperatorAsyncFunction<TSource>} The source sequence with a TimeoutError in case of a timeout. */ export function timeout(dueTime) { return function timeoutOperatorFunction(source) { return new TimeoutAsyncIterable(source, dueTime); }; } //# sourceMappingURL=timeout.js.map