Lint connection status
This commit is contained in:
parent
b6d0416807
commit
149b8a1de5
1 changed files with 7 additions and 7 deletions
|
|
@ -6,8 +6,8 @@ import { sleep } from "../utils/sleep";
|
||||||
export class ConnectionStatus {
|
export class ConnectionStatus {
|
||||||
private static readonly UNTIL_RESOLUTION = Symbol();
|
private static readonly UNTIL_RESOLUTION = Symbol();
|
||||||
private canFetch = true;
|
private canFetch = true;
|
||||||
private until: Promise<Symbol>;
|
private until: Promise<symbol>;
|
||||||
private resolveUntil: (result: Symbol) => void;
|
private resolveUntil: (result: symbol) => void;
|
||||||
private rejectUntil: (reason: any) => void;
|
private rejectUntil: (reason: any) => void;
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
|
|
@ -15,14 +15,14 @@ export class ConnectionStatus {
|
||||||
private readonly logger: Logger
|
private readonly logger: Logger
|
||||||
) {
|
) {
|
||||||
[this.until, this.resolveUntil, this.rejectUntil] =
|
[this.until, this.resolveUntil, this.rejectUntil] =
|
||||||
createPromise<Symbol>();
|
createPromise<symbol>();
|
||||||
|
|
||||||
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
||||||
if (oldSettings.isSyncEnabled != newSettings.isSyncEnabled) {
|
if (oldSettings.isSyncEnabled != newSettings.isSyncEnabled) {
|
||||||
this.canFetch = newSettings.isSyncEnabled;
|
this.canFetch = newSettings.isSyncEnabled;
|
||||||
this.resolveUntil(ConnectionStatus.UNTIL_RESOLUTION);
|
this.resolveUntil(ConnectionStatus.UNTIL_RESOLUTION);
|
||||||
[this.until, this.resolveUntil, this.rejectUntil] =
|
[this.until, this.resolveUntil, this.rejectUntil] =
|
||||||
createPromise<Symbol>();
|
createPromise<symbol>();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -45,13 +45,13 @@ export class ConnectionStatus {
|
||||||
) {
|
) {
|
||||||
return async (input: RequestInfo | URL): Promise<Response> => {
|
return async (input: RequestInfo | URL): Promise<Response> => {
|
||||||
while (true) {
|
while (true) {
|
||||||
while (this.canFetch === false) {
|
while (!this.canFetch) {
|
||||||
await this.until;
|
await this.until;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// https://github.com/jonbern/fetch-retry/blob/8684ef4e688375f623bd76f13add76dbc1d67cfb/index.js#L67C1-L70C21
|
// https://github.com/jonbern/fetch-retry/blob/8684ef4e688375f623bd76f13add76dbc1d67cfb/index.js#L67C1-L70C21
|
||||||
let _input =
|
const _input =
|
||||||
typeof Request !== "undefined" &&
|
typeof Request !== "undefined" &&
|
||||||
input instanceof Request
|
input instanceof Request
|
||||||
? input.clone()
|
? input.clone()
|
||||||
|
|
@ -65,7 +65,7 @@ export class ConnectionStatus {
|
||||||
result = await Promise.race([this.until, fetchPromise]);
|
result = await Promise.race([this.until, fetchPromise]);
|
||||||
} while (result === ConnectionStatus.UNTIL_RESOLUTION);
|
} while (result === ConnectionStatus.UNTIL_RESOLUTION);
|
||||||
|
|
||||||
let fetchResult: Response = result as Response;
|
const fetchResult: Response = result as Response;
|
||||||
|
|
||||||
if (!fetchResult.ok) {
|
if (!fetchResult.ok) {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue