Skip to content

Commit

Permalink
fix(route): await last status resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike committed Jun 25, 2022
1 parent cd4a6ea commit 639e70e
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/WS/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ export default class WSClient {
/**
* Promise of the current status switching.
*/
private statusPromise: null | Promise<void> = null;
private statusPromise = Promise.resolve();

/**
* Resolve status at a later message.
*/
private statusResolve: null | ((status: 'on' | 'off') => void) = null;

private async toggleServerRoute(status: 'on' | 'off') {
if (this.statusPromise !== null) {
await this.statusPromise;
return;
}
const { statusPromise } = this;
this.statusPromise = (async () => {
await statusPromise.catch(() => {});
const response = new Promise<'on' | 'off'>((resolve) => {
this.statusResolve = resolve;
});
Expand All @@ -78,7 +76,7 @@ export default class WSClient {
throw new Error(`Server failed to switch ${status}`);
}
})();
await this.statusPromise;
return this.statusPromise;
}

bypassFetch(...fetchArgs: Parameters<typeof fetch>) {
Expand Down Expand Up @@ -116,7 +114,6 @@ export default class WSClient {
}
this.statusResolve(meta.to);
this.statusResolve = null;
this.statusPromise = null;
return;
}
let body: Blob | undefined;
Expand Down

0 comments on commit 639e70e

Please sign in to comment.