Skip to content

Instantly share code, notes, and snippets.

@wyrfel
Last active November 12, 2023 19:23
Show Gist options
  • Save wyrfel/249513ab905a92f09983f7972474bfc1 to your computer and use it in GitHub Desktop.
Save wyrfel/249513ab905a92f09983f7972474bfc1 to your computer and use it in GitHub Desktop.
RxJS timer() replacement for values more than 24 days in the future
// 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