Angular πŸ‡ΊπŸ‡¦ - practical notes
1.63K subscribers
1.6K photos
1 file
532 links
Angular - practical notes

This group is for posting practical notes for Angular developers. Mostly all posts are for quick implementation https://t.me/angular_practical_notes (Commenting on posts only in ENG and UA langs here). Welcome!
Download Telegram
πŸ“„ Custom RxJS operators 5/7: "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