Skip to content

Commit

Permalink
Fix: waitForReady() if the controller is not ready for requests, inst…
Browse files Browse the repository at this point in the history
…ead of using checkIsReadyForRequest when isReadyForRequest is false
  • Loading branch information
romin-halltari committed Dec 15, 2023
1 parent f5920ab commit 605cf3e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/@magic-sdk/provider/src/core/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ async function persistMagicEventRefreshToken(event: MagicMessageEvent) {

export abstract class ViewController {
public checkIsReadyForRequest: Promise<void>;
public isReadyForRequest: boolean;
protected readonly messageHandlers = new Set<(event: MagicMessageEvent) => any>();
protected isConnectedToInternet = true;

Expand All @@ -102,6 +103,7 @@ export abstract class ViewController {
*/
constructor(protected readonly endpoint: string, protected readonly parameters: string) {
this.checkIsReadyForRequest = this.waitForReady();
this.isReadyForRequest = false;
this.listen();
}

Expand Down Expand Up @@ -137,7 +139,9 @@ export abstract class ViewController {
reject(error);
}

await this.checkIsReadyForRequest;
if (!this.isReadyForRequest) {
await this.waitForReady();
}

const batchData: JsonRpcResponse[] = [];
const batchIds = Array.isArray(payload) ? payload.map((p) => p.id) : [];
Expand Down Expand Up @@ -201,8 +205,10 @@ export abstract class ViewController {

private waitForReady() {
return new Promise<void>((resolve) => {
this.on(MagicIncomingWindowMessage.MAGIC_OVERLAY_READY, () => {
const unsubscribe = this.on(MagicIncomingWindowMessage.MAGIC_OVERLAY_READY, () => {
this.isReadyForRequest = true;
resolve();
unsubscribe();
});
});
}
Expand Down

0 comments on commit 605cf3e

Please sign in to comment.