Skip to content

Commit

Permalink
feat: prioritize resume over deeplink to accelerate reconnection flow
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s committed Jul 13, 2023
1 parent 151b54e commit 55aa18e
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions app/core/SDKConnect/SDKConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ export const METHODS_TO_REDIRECT: { [method: string]: boolean } = {
wallet_switchEthereumChain: true,
};

let wentBackMinimizer = false;
export const METHODS_TO_DELAY: { [method: string]: boolean } = {
...METHODS_TO_REDIRECT,
eth_requestAccounts: false,
};

// eslint-disable-next-line
const { version } = require('../../../package.json');
Expand Down Expand Up @@ -392,8 +395,6 @@ export class Connection extends EventEmitter2 {
}

let needsRedirect = METHODS_TO_REDIRECT[message?.method] ?? false;
// reset wentBack state to allow Minimizer.goBack()
wentBackMinimizer = false;

if (needsRedirect) {
this.requestsToRedirect[message?.id] = true;
Expand Down Expand Up @@ -739,13 +740,11 @@ export class Connection extends EventEmitter2 {
// Queue might not be empty if it timedout ---Always force clear before to go back
this.rpcQueueManager.reset();

// Prevent double back issue android. (it seems that the app goes back randomly by itself)
if (wentBackMinimizer) {
// Skip, already went back.
return;
if (METHODS_TO_DELAY[rpcMethod]) {
// Delay goback in order to display the feedback modal.
await wait(1000);
}

this.setLoading(false);
Minimizer.goBack();
})
.catch((err) => {
Expand Down Expand Up @@ -790,9 +789,15 @@ export class SDKConnect extends EventEmitter2 {
origin,
}: ConnectionProps) {
const existingConnection = this.connected[id] !== undefined;
const isReady = existingConnection && this.connected[id].isReady;

if (isReady) {
// Nothing to do, already connected.
return;
}

Logger.log(
`SDKConnect::connectToChannel - paused=${this.paused} existingConnection=${existingConnection} connecting to channel ${id} from '${origin}'`,
`SDKConnect::connectToChannel - paused=${this.paused} existingConnection=${existingConnection} isReady=${isReady} connecting to channel ${id} from '${origin}'`,
otherPublicKey,
);

Expand Down Expand Up @@ -1082,6 +1087,9 @@ export class SDKConnect extends EventEmitter2 {
}
this.paused = true;
this.connecting = {};

// Cancel rpc queue anytime app is backgrounded
this.rpcqueueManager.reset();
}

/**
Expand Down Expand Up @@ -1225,23 +1233,12 @@ export class SDKConnect extends EventEmitter2 {
this.timeout = undefined;

if (this.paused) {
const keyringController = (
Engine.context as { KeyringController: KeyringController }
).KeyringController;
this.reconnected = false;
await waitForKeychainUnlocked({ keyringController });
// Add delay in case app opened from deeplink so that it doesn't create 2 connections.
await wait(1000);
for (const id in this.connected) {
this.resume({ channelId: id });
}
}
this.paused = false;
} else if (appState === 'background') {
// Reset wentBack state
wentBackMinimizer = true;
// Cancel rpc queue anytime app is backgrounded
this.rpcqueueManager.reset();
if (!this.paused) {
/**
* Pause connections after 20 seconds of the app being in background to respect device resources.
Expand Down

0 comments on commit 55aa18e

Please sign in to comment.