Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve assets perfs #12843

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
17cb451
fix: improve network switch perfs
salimtb Jan 7, 2025
3787621
fix: improve perf
salimtb Jan 8, 2025
c57cdf8
fix: reduce scope
salimtb Jan 9, 2025
6ec4d9f
fix: fix PR comments
salimtb Jan 9, 2025
82bb345
fix: fix PR comment
salimtb Jan 9, 2025
46898c1
fix: fix PR comments
salimtb Jan 9, 2025
a14212a
fix: fix list
salimtb Jan 10, 2025
9a06331
Merge branch 'main' into fix/improve-network-switch-perf
salimtb Jan 10, 2025
efffe78
fix: fix perf
salimtb Jan 10, 2025
2f0c3a5
fix: clean up
salimtb Jan 10, 2025
32663de
fix: fix clean up
salimtb Jan 10, 2025
fd9ce31
fix: fix unit test
salimtb Jan 10, 2025
ab008d7
fix: fix linter
salimtb Jan 10, 2025
16dd60a
Merge branch 'main' into fix/improve-network-switch-perf
salimtb Jan 10, 2025
623ed65
fix: improve set active network execution
salimtb Jan 12, 2025
8a65d88
fix: fix linter
salimtb Jan 12, 2025
0ca94db
fix: use throttle to improve perf
salimtb Jan 13, 2025
ebce0f9
fix: fix linter
salimtb Jan 13, 2025
48bd136
fix: use throttle to set active network
salimtb Jan 13, 2025
64b0816
fix: fix linter
salimtb Jan 13, 2025
21de161
Merge branch 'main' into fix/improve-network-switch-perf
salimtb Jan 13, 2025
deff8cb
Merge branch 'main' into fix/improve-network-switch-perf
salimtb Jan 13, 2025
560110c
fix: clean up
salimtb Jan 13, 2025
b29db02
Merge branch 'main' into fix/improve-network-switch-perf
salimtb Jan 14, 2025
f863139
fix: fix perf token list
salimtb Jan 14, 2025
395d101
Merge branch 'main' into fix/improve-network-switch-perf
salimtb Jan 14, 2025
f0ccadb
fix: fix after merge
salimtb Jan 14, 2025
9924753
fix: add fashlist setup
salimtb Jan 15, 2025
9ae0c13
fix: use flash list
salimtb Jan 15, 2025
79c000e
fix: clean up
salimtb Jan 15, 2025
d287b7e
fix: remove estimated size
salimtb Jan 15, 2025
14dd5b9
fix: clean up
salimtb Jan 15, 2025
3f6c35d
fix: clean up
salimtb Jan 15, 2025
5689064
fix: optimization
salimtb Jan 15, 2025
4742e69
fix: clean up
salimtb Jan 15, 2025
173ed61
fix: optimisation
salimtb Jan 15, 2025
aa826aa
fix: clean up
salimtb Jan 15, 2025
dbc4d96
fix: optimization
salimtb Jan 15, 2025
b5ed66a
fix: optimization
salimtb Jan 15, 2025
788e1be
fix: fix error
salimtb Jan 15, 2025
b2f6a00
fix: remove token detection polling
salimtb Jan 15, 2025
efa1250
fix: fix optimization
salimtb Jan 15, 2025
3964087
fix: improve refresh
salimtb Jan 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions app/components/UI/AssetOverview/AssetOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { useMetrics } from '../../../components/hooks/useMetrics';
import { createBuyNavigationDetails } from '../Ramp/routes/utils';
import { TokenI } from '../Tokens/types';
import AssetDetailsActions from '../../../components/Views/AssetDetails/AssetDetailsActions';
import { throttle } from 'lodash';

