@bemedev/rx-add-ons
Version:
A collection of RxJS operators and utilities to enhance reactive programming capabilities.
1 lines • 6.02 kB
Source Map (JSON)
{"version":3,"file":"time.cjs","sources":["../src/time.ts"],"sourcesContent":["import { EMPTY, Observable } from 'rxjs';\nimport { race } from 'rxjs/internal/observable/race';\nimport { timer } from 'rxjs/internal/observable/timer';\nimport { switchMap } from 'rxjs/internal/operators/switchMap';\n\n/**\n * Switches to a fallback Observable if the source doesn't emit within the specified timeout.\n * Unlike the standard timeout operator, this doesn't error but switches to the fallback.\n *\n * @param timeout - Timeout duration in milliseconds\n * @param fallback - Observable to switch to on timeout\n * @returns A function that returns an Observable that emits from source or fallback\n *\n * @example\n * ```typescript\n * // Switch to fallback after 5 seconds\n * source$.pipe(\n * timeoutWithFallback(5000, of('timeout'))\n * )\n *\n * // Switch to alternative data source on timeout\n * source$.pipe(\n * timeoutWithFallback(3000, alternativeSource$)\n * )\n * ```\n */\nexport const timeoutWithFallback = <T>(\n timeout: number,\n fallback: Observable<T> = EMPTY,\n): ((source: Observable<T>) => Observable<T>) => {\n return source => {\n return race(source, timer(timeout).pipe(switchMap(() => fallback)));\n };\n};\n\n/**\n * Switches to a fallback Observable if the source doesn't emit within the specified timeout.\n * Unlike the standard timeout operator, this doesn't error but switches to the fallback.\n *\n * @param timeout - Timeout duration in milliseconds\n * @param fallback - Observable to switch to on timeout\n * @returns A function that returns an Observable that emits from source or fallback\n *\n * @example\n * ```typescript\n * // Switch to fallback after 5 seconds\n * source$.pipe(\n * timeoutWithFallback(5000, of('timeout'))\n * )\n *\n * // Switch to alternative data source on timeout\n * source$.pipe(\n * timeoutWithFallback(3000, alternativeSource$)\n * )\n * ```\n */\nexport const timeout = timeoutWithFallback;\n\n/**\n * Timeout with fallback that can be configured per emission\n * Resets timeout on each emission from source\n *\n * @see {@linkcode timeoutWithFallback}\n */\nexport const tickWithFallback = <T>(\n timeout: number,\n fallback: Observable<T> = EMPTY,\n): ((source: Observable<T>) => Observable<T>) => {\n return source => {\n return new Observable<T>(subscriber => {\n let timeoutHandle: NodeJS.Timeout | null = null;\n let isCompleted = false;\n\n const resetTimeout = () => {\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n\n timeoutHandle = setTimeout(() => {\n if (!isCompleted) {\n isCompleted = true;\n sourceSubscription.unsubscribe();\n\n // Switch to fallback\n fallback.subscribe({\n next: value => subscriber.next(value),\n error: err => subscriber.error(err),\n complete: () => subscriber.complete(),\n });\n }\n }, timeout);\n };\n\n // Start initial timeout\n resetTimeout();\n\n const sourceSubscription = source.subscribe({\n next: value => {\n if (!isCompleted) {\n subscriber.next(value);\n resetTimeout(); // Reset timeout on each emission\n }\n },\n error: err => {\n if (!isCompleted) {\n isCompleted = true;\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n subscriber.error(err);\n }\n },\n complete: () => {\n if (!isCompleted) {\n isCompleted = true;\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n subscriber.complete();\n }\n },\n });\n\n return () => {\n isCompleted = true;\n if (timeoutHandle) {\n clearTimeout(timeoutHandle);\n }\n sourceSubscription.unsubscribe();\n };\n });\n };\n};\n\n/**\n * Timeout with fallback that can be configured per emission\n * Resets timeout on each emission from source\n *\n * @see {@linkcode timeoutWithFallback}\n */\nexport const tick = tickWithFallback;\n"],"names":["EMPTY","race","timer","switchMap","Observable"],"mappings":";;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;AAoBG;AACI,MAAM,mBAAmB,GAAG,CACjC,OAAe,EACf,QAAA,GAA0BA,UAAK,KACe;IAC9C,OAAO,MAAM,IAAG;QACd,OAAOC,SAAI,CAAC,MAAM,EAAEC,WAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAACC,mBAAS,CAAC,MAAM,QAAQ,CAAC,CAAC,CAAC;AACrE,IAAA,CAAC;AACH;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACI,MAAM,OAAO,GAAG;AAEvB;;;;;AAKG;AACI,MAAM,gBAAgB,GAAG,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