Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Extrapolate smart logic into containers - stop mem leak #1365

Closed
wants to merge 18 commits into from

Conversation

W3stside
Copy link
Contributor

Summary

There was smart logic (hooks, async logic etc) causing memory leaks when these components were being unmounted conditionally inside of SwapMod.

Examples: TradeBasicDetails, RowFee, RowSlippage whom all calculated USD estimation inside their funciton body but were being unmounted inside SwapMod. Now the logic is moved to the relevant parent containers and unmounted inside the render method to safely avoid mem leaks.

To Test

  1. use this pr and try different prices to show fee warning, then quickly switch amounts
  • in this PR there should be NO errors concerning sth like "can't setState on unmounted component blah blah"
  1. Compare this with [USD Price] Add coingecko api and hooks #1356 where you should see the errors in the console

@W3stside W3stside requested review from anxolin, alfetopito and a team August 31, 2021 11:13
@github-actions
Copy link
Contributor

  • 🔭 GP Swap: Gnosis Protocol v2 Swap UI


return <TradePriceMod {...props} fiatValue={fiatValueFormatted} />
export default function TradePrice(props: TradePriceProps) {
return <TradePriceMod {...props} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not exporting this directly if u don't modify anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

artifact, i am removing this. before i had logic in here but moved it away, good catch

@@ -186,7 +186,7 @@ export function useCoingeckoUsdValue(params: CoinGeckoUsdValueParams) {
return useGetPriceQuote({ ...coingeckoUsdPrice, currencyAmount })
}

export function useHigherUSDValue(currencyAmount: CurrencyAmount<Currency> | undefined) {
export function useHigherUSDValue(currencyAmount: CurrencyAmount<Currency> | undefined | null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need to have both undefined and null? what is the meaning of each?
Makes API of this hook harder imo

Copy link
Contributor Author

@W3stside W3stside Aug 31, 2021

Choose a reason for hiding this comment

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

no we don't made this for ease of typing as it's a bit random (Uni's fault hoenstly) as they have TYPE | undefined or TYPE | null everywhere it's wild

{trade && (
<Price trade={trade} theme={theme} showInverted={showInverted} setShowInverted={setShowInverted} />
)}
<Price trade={trade} theme={theme} showInverted={showInverted} setShowInverted={setShowInverted} />
Copy link
Contributor

Choose a reason for hiding this comment

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

But why? I would think the fault of memory leaks would be inside Price not in SwapMod. What is the issue of conditionally rendering a component?

If you change it for memory leaks here, u are doing a "white box" fix, where u know the implementation of Price does something bad when unmounted. I understand SwapMod wouldn't need to know anything about the Price component internals (should treat it as a "black box").

Let me know if my reasoning above is incorrect, likely missing sth here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because price component was rendering a child that was calculating state that when un mounted was not cancelling the asynchronous promise

Copy link
Contributor

Choose a reason for hiding this comment

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

This is what i mean, the problem is in price, not in SwapMod, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i see it 2 ways:

  1. SwapMod has the data sources/biz logic, and Price is dumb. It gets all the props etc, but now it needs a wrapping container to be independent and reusable elsewhere - SwapMod is the smart container in this example and relies on it to render it it's smart props
  2. it has a closer related wrapping container that isn't SwapMod that handles the biz logic and can be used as 1 component in other places (which is what i assume is what u intended with the refactor) - so now inside /custom/swap/index.ts we have the logic inside the parent containers that allow the entire components to be reusable. for Price you can see that logic here: https://github.com/gnosis/cowswap/pull/1365/files#diff-e1d5a26ef1f0d5d518088887aa8e77620e509679ab3a6138b94360dc722102ccR196
    which we can export and use everywhere and it will always render the price in USD

does that make sense?

Copy link
Contributor

@alfetopito alfetopito left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 93 to 92
currencyAmount?: CurrencyAmount<Currency>
currencyAmount: CurrencyAmount<Currency> | undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

potato / potahto ? (in other words, same thing, change needed?

@W3stside W3stside force-pushed the extrapolate-smart-logic-into-containers branch from 8fead7c to 34b06fe Compare September 3, 2021 08:58
@W3stside W3stside requested a review from anxolin September 3, 2021 08:59
@W3stside
Copy link
Contributor Author

W3stside commented Sep 6, 2021

@anxolin I'd like to move this forward, please re-review when you can

Base automatically changed from add-coingecko-usd-query to develop September 7, 2021 14:28
@W3stside
Copy link
Contributor Author

updated with develop but closing for now as not seeing the unmount error in barn/staging

@W3stside W3stside closed this Sep 23, 2021
@alfetopito alfetopito deleted the extrapolate-smart-logic-into-containers branch September 23, 2021 16:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants