This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface IntervalBackoffConfig { | |
initialInterval: number; | |
maxInterval?: number; | |
backoffDelay?: (iteration: number, initialInterval: number) => number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface RetryBackoffConfig { | |
initialInterval: number; | |
maxRetries?: number; | |
maxInterval?: number; | |
shouldRetry?: (error: any) => boolean; | |
backoffDelay?: (iteration: number, initialInterval: number) => number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { of } from 'rxjs'; | |
import { tap, switchMap} from 'rxjs/operators'; | |
import { retryBackoff } from 'backoff-rxjs'; | |
import { BackendService, HttpError } from './backend.service'; | |
export const INIT_INTERVAL_MS = 100; // 100 ms | |
export const MAX_INTERVAL_MS = 20 * 1000; // 20 sec | |
@Component({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { of, throwError, defer } from 'rxjs'; | |
export interface HttpError { | |
error: string, | |
status: string, | |
} | |
const ERROR_404: HttpError = { error: 'No need to try anymore', status: '404' }; | |
const ERROR_503: HttpError = { error: 'Try again?', status: '503' }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Determine if the error matches our expected type | |
// http://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards | |
function isHttpError(error: {}): error is HttpError { | |
// This is a type guard for interface | |
// if HttpError was a class we would use instanceof check instead | |
return (error as HttpError).status !== undefined; | |
} | |
message$ = of('Call me!').pipe( | |
tap(console.log), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {fromEvent} from 'rxjs'; | |
import {sampleTime, startWith, switchMap} from 'rxjs/operators'; | |
import {intervalBackoff} from 'backoff-rxjs'; | |
import {service} from './service'; | |
const newData$ = fromEvent(document, 'mousemove').pipe( | |
// There could be many mousemoves, we'd want to sample only | |
// with certain frequency | |
sampleTime(1000), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message$ = of('Call me!').pipe( | |
switchMap(() => this.service.callBackend()), | |
retryBackoff(1000), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateDelay(iteration, initialInterval) { | |
return Math.pow(2, iteration) * initialInterval; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message$ = of('Call me!').pipe( | |
switchMap(() => this.service.callBackend()), | |
retryBackoff({ | |
initialInterval: 100, | |
maxRetries: 12, | |
}), | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("should handle a basic source that emits next then errors, count=3", () => { | |
testScheduler.run(({ expectObservable, cold, expectSubscriptions }) => { | |
const source = cold("--1-2-3-#"); | |
const subs = [ "^ ! ", | |
" ^ ! ", | |
" ^ !" | |
]; | |
const expected = "--1-2-3----1-2-3-----1-2-3-#"; | |
expectObservable( |
OlderNewer