@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
1 lines • 4.87 kB
Source Map (JSON)
{"version":3,"sources":["asynciterable/operators/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAElC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YAAY,UAAkB,sBAAsB;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;QACpD,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAChD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;QACtB,OAAO,cAAc,CAAC;IACxB,CAAC;CACF;AAED,MAAM,CAAC,cAAc,CAAC,YAAY,EAAE,MAAM,CAAC,WAAW,EAAE;IACtD,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,KAAK,CAAC,CAAM;QACV,OAAO,CACL,QAAQ,CAAC,CAAC,CAAC;YACX,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,KAAK,cAAc,IAAI,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,KAAK,cAAc,CAAC,CACpF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,UAAU,GAAG,OAAO,CAAC;AAC3B,MAAM,UAAU,GAAG,OAAO,CAAC;AAO3B,MAAM,OAAO,oBAA8B,SAAQ,cAAuB;IAIxE,YAAY,MAA8B,EAAE,OAAe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nimport { MonoTypeOperatorAsyncFunction } from '../../interfaces';\nimport { wrapWithAbort } from './withabort';\nimport { throwIfAborted } from '../../aborterror';\nimport { isObject } from '../../util/isiterable';\n\nexport class TimeoutError extends Error {\n constructor(message: string = 'Timeout has occurred') {\n super(message);\n Object.setPrototypeOf(this, TimeoutError.prototype);\n Error.captureStackTrace(this, this.constructor);\n this.name = 'TimeoutError';\n }\n\n get [Symbol.toStringTag]() {\n return 'TimeoutError';\n }\n}\n\nObject.defineProperty(TimeoutError, Symbol.hasInstance, {\n writable: true,\n configurable: true,\n value(x: any) {\n return (\n isObject(x) &&\n (x.constructor.name === 'TimeoutError' || x[Symbol.toStringTag] === 'TimeoutError')\n );\n },\n});\n\nconst VALUE_TYPE = 'value';\nconst ERROR_TYPE = 'error';\n\ninterface TimeoutOperation<T> {\n type: string;\n value?: IteratorResult<T>;\n}\n\nexport class TimeoutAsyncIterable<TSource> extends AsyncIterableX<TSource> {\n private _source: AsyncIterable<TSource>;\n private _dueTime: number;\n\n constructor(source: AsyncIterable<TSource>, dueTime: number) {\n super();\n this._source = source;\n this._dueTime = dueTime;\n }\n\n async *[Symbol.asyncIterator](signal?: AbortSignal) {\n throwIfAborted(signal);\n const it = wrapWithAbort(this._source, signal)[Symbol.asyncIterator]();\n while (1) {\n const { type, value } = await Promise.race<TimeoutOperation<TSource>>([\n it.next().then((val) => {\n return { type: VALUE_TYPE, val };\n }),\n sleep(this._dueTime, signal).then(() => {\n return { type: ERROR_TYPE };\n }),\n ]);\n\n if (type === ERROR_TYPE) {\n throw new TimeoutError();\n }\n\n if (!value || value.done) {\n break;\n }\n yield value.value;\n }\n }\n}\n\n/**\n * Applies a timeout policy for each element in the async-iterable sequence.\n * If the next element isn't received within the specified timeout duration starting from its predecessor, a TimeoutError is thrown.\n *\n * @export\n * @template TSource The type of the elements in the source sequence.\n * @param {number} dueTime Maximum duration in milliseconds between values before a timeout occurs.\n * @returns {MonoTypeOperatorAsyncFunction<TSource>} The source sequence with a TimeoutError in case of a timeout.\n */\nexport function timeout<TSource>(dueTime: number): MonoTypeOperatorAsyncFunction<TSource> {\n return function timeoutOperatorFunction(source: AsyncIterable<TSource>): AsyncIterableX<TSource> {\n return new TimeoutAsyncIterable<TSource>(source, dueTime);\n };\n}\n"]}