From caf756d23557d7cb48f3c1113b86125854c71355 Mon Sep 17 00:00:00 2001 From: hyphenized <28708889+hyphenized@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:21:23 -0500 Subject: [PATCH] Batch assets emitted from indexing --- background/services/indexing/index.ts | 41 +++++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/background/services/indexing/index.ts b/background/services/indexing/index.ts index dbfccc02bf..bc129ea65e 100644 --- a/background/services/indexing/index.ts +++ b/background/services/indexing/index.ts @@ -52,6 +52,7 @@ import { isUnverifiedAsset, isTrustedAsset, } from "../../redux-slices/utils/asset-utils" +import { isFulfilledPromise } from "../../lib/utils/type-guards" // Transactions seen within this many blocks of the chain tip will schedule a // token refresh sooner than the standard rate. @@ -208,14 +209,22 @@ export default class IndexingService extends BaseService { const trackedNetworks = await this.chainService.getTrackedNetworks() // Push any assets we have cached in the db for all active networks - Promise.allSettled( - trackedNetworks.map(async (network) => { - await this.cacheAssetsForNetwork(network) - this.emitter.emit("assets", this.getCachedAssets(network)) - }) + await Promise.allSettled( + trackedNetworks.map(async (network) => + this.cacheAssetsForNetwork(network) + ) + ).then((cachedAssetsPromises) => { + const cachedAssets = cachedAssetsPromises + .filter(isFulfilledPromise) + .map(({ value }) => value) + .flat() + + this.emitter.emit("assets", cachedAssets) + // Load balances after token lists load and after assets are cached, otherwise // we will not load balances on initial balance query - ).then(() => tokenListLoad.then(() => this.loadAccountBalances())) + return tokenListLoad.then(() => this.loadAccountBalances()) + }) }) } @@ -281,7 +290,7 @@ export default class IndexingService extends BaseService { * Caches to memory asset metadata from hard-coded base assets and configured token * lists. */ - async cacheAssetsForNetwork(network: EVMNetwork): Promise { + async cacheAssetsForNetwork(network: EVMNetwork): Promise { const customAssets = await this.db.getActiveCustomAssetsByNetworks([ network, ]) @@ -294,6 +303,8 @@ export default class IndexingService extends BaseService { customAssets, networkAssetsFromLists(network, tokenLists) ) + + return this.cachedAssets[network.chainID] } /** @@ -965,11 +976,17 @@ export default class IndexingService extends BaseService { // Cache assets across all supported networks even if a network // may be inactive. await Promise.allSettled( - this.chainService.supportedNetworks.map(async (network) => { - await this.cacheAssetsForNetwork(network) - this.emitter.emit("assets", this.getCachedAssets(network)) - }) - ) + this.chainService.supportedNetworks.map(async (network) => + this.cacheAssetsForNetwork(network) + ) + ).then((cachedAssetsPromises) => { + const cachedAssets = cachedAssetsPromises + .filter(isFulfilledPromise) + .map(({ value }) => value) + .flat() + + this.emitter.emit("assets", cachedAssets) + }) // TODO if tokenListPrefs.autoUpdate is true, pull the latest and update if // the version has gone up