Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple network setups for React Native SDKs #687

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -114,6 +114,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 @@ -132,6 +133,7 @@ export abstract class ViewController {
protected readonly networkHash: string,
) {
this.checkIsReadyForRequest = this.waitForReady();
this.isReadyForRequest = false;
this.listen();
}

Expand Down Expand Up @@ -167,7 +169,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 @@ -236,8 +240,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
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export class ReactNativeWebViewController extends ViewController {
this.isConnectedToInternet = isConnected;
}, [isConnected]);

useEffect(() => {
return () => {
this.isReadyForRequest = false;
};
}, []);

/**
* Saves a reference to the underlying `<WebView>` node so we can interact
* with incoming messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export class ReactNativeWebViewController extends ViewController {
this.isConnectedToInternet = isConnected;
}, [isConnected]);

useEffect(() => {
return () => {
this.isReadyForRequest = false;
};
}, []);

/**
* Saves a reference to the underlying `<WebView>` node so we can interact
* with incoming messages.
Expand Down
Loading