@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
1 lines • 4.88 kB
Source Map (JSON)
{"version":3,"sources":["asynciterable/operators/debounce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAE9D,MAAM,OAAO,qBAA+B,SAAQ,cAAuB;IAIzE,YAAY,MAA8B,EAAE,IAAY;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,MAAoB;QAChD,cAAc,CAAC,MAAM,CAAC,CAAC;QACvB,IAAI,IAAI,GAAG,KAAK,CAAC;QACjB,IAAI,MAAM,GAA2B,GAAG,EAAE;YACxC,IAAI;QACN,CAAC,CAAC;QACF,IAAI,OAAO,GAAqD,GAAG,EAAE;YACnE,IAAI;QACN,CAAC,CAAC;QACF,IAAI,OAAO,GAAG,IAAI,OAAO,CAAU,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE;YAC5C,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,GAAG,EAAE,CAAC;QACd,CAAC,CAAC,CAAC;QACH,CAAC,KAAK,IAAI,EAAE;YACV,IAAI,EAAE,GAAQ,IAAI,CAAC;YAEnB,MAAM,SAAS,GAAG,CAAC,KAAc,EAAE,EAAE;gBACnC,EAAE,GAAG,IAAI,CAAC;gBACV,OAAO,CAAC,KAAK,CAAC,CAAC;gBACf,OAAO,GAAG,IAAI,OAAO,CAAU,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE;oBACxC,OAAO,GAAG,EAAE,CAAC;oBACb,MAAM,GAAG,EAAE,CAAC;gBACd,CAAC,CAAC,CAAC;YACL,CAAC,CAAC;YAEF,IAAI,MAAM,EA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ynciterablex';\nimport { MonoTypeOperatorAsyncFunction } from '../../interfaces';\nimport { AbortError, throwIfAborted } from '../../aborterror';\n\nexport class DebounceAsyncIterable<TSource> extends AsyncIterableX<TSource> {\n private _source: AsyncIterable<TSource>;\n private _time: number;\n\n constructor(source: AsyncIterable<TSource>, time: number) {\n super();\n this._source = source;\n this._time = time;\n }\n\n async *[Symbol.asyncIterator](signal?: AbortSignal) {\n throwIfAborted(signal);\n let done = false;\n let reject: (reason?: any) => void = () => {\n /**/\n };\n let resolve: (value?: TSource | PromiseLike<TSource>) => void = () => {\n /**/\n };\n let promise = new Promise<TSource>((r1, r2) => {\n resolve = r1;\n reject = r2;\n });\n (async () => {\n let id: any = null;\n\n const emitValue = (value: TSource) => {\n id = null;\n resolve(value);\n promise = new Promise<TSource>((r1, r2) => {\n resolve = r1;\n reject = r2;\n });\n };\n\n if (signal) {\n signal.addEventListener(\n 'abort',\n () => {\n done = true;\n if (id) {\n clearTimeout(id);\n }\n id = null;\n reject(new AbortError());\n },\n { once: true }\n );\n }\n\n try {\n let result: IteratorResult<TSource>;\n // @ts-ignore\n const it = this._source[Symbol.asyncIterator](signal);\n // 1. check `!done`\n // 2. await next value\n // 3. check `!done` again, in case the signal aborted while the promise was pending\n while (!done && !(result = await it.next()).done && !done) {\n if (id) {\n clearTimeout(id);\n }\n id = setTimeout(emitValue, this._time, result.value);\n }\n } catch (e) {\n reject(e);\n }\n done = true;\n })();\n\n while (!done) {\n yield await promise;\n }\n }\n}\n\n/**\n * Emits a notification from the source async-iterable only after a particular time span\n * has passed without another source emission.\n *\n * @export\n * @template TSource The type of elements in the source sequence.\n * @param {number} time The timeout duration in milliseconds\n * @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator function which debounces by the given timeout.\n */\nexport function debounce<TSource>(time: number): MonoTypeOperatorAsyncFunction<TSource> {\n return function debounceOperatorFunction(\n source: AsyncIterable<TSource>\n ): AsyncIterableX<TSource> {\n return new DebounceAsyncIterable<TSource>(source, time);\n };\n}\n"]}