UNPKG

backoff-rxjs

Version:

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

16 lines (15 loc) 936 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.intervalBackoff = void 0; var rxjs_1 = require("rxjs"); var utils_1 = require("../utils"); function intervalBackoff(config, scheduler) { if (scheduler === void 0) { scheduler = rxjs_1.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 ? utils_1.exponentialBackoffDelay : _c; initialInterval = initialInterval < 0 ? 0 : initialInterval; return (0, rxjs_1.of)(0, scheduler).pipe((0, rxjs_1.expand)(function (iteration) { return (0, rxjs_1.timer)((0, utils_1.getDelay)(backoffDelay(iteration, initialInterval), maxInterval)) .pipe((0, rxjs_1.mapTo)(iteration + 1)); })); } exports.intervalBackoff = intervalBackoff;