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 / 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),
@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 / 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
Created May 23, 2018 03:13
Exponential backoff retry - example with fake service
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({
@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;
}
@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;
}