backoff-rxjs
Advanced tools
Comparing version 0.0.10 to 6.1.0
{ | ||
"name": "backoff-rxjs", | ||
"version": "0.0.10", | ||
"version": "6.1.0", | ||
"description": "A collection of helpful RxJS operators to deal with backoff strategies (like exponential backoff)", | ||
@@ -26,3 +26,3 @@ "main": "dist/index.js", | ||
"homepage": "https://github.com/alex-okrushko/backoff-rxjs#readme", | ||
"dependencies": { | ||
"peerDependencies": { | ||
"rxjs": "^6.1.0" | ||
@@ -29,0 +29,0 @@ }, |
# backoff-rxjs | ||
A collection of helpful RxJS operators to deal with backoff strategies (like exponential backoff) | ||
Angular-in-Depth article about this library is at https://blog.angularindepth.com/power-of-rxjs-when-using-exponential-backoff-a4b8bde276b0 | ||
## intervalBackoff | ||
![Basic interval backoff](./intervalBackoffBasic.svg) | ||
@@ -9,28 +12,33 @@ | ||
| name | type | attirbute | description | | ||
| ------------- |-------------| -----| ---------------| | ||
| config | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [IntervalBackoffConfig](https://github.com/alex-okrushko/backoff-rxjs/blob/bddb11d6d06d2d2ccdeb12e3c779bc3ae03311db/src/observable/intervalBackoff.ts#L6)| required |Can take number as initial interval or a config with initial interval, optional max Interval and optional backoff delay function (exponential by default) | | ||
| name | type | attirbute | description | | ||
| ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| config | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [IntervalBackoffConfig](https://github.com/alex-okrushko/backoff-rxjs/blob/bddb11d6d06d2d2ccdeb12e3c779bc3ae03311db/src/observable/intervalBackoff.ts#L6) | required | Can take number as initial interval or a config with initial interval, optional max Interval and optional backoff delay function (exponential by default) | | ||
`interval` is especially useful for periodic polls that are reset whenever user activity is detected: | ||
```ts | ||
fromEvent(document, 'mousemove').pipe( | ||
// There could be many mousemoves, we'd want to sample only | ||
// with certain frequency | ||
sampleTime(LOAD_INTERVAL_MS), | ||
// There could be many mousemoves, we'd want to sample only | ||
// with certain frequency | ||
sampleTime(LOAD_INTERVAL_MS), | ||
// Start immediately | ||
startWith(null), | ||
// Start immediately | ||
startWith(null), | ||
// Resetting exponential interval | ||
switchMapTo(intervalBackoff({initialInterval: LOAD_INTERVAL_MS, maxInterval: MAX_INTERVAL_MS})), | ||
); | ||
// Resetting exponential interval | ||
switchMapTo( | ||
intervalBackoff({ | ||
initialInterval: LOAD_INTERVAL_MS, | ||
maxInterval: MAX_INTERVAL_MS | ||
}) | ||
) | ||
); | ||
``` | ||
## retryBackoff | ||
## retryBackoff | ||
![Retry Backoff Exponential Image](./retryBackoff.svg) | ||
| name | type | attirbute | description | | ||
| ------------- |-------------| -----| ---------------| | ||
| config | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [RetryBackoffConfig](https://github.com/alex-okrushko/backoff-rxjs/blob/bddb11d6d06d2d2ccdeb12e3c779bc3ae03311db/src/operators/retryBackoff.ts#L6)| required |Can take number as initial interval or a config with initial interval, optional max Interval, optional max number of retry attempts, optional function to cancel reties and optional backoff delay function (exponential by default) | | ||
| name | type | attirbute | description | | ||
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| config | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [RetryBackoffConfig](https://github.com/alex-okrushko/backoff-rxjs/blob/bddb11d6d06d2d2ccdeb12e3c779bc3ae03311db/src/operators/retryBackoff.ts#L6) | required | Can take number as initial interval or a config with initial interval, optional max Interval, optional max number of retry attempts, optional function to cancel reties and optional backoff delay function (exponential by default) | |
@@ -1,5 +0,5 @@ | ||
import { Observable, of, timer, SchedulerLike, asyncScheduler } from "rxjs"; | ||
import { expand, mapTo } from "rxjs/operators"; | ||
import { Observable, of, timer, SchedulerLike, asyncScheduler } from 'rxjs'; | ||
import { expand, mapTo } from 'rxjs/operators'; | ||
import { exponentialBackoffDelay, getDelay } from "../utils"; | ||
import { exponentialBackoffDelay, getDelay } from '../utils'; | ||
@@ -24,3 +24,3 @@ export interface IntervalBackoffConfig { | ||
} = | ||
typeof config === "number" ? { initialInterval: config } : config; | ||
typeof config === 'number' ? { initialInterval: config } : config; | ||
initialInterval = (initialInterval < 0) ? 0 : initialInterval; | ||
@@ -27,0 +27,0 @@ return of(0, scheduler).pipe( |
@@ -1,10 +0,14 @@ | ||
import {iif, Observable, throwError, timer} from 'rxjs'; | ||
import {concatMap, retryWhen} from 'rxjs/operators'; | ||
import { iif, Observable, throwError, timer } from 'rxjs'; | ||
import { concatMap, retryWhen } from 'rxjs/operators'; | ||
import {getDelay, exponentialBackoffDelay} from '../utils'; | ||
import { getDelay, exponentialBackoffDelay } from '../utils'; | ||
export interface RetryBackoffConfig { | ||
// Initial interval. It will eventually go as high as maxInterval. | ||
initialInterval: number; | ||
maxAttempts?: number; | ||
// Maximum number of retry attempts. | ||
maxRetries?: number; | ||
// Maximum delay between retries. | ||
maxInterval?: number; | ||
// Conditional retry. | ||
shouldRetry?: (error: any) => boolean; | ||
@@ -23,21 +27,25 @@ backoffDelay?: (iteration: number, initialInterval: number) => number; | ||
export function retryBackoff( | ||
config: number|RetryBackoffConfig): | ||
<T>(source: Observable<T>) => Observable<T> { | ||
config: number | RetryBackoffConfig | ||
): <T>(source: Observable<T>) => Observable<T> { | ||
const { | ||
initialInterval, | ||
maxAttempts = Infinity, | ||
maxRetries = Infinity, | ||
maxInterval = Infinity, | ||
shouldRetry = () => true, | ||
backoffDelay = exponentialBackoffDelay, | ||
} = (typeof config === 'number') ? {initialInterval: config} : config; | ||
return <T>(source: Observable<T>) => source.pipe( | ||
retryWhen<T>(errors => errors.pipe( | ||
concatMap((error, i) => iif( | ||
() => i < maxAttempts && shouldRetry(error), | ||
backoffDelay = exponentialBackoffDelay | ||
} = typeof config === 'number' ? { initialInterval: config } : config; | ||
return <T>(source: Observable<T>) => | ||
source.pipe( | ||
retryWhen<T>(errors => | ||
errors.pipe( | ||
concatMap((error, i) => | ||
iif( | ||
() => i < maxRetries && shouldRetry(error), | ||
timer(getDelay(backoffDelay(i, initialInterval), maxInterval)), | ||
throwError(error), | ||
), | ||
), | ||
)), | ||
); | ||
} | ||
throwError(error) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
55673
22
530
0
44
- Removedrxjs@^6.1.0