diff --git a/packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityNotificationSettings.ts b/packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityNotificationSettings.ts index d304ae9ebcf..11fd0f3d2cf 100644 --- a/packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityNotificationSettings.ts +++ b/packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityNotificationSettings.ts @@ -62,9 +62,9 @@ export class SimpleDbEntityNotificationSettings extends SimpleDbEntityBase { - if (wallet.enabled) { - Object.values(wallet.accounts || {}).forEach((account) => { - if (account.enabled) { + if (wallet?.enabled) { + Object.values(wallet?.accounts || {}).forEach((account) => { + if (account?.enabled) { count += 1; } }); diff --git a/packages/kit-bg/src/services/ServiceNetwork/ServiceNetwork.ts b/packages/kit-bg/src/services/ServiceNetwork/ServiceNetwork.ts index 0dbb3d25909..5e208d90bca 100644 --- a/packages/kit-bg/src/services/ServiceNetwork/ServiceNetwork.ts +++ b/packages/kit-bg/src/services/ServiceNetwork/ServiceNetwork.ts @@ -14,6 +14,9 @@ import { appLocale } from '@onekeyhq/shared/src/locale/appLocale'; import accountUtils from '@onekeyhq/shared/src/utils/accountUtils'; import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils'; import networkUtils from '@onekeyhq/shared/src/utils/networkUtils'; +import perfUtils, { + EPerformanceTimerLogNames, +} from '@onekeyhq/shared/src/utils/perfUtils'; import type { IServerNetwork } from '@onekeyhq/shared/types'; import { vaultFactory } from '../../vaults/factory'; @@ -57,6 +60,11 @@ class ServiceNetwork extends ServiceBase { uniqByImpl?: boolean; } = {}, ): Promise<{ networks: IServerNetwork[] }> { + const perf = perfUtils.createPerf( + EPerformanceTimerLogNames.serviceNetwork__getAllNetworks, + ); + + perf.markStart('getPresetNetworks'); // TODO save to simpleDB const excludeTestNetwork = params?.excludeTestNetwork ?? false; const uniqByImpl = params?.uniqByImpl ?? false; @@ -64,14 +72,16 @@ class ServiceNetwork extends ServiceBase { if (params.excludeAllNetworkItem) { excludeNetworkIds.push(getNetworkIdsMap().onekeyall); } - const presetNetworks = getPresetNetworks(); + perf.markEnd('getPresetNetworks'); + perf.markStart('getServerNetworks-and-getAllCustomNetworks'); // Fetch server and custom networks const [serverNetworks, customNetworks] = await Promise.all([ this.backgroundApi.serviceCustomRpc.getServerNetworks(), this.backgroundApi.serviceCustomRpc.getAllCustomNetworks(), ]); + perf.markEnd('getServerNetworks-and-getAllCustomNetworks'); // Create a Map to store unique networks by id // Priority: serverNetworks > presetNetworks > customNetworks @@ -86,25 +96,50 @@ class ServiceNetwork extends ServiceBase { }); }; + perf.markStart('addNetworks-presetNetworks'); // Add networks in order of priority addNetworks(presetNetworks); + perf.markEnd('addNetworks-presetNetworks'); + + perf.markStart('addNetworks-serverNetworks'); addNetworks(serverNetworks); + perf.markEnd('addNetworks-serverNetworks'); + + perf.markStart('addNetworks-customNetworks'); addNetworks(customNetworks); + perf.markEnd('addNetworks-customNetworks'); + perf.markStart('convertMapToArray'); // Convert Map back to array let networks = Array.from(networkMap.values()); + perf.markEnd('convertMapToArray'); + + perf.markStart('filterNetworks-excludeCustomNetwork'); if (params.excludeCustomNetwork) { excludeNetworkIds.push(...customNetworks.map((n) => n.id)); } + perf.markEnd('filterNetworks-excludeCustomNetwork'); + + perf.markStart('filterNetworks-uniqByImpl'); if (uniqByImpl) { networks = uniqBy(networks, (n) => n.impl); } + perf.markEnd('filterNetworks-uniqByImpl'); + + perf.markStart('filterNetworks-excludeTestNetwork'); if (excludeTestNetwork) { networks = networks.filter((n) => !n.isTestnet); } + perf.markEnd('filterNetworks-excludeTestNetwork'); + + perf.markStart('filterNetworks-excludeNetworkIds'); if (excludeNetworkIds?.length) { networks = networks.filter((n) => !excludeNetworkIds.includes(n.id)); } + perf.markEnd('filterNetworks-excludeNetworkIds'); + + perf.done(); + return Promise.resolve({ networks }); } diff --git a/packages/kit-bg/src/services/ServiceNotification/ServiceNotification.ts b/packages/kit-bg/src/services/ServiceNotification/ServiceNotification.ts index 8d85bcf751d..1aa41e971a6 100644 --- a/packages/kit-bg/src/services/ServiceNotification/ServiceNotification.ts +++ b/packages/kit-bg/src/services/ServiceNotification/ServiceNotification.ts @@ -637,6 +637,10 @@ export default class ServiceNotification extends ServiceBase { const { wallets } = await this.getNotificationWalletsAndAccounts(); const oldAccountActivity = cloneDeep(settings?.accountActivity ?? {}); const accountActivity: IAccountActivityNotificationSettings = {}; + + const currentEnabledAccountCount = + await this.backgroundApi.simpleDb.notificationSettings.getEnabledAccountCount(); + let totalEnabledCount = 0; const isInit = !settings?.accountActivity; const updateWalletAccountActivity = (wallet: IDBWallet) => { @@ -674,14 +678,22 @@ export default class ServiceNotification extends ServiceBase { const isWalletEnabled = oldAccountActivity?.[wallet.id]?.enabled === true || oldAccountActivity?.[wallet.id]?.enabled === undefined; - const isAccountEnabled = + const isAccountEnabledUndefined = oldAccountActivity?.[wallet.id]?.accounts?.[account.id]?.enabled === - true || + undefined; + const isAccountEnabled = oldAccountActivity?.[wallet.id]?.accounts?.[account.id]?.enabled === - undefined; + true || isAccountEnabledUndefined; if (isWalletEnabled && isAccountEnabled) { - enableAccount(account); + if ( + isAccountEnabledUndefined && + currentEnabledAccountCount >= maxAccountCount + ) { + disableAccount(account); + } else { + enableAccount(account); + } } else { disableAccount(account); } diff --git a/packages/shared/src/utils/perfUtils.ts b/packages/shared/src/utils/perfUtils.ts index 1f30bf31202..8c5ec1f484b 100644 --- a/packages/shared/src/utils/perfUtils.ts +++ b/packages/shared/src/utils/perfUtils.ts @@ -16,6 +16,7 @@ export enum EPerformanceTimerLogNames { allNetwork__getAccountLocalTokens = 'allNetwork__getAccountLocalTokens', allNetwork__useAllNetworkRequests = 'allNetwork__useAllNetworkRequests', allNetwork__handleAllNetworkCacheRequests = 'allNetwork__handleAllNetworkCacheRequests', + serviceNetwork__getAllNetworks = 'serviceNetwork__getAllNetworks', } function getPerformanceTimerLogConfigMap() {