Last active
November 12, 2023 19:23
-
-
Save wyrfel/249513ab905a92f09983f7972474bfc1 to your computer and use it in GitHub Desktop.
RxJS timer() replacement for values more than 24 days in the future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compare https://github.com/ReactiveX/rxjs/issues/3015 | |
import { concat, delay, delayWhen, last, MonoTypeOperatorFunction, Observable, SchedulerLike, take, timer } from 'rxjs'; | |
const MAX_INT = 2147483647; | |
export const longDelay = | |
<T>(due: number | Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T> => | |
(source: Observable<T>): Observable<T> => { | |
if (due <= MAX_INT) { | |
return source.pipe(delay(due, scheduler)); | |
} | |
return source.pipe( | |
delayWhen(() => { | |
if (due instanceof Date) { | |
due = due.getTime() - new Date().getTime(); | |
} | |
const n: number = Math.floor(due / MAX_INT); | |
const r: number = due % MAX_INT; | |
return concat(timer(MAX_INT, MAX_INT, scheduler).pipe(take(n)), timer(r, scheduler)).pipe(last()); | |
}) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment