libs/retry-loader/src/faulty.ts
Properties |
errorProbability |
errorProbability:
|
Type : number
|
Optional |
Defined in libs/retry-loader/src/faulty.ts:14
|
maxDelayMs |
maxDelayMs:
|
Type : number
|
Optional |
Defined in libs/retry-loader/src/faulty.ts:15
|
import { Observable } from 'rxjs/Observable';
import { defer } from 'rxjs/observable/defer';
import { _throw } from 'rxjs/observable/throw';
import { timer } from 'rxjs/observable/timer';
import { flatMap } from 'rxjs/operators';
export interface IFaultyOptions {
errorProbability?: number;
maxDelayMs?: number;
}
const DEFAULT_OPTIONS = {
errorProbability: 0.3,
maxDelayMs: 1000
};
export function faulty<T>(options?: IFaultyOptions): (source: Observable<T>) => Observable<T> {
options = Object.assign({}, DEFAULT_OPTIONS, options);
return (source) => defer<T>(() => {
return timer(Math.random() * options.maxDelayMs)
.pipe(flatMap(_value =>
(Math.random() < options.errorProbability) ?
_throw(new Error('Failed in faulty')) :
source));
});
}