-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
fix: improve assets perfs #12843
Changes from all commits
17cb451
3787621
c57cdf8
6ec4d9f
82bb345
46898c1
a14212a
9a06331
efffe78
2f0c3a5
32663de
fd9ce31
ab008d7
16dd60a
623ed65
8a65d88
0ca94db
ebce0f9
48bd136
64b0816
21de161
deff8cb
560110c
b29db02
f863139
395d101
f0ccadb
9924753
9ae0c13
79c000e
d287b7e
14dd5b9
3f6c35d
5689064
4742e69
173ed61
aa826aa
dbc4d96
b5ed66a
788e1be
b2f6a00
efa1250
3964087
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -195,7 +196,11 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({ | |
networkConfiguration.defaultRpcEndpointIndex | ||
]?.networkClientId; | ||
|
||
await NetworkController.setActiveNetwork(networkClientId as string); | ||
const throttledSetActiveNetwork = throttle(async (id: string) => { | ||
await NetworkController.setActiveNetwork(id); | ||
}, 300); | ||
|
||
throttledSetActiveNetwork(networkClientId as string); | ||
} | ||
} | ||
if (asset.isETH && ticker) { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ import { | |
RpcEndpointType, | ||
AddNetworkFields, | ||
} from '@metamask/network-controller'; | ||
import { throttle } from 'lodash'; | ||
|
||
export interface SafeChain { | ||
chainId: string; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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 ( | ||
|
@@ -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(); | ||
|
||
|
There was a problem hiding this comment.
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?
https://css-tricks.com/debouncing-throttling-explained-examples/