π Custom RxJS operators 5/7: "
#angular #rxjs #pollWhile
The good old polling mechanism is still quite relevant in applications which rely on data that changes over a period of time. Letβs consider a scenario when an application has to make http requests in a certain time interval until an http response body fulfils a given condition, e.g. analysis status is set to completed. The built-in repeat operator combined with a limiting operator allows to accomplish the goal with a few lines of code.
operator
pollWhile operator"#angular #rxjs #pollWhile
The good old polling mechanism is still quite relevant in applications which rely on data that changes over a period of time. Letβs consider a scenario when an application has to make http requests in a certain time interval until an http response body fulfils a given condition, e.g. analysis status is set to completed. The built-in repeat operator combined with a limiting operator allows to accomplish the goal with a few lines of code.
operator
interface PollWhileConfig<T> {
predicateFn: (v: T) => boolean;
delay: number;
count?: number;
lastOnly?: boolean;
}
function pollWhile<T>({
predicateFn,
delay,
count = Infinity,
lastOnly = false,
}: PollWhileConfig<T>): MonoTypeOperatorFunction<T> {
const limiter = lastOnly
? pipe(
filter((v: T) => !predicateFn(v)),
take(1)
)
: takeWhile(predicateFn, true);
return pipe(repeat({ delay, count }), limiter);
}π₯1