Skip to content

Instantly share code, notes, and snippets.

View alex-okrushko's full-sized avatar

Alex Okrushko alex-okrushko

View GitHub Profile
@alex-okrushko
alex-okrushko / IntervalBackoffConfig.ts
Last active May 23, 2018 01:58
IntervalBackoffConfig
export interface IntervalBackoffConfig {
initialInterval: number;
maxInterval?: number;
backoffDelay?: (iteration: number, initialInterval: number) => number;
}
@alex-okrushko
alex-okrushko / backend.service.ts
Last active May 23, 2018 03:23
Exponential backoff retry - fake service
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' };
@alex-okrushko
alex-okrushko / app.component.ts
Last active May 28, 2018 21:34
Exponential backoff retry - example with fake service
// 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),
@alex-okrushko
alex-okrushko / calculate_delay.ts
Created May 29, 2018 22:14
example of a function that calculated delay for each iteration in exponential backoff
function calculateDelay(iteration, initialInterval) {
 return Math.pow(2, iteration) * initialInterval;
}
@alex-okrushko
alex-okrushko / retry_backoff_simple.ts
Last active May 30, 2018 13:27
simple example of exponential backoff usage
message$ = of('Call me!').pipe(
switchMap(() => this.service.callBackend()),
retryBackoff(1000),
);
@alex-okrushko
alex-okrushko / intervalBackoffUseCase.ts
Last active August 2, 2018 16:07
example of intervalBackoff that is reset on mousemove.
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),
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(
@alex-okrushko
alex-okrushko / retry_backoff_config.ts
Last active October 2, 2018 18:24
RetryBackoffConfig used in retryBackoff - exponential backoff retry for pipeable operators
export interface RetryBackoffConfig {
initialInterval: number;
maxRetries?: number;
maxInterval?: number;
shouldRetry?: (error: any) => boolean;
backoffDelay?: (iteration: number, initialInterval: number) => number;
}
message$ = of('Call me!').pipe(
switchMap(() => this.service.callBackend()),
retryBackoff({
initialInterval: 100,
maxRetries: 12,
}),
);
@alex-okrushko
alex-okrushko / basic_effects.ts
Created December 6, 2018 03:39
most basic API call
@Effect()
fetchProducts: Observable<Action> = this.actions$.pipe(
ofType<actions.FetchProducts>(actions.FETCH_PRODUCTS),
switchMap(() =>
this.productService.getProducts().pipe(
map(products => new actions.FetchProductsSuccess(products)),
catchError(() => of(new actions.FetchProductsError()))
)
)
);