File

libs/retry-loader/src/faulty.ts

Index

Properties

Properties

errorProbability
errorProbability: number
Type : number
Optional
maxDelayMs
maxDelayMs: number
Type : number
Optional
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));
  });
}

results matching ""

    No results matching ""