Skip to content

Commit

Permalink
Fix intendations
Browse files Browse the repository at this point in the history
  • Loading branch information
Caliman-Nicolae committed Apr 13, 2020
1 parent bc449ff commit 6e50717
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
33 changes: 17 additions & 16 deletions lib/retry/Retryer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { WaitStrategy } from './WaitStrategy';

export class Retryer {
private attempts: number = 0;
private retryOptions: RetryOptions = DEFAULT_OPTIONS;
private readonly retryOptions: RetryOptions = { ...DEFAULT_OPTIONS, ...this.options };

constructor(
private readonly options: RetryOptions,
private readonly method: any,
private readonly instance: any,
private readonly retryCount: number,
) {
this.attempts = (!retryCount || retryCount < 0) ? 0 : retryCount;
this.retryOptions = { ...this.retryOptions, ...options };
this.attempts = (!this.retryCount || this.retryCount < 0) ? 0 : this.retryCount;
}

public getResponse(): any | Promise<any> {
Expand All @@ -25,7 +24,7 @@ export class Retryer {
} catch (err) {
const isFiltered = this.retryOptions.errorFilter.bind(this.instance)(err);

return !isFiltered ? this.error() : this.retryGetSyncResponse();
return isFiltered ? this.retryGetSyncResponse() : this.error();
}
}

Expand All @@ -34,9 +33,9 @@ export class Retryer {
try {
return this.method();
} catch (err) {
const isFiltered = this.retryOptions.errorFilter.bind(this.instance)(err);
const filteredError = this.retryOptions.errorFilter.bind(this.instance)(err);

if (!isFiltered) {
if (!filteredError) {
return this.error();
}
}
Expand All @@ -45,22 +44,16 @@ export class Retryer {
return this.error();
}

// tslint:disable-next-line:max-line-length
private async getAsyncResponse(asyncResponse: any): Promise<any> {
for (let index = 0; index <= this.attempts; index += 1) {
if (index > 0) {
const waitStrategy = new WaitStrategy(this.retryOptions.waitPattern);
await waitStrategy.wait(index - 1);
}
await this.waitBeforeResponse(index);

try {
const response = index === 0 ? await asyncResponse : await this.method();

return response;
return index === 0 ? await asyncResponse : await this.method();
} catch (err) {
const isFiltered = this.retryOptions.errorFilter.bind(this.instance)(err);
const filteredError = this.retryOptions.errorFilter.bind(this.instance)(err);

if (!isFiltered) {
if (!filteredError) {
return this.error();
}
}
Expand All @@ -69,8 +62,16 @@ export class Retryer {
return this.error();
}

private async waitBeforeResponse(attemptIndex: number): Promise<void> {
if (attemptIndex > 0) {
const waitStrategy = new WaitStrategy(this.retryOptions.waitPattern);
await waitStrategy.wait(attemptIndex - 1);
}
}

private error() {
const raise = raiseStrategy(this.retryOptions);

return raise(new Error(DEFAULT_ERROR));
}
}
2 changes: 2 additions & 0 deletions lib/retry/WaitStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class WaitStrategy {
if (Array.isArray(this.waitPattern)) {
const values = this.waitPattern as number[];
const count = values.length;

return index > count ? values[count - 1] : values[index];
}

Expand All @@ -32,4 +33,5 @@ export class WaitStrategy {

throw new Error(`Option ${typeof this.waitPattern} is not supported for 'waitPattern'.`);
}

}
2 changes: 1 addition & 1 deletion lib/retry/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Retryer } from './Retryer';
import { DEFAULT_OPTIONS, RetryOptions } from './RetryOptions';
import { RetryOptions } from './RetryOptions';

export { RetryOptions };

Expand Down

0 comments on commit 6e50717

Please sign in to comment.