UNPKG

backoff-rxjs

Version:

A collection of helpful RxJS operators to deal with backoff strategies (like exponential backoff)

12 lines (11 loc) 775 B
import { of, timer, asyncScheduler, expand, mapTo } from 'rxjs'; import { exponentialBackoffDelay, getDelay } from '../utils'; export function intervalBackoff(config, scheduler) { if (scheduler === void 0) { scheduler = asyncScheduler; } var _a = typeof config === 'number' ? { initialInterval: config } : config, initialInterval = _a.initialInterval, _b = _a.maxInterval, maxInterval = _b === void 0 ? Infinity : _b, _c = _a.backoffDelay, backoffDelay = _c === void 0 ? exponentialBackoffDelay : _c; initialInterval = initialInterval < 0 ? 0 : initialInterval; return of(0, scheduler).pipe(expand(function (iteration) { return timer(getDelay(backoffDelay(iteration, initialInterval), maxInterval)) .pipe(mapTo(iteration + 1)); })); }