interface AssetOverviewProps {
asset: TokenI;
Expand Down Expand Up @@ -195,7 +196,11 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({
networkConfiguration.defaultRpcEndpointIndex
]?.networkClientId;

await NetworkController.setActiveNetwork(networkClientId as string);
const throttledSetActiveNetwork = throttle(async (id: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Discuss - do we want a throttle, or a debounce here?

  • Throttle - every Xms, we will make the call to set active network. So when "spammed" we will attempt to switch the network.
  • Debounce - we only select the active network once, when spam ends. So whatever the last value received from the "spam" invocations, we will execute.

https://css-tricks.com/debouncing-throttling-explained-examples/

await NetworkController.setActiveNetwork(id);
}, 300);

throttledSetActiveNetwork(networkClientId as string);
}
}
if (asset.isETH && ticker) {
Expand Down Expand Up @@ -226,13 +231,23 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({
networkConfiguration.defaultRpcEndpointIndex
]?.networkClientId;

NetworkController.setActiveNetwork(networkClientId as string).then(
() => {
setTimeout(() => {
handleSwapNavigation();
}, 500);
// Create a throttled version of setActiveNetwork
const throttledSetActiveNetwork = throttle(
async (networkClientIdToUse: string) => {
try {
await NetworkController.setActiveNetwork(networkClientIdToUse);
setTimeout(() => {
handleSwapNavigation();
}, 500);
Comment on lines +239 to +241
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retaining the original setTimeout here. I'm not entirely sure why it is needed, but that's fine.

} catch (error) {
Logger.error(new Error(`Error in setActiveNetwork: ${error}`));
}
},
300,
{ leading: true, trailing: false },
);

throttledSetActiveNetwork(networkClientId as string);
} else {
handleSwapNavigation();
}
Expand Down
19 changes: 16 additions & 3 deletions app/components/UI/NetworkModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
RpcEndpointType,
AddNetworkFields,
} from '@metamask/network-controller';
import { throttle } from 'lodash';

export interface SafeChain {
chainId: string;
Expand Down Expand Up @@ -266,7 +267,11 @@ const NetworkModals = (props: NetworkProps) => {

if (networkClientId) {
onUpdateNetworkFilter();
await NetworkController.setActiveNetwork(networkClientId);
const throttledSetActiveNetwork = throttle(async (id: string) => {
await NetworkController.setActiveNetwork(id);
}, 300);

throttledSetActiveNetwork(networkClientId as string);
Comment on lines +270 to +274
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (we don't need to do this now) - is it possible to make this a reusable func?

}

onClose();
Expand All @@ -292,7 +297,11 @@ const NetworkModals = (props: NetworkProps) => {
updatedNetwork?.rpcEndpoints?.[updatedNetwork.defaultRpcEndpointIndex] ??
{};
onUpdateNetworkFilter();
await NetworkController.setActiveNetwork(networkClientId);
const throttledSetActiveNetwork = throttle(async (id: string) => {
await NetworkController.setActiveNetwork(id);
}, 300);

throttledSetActiveNetwork(networkClientId as string);
};

const handleNewNetwork = async (
Expand Down Expand Up @@ -361,7 +370,11 @@ const NetworkModals = (props: NetworkProps) => {
{};

onUpdateNetworkFilter();
NetworkController.setActiveNetwork(networkClientId);
const throttledSetActiveNetwork = throttle(async (id: string) => {
await NetworkController.setActiveNetwork(id);
}, 300);

throttledSetActiveNetwork(networkClientId as string);
}
onClose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { ScrollView } from 'react-native-gesture-handler';
import { ChainId, toHex } from '@metamask/controller-utils';
import { useSelector } from 'react-redux';
import { throttle } from 'lodash';

import LoadingNetworksSkeleton from './LoadingNetworksSkeleton';
import ScreenLayout from '../../components/ScreenLayout';
Expand Down Expand Up @@ -145,7 +146,11 @@ function NetworkSwitcher() {
const switchToMainnet = useCallback(
(type: 'mainnet' | 'linea-mainnet') => {
const { NetworkController } = Engine.context;
NetworkController.setActiveNetwork(type);
const throttledSetActiveNetwork = throttle(async (id: string) => {
await NetworkController.setActiveNetwork(id);
}, 300);

throttledSetActiveNetwork(type);
navigateToGetStarted();
},
[navigateToGetStarted],
Expand All @@ -164,7 +169,11 @@ function NetworkSwitcher() {
const { networkClientId } =
rpcEndpoints?.[defaultRpcEndpointIndex] ?? {};

NetworkController.setActiveNetwork(networkClientId);
const throttledSetActiveNetwork = throttle(async (id: string) => {
await NetworkController.setActiveNetwork(id);
}, 300);

throttledSetActiveNetwork(networkClientId as string);
navigateToGetStarted();
}
},
Expand Down
60 changes: 39 additions & 21 deletions app/components/UI/Tokens/TokenList/PortfolioBalance/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo, useCallback } from 'react';
import { View, TouchableOpacity } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { useSelector } from 'react-redux';
Expand Down Expand Up @@ -56,7 +56,7 @@
import { getChainIdsToPoll } from '../../../../../selectors/tokensController';
import AggregatedPercentageCrossChains from '../../../../../component-library/components-temp/Price/AggregatedPercentage/AggregatedPercentageCrossChains';

export const PortfolioBalance = () => {
export const PortfolioBalance = React.memo(() => {
const { PreferencesController } = Engine.context;
const { colors } = useTheme();
const styles = createStyles(colors);
Expand Down Expand Up @@ -112,24 +112,32 @@
type,
);

let total;
if (isOriginalNativeTokenSymbol) {
if (isPortfolioViewEnabled()) {
total = totalFiatBalance ?? 0;
} else {
const total = useMemo(() => {
if (isOriginalNativeTokenSymbol) {
if (isPortfolioViewEnabled()) {
return totalFiatBalance ?? 0;
}
const tokenFiatTotal = balance?.tokenFiat ?? 0;
const ethFiatTotal = balance?.ethFiat ?? 0;
total = tokenFiatTotal + ethFiatTotal;
return tokenFiatTotal + ethFiatTotal;
}
} else if (isPortfolioViewEnabled()) {
total = totalTokenFiat ?? 0;
} else {
total = balance?.tokenFiat ?? 0;
}

const fiatBalance = `${renderFiat(total, currentCurrency)}`;
if (isPortfolioViewEnabled()) {
return totalTokenFiat ?? 0;
}
return balance?.tokenFiat ?? 0;
}, [

Check failure on line 128 in app/components/UI/Tokens/TokenList/PortfolioBalance/index.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

React Hook useMemo has an unnecessary dependency: 'isPortfolioViewEnabled'. Either exclude it or remove the dependency array. Outer scope values like 'isPortfolioViewEnabled' aren't valid dependencies because mutating them doesn't re-render the component
isOriginalNativeTokenSymbol,
isPortfolioViewEnabled,
totalFiatBalance,
totalTokenFiat,
balance,
]);

const onOpenPortfolio = () => {
const fiatBalance = useMemo(
() => `${renderFiat(total, currentCurrency)}`,
[total, currentCurrency],
);
const onOpenPortfolio = useCallback(() => {
const existingPortfolioTab = browserTabs.find(({ url }: BrowserTab) =>
isPortfolioUrl(url),
);
Expand Down Expand Up @@ -172,7 +180,14 @@
})
.build(),
);
};
}, [
navigation,
trackEvent,
createEventBuilder,
isEnabled,
isDataCollectionForMarketingEnabled,
browserTabs,
]);

const renderAggregatedPercentage = () => {
if (isTestNet(chainId)) {
Expand All @@ -199,9 +214,12 @@
);
};

const toggleIsBalanceAndAssetsHidden = (value: boolean) => {
PreferencesController.setPrivacyMode(value);
};
const toggleIsBalanceAndAssetsHidden = useCallback(
(value: boolean) => {
PreferencesController.setPrivacyMode(value);
},
[PreferencesController],
);

return (
<View style={styles.portfolioBalance}>
Expand Down Expand Up @@ -247,4 +265,4 @@
</View>
</View>
);
};
});
Loading
Loading