Skip to content

Commit

Permalink
feat(core): adding custom rxjs operator retryWithBackoff
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jul 3, 2019
1 parent d94e514 commit 37dffcf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
14 changes: 10 additions & 4 deletions libs/core/src/lib/services/profile.service.ts
Expand Up @@ -3,7 +3,8 @@ import { Injectable } from '@angular/core';
import { Profile } from '@ngx-starter-kit/models';
import { environment } from '@env/environment';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { EMPTY, throwError } from 'rxjs';
import { retryWithBackoff } from '@ngx-starter-kit/utils';

@Injectable({
providedIn: 'root',
Expand All @@ -15,9 +16,14 @@ export class ProfileService {
constructor(private httpClient: HttpClient) {}

getMyProfile() {
return this.httpClient
.get<Profile>(`${this.baseUrl}/${this.entityPath}/myprofile`)
.pipe(catchError(this.handleError));
return this.httpClient.get<Profile>(`${this.baseUrl}/${this.entityPath}/myprofile`).pipe(
retryWithBackoff(1000, 3),
catchError(error => {
this.handleError(error);
return EMPTY;
}),
// catchError(this.handleError)
);
}

create(entity: Partial<Profile>) {
Expand Down
1 change: 1 addition & 0 deletions libs/utils/src/index.ts
Expand Up @@ -3,6 +3,7 @@ export { waitUntil } from './lib/wait-until';
export { DeepPartial } from './lib/deep-partial';
export { ObjectLiteral } from './lib/object-literal';
export { delayAtLeast } from './lib/delay-at-least';
export { retryWithBackoff } from './lib/retry-with-backoff';
export { toClass } from './lib/to-class';
export { fromAsyncIterator } from './lib/from-async-iterator';
export * from './lib/require-multi';
31 changes: 31 additions & 0 deletions libs/utils/src/lib/retry-with-backoff.ts
@@ -0,0 +1,31 @@
import { Observable, of, throwError } from 'rxjs';
import { delay, mergeMap, retryWhen } from 'rxjs/operators';

const getErrorMessage = (maxRetry: number) =>
`Tried to load Resource over XHR for ${maxRetry} times without success. Giving up.`;

const DEFAULT_MAX_RETRIES = 5;
const DEFAULT_BACKOFF = 1000;

export function retryWithBackoff<T>(
delayMs: number,
maxRetry = DEFAULT_BACKOFF,
backoffMs = DEFAULT_BACKOFF,
): (src$: Observable<T>) => Observable<T> {
let retries = maxRetry;

return (src$: Observable<T>): Observable<T> =>
src$.pipe(
retryWhen((errors: Observable<T>) =>
errors.pipe(
mergeMap(error => {
if (retries-- > 0) {
const backoffTime = delayMs + (maxRetry - retries) * backoffMs;
return of(error).pipe(delay(backoffTime));
}
return throwError(getErrorMessage(maxRetry));
}),
),
),
);
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -77,7 +77,7 @@
},
"lint-staged": {
"{apps,libs}/**/*.{ts,json,md,scss,html}": [
"yarn affected:lint --uncommitted --parallel --fix",
"yarn affected:lint --uncommitted --parallel -- --fix",
"yarn format:write --uncommitted",
"git add"
]
Expand Down

0 comments on commit 37dffcf

Please sign in to comment.