From 55aa18e9263071bb14b1d46ec6fd1a6c6edfdb53 Mon Sep 17 00:00:00 2001 From: abretonc7s Date: Thu, 13 Jul 2023 13:36:08 +0800 Subject: [PATCH] feat: prioritize resume over deeplink to accelerate reconnection flow --- app/core/SDKConnect/SDKConnect.ts | 37 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/app/core/SDKConnect/SDKConnect.ts b/app/core/SDKConnect/SDKConnect.ts index b7ba6da14bc5..038e61c5c383 100644 --- a/app/core/SDKConnect/SDKConnect.ts +++ b/app/core/SDKConnect/SDKConnect.ts @@ -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'); @@ -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; @@ -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) => { @@ -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, ); @@ -1082,6 +1087,9 @@ export class SDKConnect extends EventEmitter2 { } this.paused = true; this.connecting = {}; + + // Cancel rpc queue anytime app is backgrounded + this.rpcqueueManager.reset(); } /** @@ -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.