Skip to content

Commit

Permalink
Fix: Remove check for isReadyForRequest, add isConnectedToInternet
Browse files Browse the repository at this point in the history
  • Loading branch information
romin-halltari committed Nov 28, 2023
1 parent 474b505 commit c2bdbd6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
11 changes: 2 additions & 9 deletions packages/@magic-sdk/provider/src/core/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ async function persistMagicEventRefreshToken(event: MagicMessageEvent) {
}

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

/**
* Create an instance of `ViewController`
Expand Down Expand Up @@ -132,13 +132,7 @@ export abstract class ViewController {
payload: JsonRpcRequestPayload | JsonRpcRequestPayload[],
): Promise<JsonRpcResponse<ResultType> | JsonRpcResponse<ResultType>[]> {
return createPromise(async (resolve, reject) => {
if (SDKEnvironment.platform !== 'react-native') {
await this.checkIsReadyForRequest;
} else if (!this.isReadyForRequest) {
// On a mobile environment, `this.checkIsReadyForRequest` never resolves
// if the app was initially opened without internet connection. That is
// why we reject the promise without waiting and just let them call it
// again when internet connection is re-established.
if (!this.isConnectedToInternet) {
const error = createModalNotReadyError();
reject(error);
}
Expand Down Expand Up @@ -207,7 +201,6 @@ export abstract class ViewController {
return new Promise<void>((resolve) => {
this.on(MagicIncomingWindowMessage.MAGIC_OVERLAY_READY, () => {
resolve();
this.isReadyForRequest = true;
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ test('Sends payload and stores rt if response event contains rt', async () => {
expect(FAKE_STORE.rt).toEqual(FAKE_RT);
});

test('does not wait for ready and throws error when platform is react-native', async () => {
SDKEnvironment.platform = 'react-native';
test('throws MODAL_NOT_READY error when not connected to the internet', async () => {
const eventWithRt = { data: { ...responseEvent().data } };
const viewController = createViewController('asdf');
const { handlerSpy, onSpy } = stubViewController(viewController, [
[MagicIncomingWindowMessage.MAGIC_HANDLE_RESPONSE, eventWithRt],
]);
viewController.checkIsReadyForRequest = new Promise(() => null);

// @ts-ignore protected variable
viewController.isConnectedToInternet = false;

const payload = requestPayload();

Expand All @@ -217,9 +218,6 @@ test('does not wait for ready and throws error when platform is react-native', a
} catch (e) {
expect(e).toEqual(createModalNotReadyError());
}
expect(createJwtStub).not.toHaveBeenCalledWith();
expect(onSpy.mock.calls[0][0]).toEqual(MagicIncomingWindowMessage.MAGIC_HANDLE_RESPONSE);
expect(handlerSpy).not.toHaveBeenCalled();
});

test('does not call web crypto api if platform is not web', async () => {
Expand All @@ -231,9 +229,6 @@ test('does not call web crypto api if platform is not web', async () => {
]);
const payload = requestPayload();

// @ts-ignore isReadyForRequest is private
viewController.isReadyForRequest = true;

const response = await viewController.post(MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST, payload);

expect(createJwtStub).not.toHaveBeenCalledWith();
Expand Down

0 comments on commit c2bdbd6

Please sign in to comment.