From 152777d3e6bd80479cf921ed8aebd8e75fcac6ca Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 14:03:22 +0200 Subject: [PATCH 1/3] preven dashboard going into a loading state when the portfolio is automatically updated in the background --- src/controllers/main/main.ts | 6 +++++- src/controllers/selectedAccount/selectedAccount.ts | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index e15a97273..a0b2dd281 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -95,7 +95,10 @@ import { NetworksController } from '../networks/networks' import { PortfolioController } from '../portfolio/portfolio' import { ProvidersController } from '../providers/providers' /* eslint-disable @typescript-eslint/no-floating-promises */ -import { SelectedAccountController } from '../selectedAccount/selectedAccount' +import { + DEFAULT_SELECTED_ACCOUNT_PORTFOLIO, + SelectedAccountController +} from '../selectedAccount/selectedAccount' /* eslint-disable no-underscore-dangle */ import { SignAccountOpController, SigningStatus } from '../signAccountOp/signAccountOp' import { SignMessageController } from '../signMessage/signMessage' @@ -902,6 +905,7 @@ export class MainController extends EventEmitter { const isUpdatingAccount = this.accounts.statuses.updateAccountState !== 'INITIAL' + this.selectedAccount.portfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO await Promise.all([ // When we trigger `reloadSelectedAccount` (for instance, from Dashboard -> Refresh balance icon), // it's very likely that the account state is already in the process of being updated. diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index 787058cb7..a4861c6de 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -17,7 +17,7 @@ import EventEmitter from '../eventEmitter/eventEmitter' // eslint-disable-next-line import/no-cycle import { PortfolioController } from '../portfolio/portfolio' -const DEFAULT_SELECTED_ACCOUNT_PORTFOLIO = { +export const DEFAULT_SELECTED_ACCOUNT_PORTFOLIO = { tokens: [], collections: [], totalBalance: 0, @@ -171,8 +171,6 @@ export class SelectedAccountController extends EventEmitter { (!this.portfolio?.tokens?.length && newSelectedAccountPortfolio.tokens.length) ) { this.portfolio = newSelectedAccountPortfolio - } else { - this.portfolio.isAllReady = false } if (!skipUpdate) { From 610202698798bc6a134a1efc77de964443e598a0 Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 15:07:13 +0200 Subject: [PATCH 2/3] add: resetSelectedAccountPortfolio method --- src/controllers/main/main.ts | 7 ++----- .../selectedAccount/selectedAccount.test.ts | 6 +++++- src/controllers/selectedAccount/selectedAccount.ts | 10 +++++++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/controllers/main/main.ts b/src/controllers/main/main.ts index a0b2dd281..b4d89d85f 100644 --- a/src/controllers/main/main.ts +++ b/src/controllers/main/main.ts @@ -95,10 +95,7 @@ import { NetworksController } from '../networks/networks' import { PortfolioController } from '../portfolio/portfolio' import { ProvidersController } from '../providers/providers' /* eslint-disable @typescript-eslint/no-floating-promises */ -import { - DEFAULT_SELECTED_ACCOUNT_PORTFOLIO, - SelectedAccountController -} from '../selectedAccount/selectedAccount' +import { SelectedAccountController } from '../selectedAccount/selectedAccount' /* eslint-disable no-underscore-dangle */ import { SignAccountOpController, SigningStatus } from '../signAccountOp/signAccountOp' import { SignMessageController } from '../signMessage/signMessage' @@ -905,7 +902,7 @@ export class MainController extends EventEmitter { const isUpdatingAccount = this.accounts.statuses.updateAccountState !== 'INITIAL' - this.selectedAccount.portfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO + this.selectedAccount.resetSelectedAccountPortfolio() await Promise.all([ // When we trigger `reloadSelectedAccount` (for instance, from Dashboard -> Refresh balance icon), // it's very likely that the account state is already in the process of being updated. diff --git a/src/controllers/selectedAccount/selectedAccount.test.ts b/src/controllers/selectedAccount/selectedAccount.test.ts index 095ef023e..1c86e80ee 100644 --- a/src/controllers/selectedAccount/selectedAccount.test.ts +++ b/src/controllers/selectedAccount/selectedAccount.test.ts @@ -16,7 +16,7 @@ import { DefiPositionsController } from '../defiPositions/defiPositions' import { NetworksController } from '../networks/networks' import { PortfolioController } from '../portfolio/portfolio' import { ProvidersController } from '../providers/providers' -import { SelectedAccountController } from './selectedAccount' +import { DEFAULT_SELECTED_ACCOUNT_PORTFOLIO, SelectedAccountController } from './selectedAccount' // @ts-ignore global.structuredClone = structuredClone as any @@ -152,6 +152,10 @@ describe('SelectedAccount Controller', () => { }) portfolioCtrl.updateSelectedAccount('0x77777777789A8BBEE6C64381e5E89E501fb0e4c8') }) + test('should rest selected account portfolio', (done) => { + selectedAccountCtrl.resetSelectedAccountPortfolio() + expect(selectedAccountCtrl.portfolio).toEqual(DEFAULT_SELECTED_ACCOUNT_PORTFOLIO) + }) test('should toJSON()', () => { const json = selectedAccountCtrl.toJSON() expect(json).toBeDefined() diff --git a/src/controllers/selectedAccount/selectedAccount.ts b/src/controllers/selectedAccount/selectedAccount.ts index a4861c6de..a70f23a47 100644 --- a/src/controllers/selectedAccount/selectedAccount.ts +++ b/src/controllers/selectedAccount/selectedAccount.ts @@ -123,7 +123,7 @@ export class SelectedAccountController extends EventEmitter { async setAccount(account: Account | null) { this.account = account - this.portfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO + this.resetSelectedAccountPortfolio(true) if (!account) { await this.#storage.remove('selectedAccount') @@ -134,6 +134,14 @@ export class SelectedAccountController extends EventEmitter { this.emitUpdate() } + resetSelectedAccountPortfolio(skipUpdate?: boolean) { + this.portfolio = DEFAULT_SELECTED_ACCOUNT_PORTFOLIO + + if (!skipUpdate) { + this.emitUpdate() + } + } + #updateSelectedAccountPortfolio(skipUpdate?: boolean) { if (!this.#portfolio || !this.#defiPositions || !this.account) return From 01f846e9d17ced2b2daa971c903099d14aa1a12e Mon Sep 17 00:00:00 2001 From: sonytooo Date: Fri, 15 Nov 2024 15:19:54 +0200 Subject: [PATCH 3/3] fix test --- src/controllers/selectedAccount/selectedAccount.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/selectedAccount/selectedAccount.test.ts b/src/controllers/selectedAccount/selectedAccount.test.ts index 1c86e80ee..29c21a2bd 100644 --- a/src/controllers/selectedAccount/selectedAccount.test.ts +++ b/src/controllers/selectedAccount/selectedAccount.test.ts @@ -152,7 +152,7 @@ describe('SelectedAccount Controller', () => { }) portfolioCtrl.updateSelectedAccount('0x77777777789A8BBEE6C64381e5E89E501fb0e4c8') }) - test('should rest selected account portfolio', (done) => { + test('should rest selected account portfolio', () => { selectedAccountCtrl.resetSelectedAccountPortfolio() expect(selectedAccountCtrl.portfolio).toEqual(DEFAULT_SELECTED_ACCOUNT_PORTFOLIO) })