-
Notifications
You must be signed in to change notification settings - Fork 268
Jon/fix/coinrank-loading #5544
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
Jon/fix/coinrank-loading #5544
Changes from all commits
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ import { fetchRates } from '../../util/network' | |
import { EdgeAnim, MAX_LIST_ITEMS_ANIM } from '../common/EdgeAnim' | ||
import { EdgeTouchableOpacity } from '../common/EdgeTouchableOpacity' | ||
import { SceneWrapper } from '../common/SceneWrapper' | ||
import { FillLoader } from '../progress-indicators/FillLoader' | ||
import { CoinRankRow } from '../rows/CoinRankRow' | ||
import { showDevError } from '../services/AirshipInstance' | ||
import { cacheStyles, Theme, useTheme } from '../services/ThemeContext' | ||
|
@@ -27,6 +28,12 @@ import { SearchFooter } from '../themed/SearchFooter' | |
|
||
const coinRanking: CoinRanking = { coinRankingDatas: [] } | ||
|
||
/** | ||
* Track changes that occurred to fiat while scene is unmounted. Don't react to | ||
* changes to this value. | ||
*/ | ||
let lastSceneFiat: string | ||
|
||
const QUERY_PAGE_SIZE = 30 | ||
const LISTINGS_REFRESH_INTERVAL = 30000 | ||
|
||
|
@@ -54,29 +61,43 @@ const CoinRankingComponent = (props: Props) => { | |
const { navigation } = props | ||
const dispatch = useDispatch() | ||
|
||
/** The user's fiat setting, falling back to USD if not supported. */ | ||
const coingeckoFiat = useSelector(state => getCoingeckoFiat(state)) | ||
const { coinRankingDatas } = coinRanking | ||
|
||
const mounted = React.useRef<boolean>(true) | ||
const lastStartIndex = React.useRef<number>(1) | ||
|
||
const [requestDataSize, setRequestDataSize] = React.useState<number>(QUERY_PAGE_SIZE) | ||
const [dataSize, setDataSize] = React.useState<number>(0) | ||
const [dataSize, setDataSize] = React.useState<number>(coinRankingDatas.length) | ||
const [searchText, setSearchText] = React.useState<string>('') | ||
const [isSearching, setIsSearching] = React.useState<boolean>(false) | ||
const [percentChangeTimeFrame, setPercentChangeTimeFrame] = React.useState<PercentChangeTimeFrame>('hours24') | ||
const [assetSubText, setPriceSubText] = React.useState<AssetSubText>('marketCap') | ||
const [footerHeight, setFooterHeight] = React.useState<number | undefined>() | ||
|
||
/** The user's fiat setting, falling back to USD if not supported. */ | ||
const coingeckoFiat = useSelector(state => { | ||
const currentCoinGeckoFiat = getCoingeckoFiat(state) | ||
if (lastSceneFiat == null) { | ||
lastSceneFiat = currentCoinGeckoFiat | ||
} | ||
|
||
// Whenever we see a different fiat, clear the cache. We want to do this | ||
// here as close to the site of the state change as possible to ensure this | ||
// happens before anything else. | ||
if (lastSceneFiat != null && currentCoinGeckoFiat !== lastSceneFiat) { | ||
debugLog(LOG_COINRANK, 'clearing cache') | ||
lastSceneFiat = currentCoinGeckoFiat | ||
coinRanking.coinRankingDatas = [] | ||
} | ||
return currentCoinGeckoFiat | ||
}) | ||
|
||
const handleScroll = useSceneScrollHandler() | ||
|
||
const extraData = React.useMemo( | ||
() => ({ assetSubText, supportedFiatSetting: coingeckoFiat, percentChangeTimeFrame }), | ||
[assetSubText, coingeckoFiat, percentChangeTimeFrame] | ||
) | ||
|
||
const { coinRankingDatas } = coinRanking | ||
|
||
const renderItem = (itemObj: ListRenderItemInfo<number>) => { | ||
const { index, item } = itemObj | ||
const currencyCode = coinRankingDatas[index]?.currencyCode ?? 'NO_CURRENCY_CODE' | ||
|
@@ -138,12 +159,6 @@ const CoinRankingComponent = (props: Props) => { | |
setFooterHeight(height) | ||
}) | ||
|
||
React.useEffect(() => { | ||
return () => { | ||
mounted.current = false | ||
} | ||
}, []) | ||
|
||
React.useEffect(() => { | ||
return navigation.addListener('focus', () => { | ||
dispatch(checkEnabledExchanges()) | ||
|
@@ -157,7 +172,7 @@ const CoinRankingComponent = (props: Props) => { | |
|
||
try { | ||
// Catch up to the total required items | ||
while (startIndex < requestDataSize - QUERY_PAGE_SIZE) { | ||
while (startIndex <= requestDataSize - QUERY_PAGE_SIZE + 1) { | ||
const url = `v2/coinrank?fiatCode=iso:${coingeckoFiat}&start=${startIndex}&length=${QUERY_PAGE_SIZE}` | ||
const response = await fetchRates(url).then(maybeAbort) | ||
if (!response.ok) { | ||
|
@@ -176,7 +191,7 @@ const CoinRankingComponent = (props: Props) => { | |
startIndex += QUERY_PAGE_SIZE | ||
} | ||
} catch (e: any) { | ||
console.warn(`Error during data fetch: ${e.message}`) | ||
console.warn(`Error during data fetch: ${e.message}, ${coingeckoFiat}, ${startIndex}, ${requestDataSize}`) | ||
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. This is good, but 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. Ah good catch yeah I didn't update this to be publicly readable from my own testing |
||
} | ||
|
||
setDataSize(coinRankingDatas.length) | ||
|
@@ -192,7 +207,7 @@ const CoinRankingComponent = (props: Props) => { | |
return abort | ||
}, [queryLoop, requestDataSize]) | ||
|
||
// Subscribe to changes to the current data set: | ||
// Subscribe to changes to coingeckoFiat and update the periodic refresh | ||
React.useEffect(() => { | ||
let abort = () => {} | ||
// Refresh from the beginning periodically | ||
|
@@ -220,8 +235,8 @@ const CoinRankingComponent = (props: Props) => { | |
} | ||
}, [coingeckoFiat /* reset subscription on fiat change */, queryLoop]) | ||
|
||
const listdata: number[] = React.useMemo(() => { | ||
debugLog(LOG_COINRANK, `Updating listdata dataSize=${dataSize} searchText=${searchText}`) | ||
const listData: number[] = React.useMemo(() => { | ||
debugLog(LOG_COINRANK, `Updating listData dataSize=${dataSize} searchText=${searchText}`) | ||
const out = [] | ||
for (let i = 0; i < dataSize; i++) { | ||
const cr = coinRankingDatas[i] | ||
|
@@ -282,17 +297,21 @@ const CoinRankingComponent = (props: Props) => { | |
</View> | ||
<DividerLine marginRem={[0, 0, 0, 1]} /> | ||
<View style={{ ...undoInsetStyle, marginTop: 0 }}> | ||
<Animated.FlatList | ||
contentContainerStyle={{ ...insetStyle, paddingTop: 0 }} | ||
data={listdata} | ||
extraData={extraData} | ||
keyboardDismissMode="on-drag" | ||
onEndReachedThreshold={1} | ||
onEndReached={handleEndReached} | ||
onScroll={handleScroll} | ||
renderItem={renderItem} | ||
scrollIndicatorInsets={SCROLL_INDICATOR_INSET_FIX} | ||
/> | ||
{listData.length === 0 ? ( | ||
<FillLoader /> | ||
) : ( | ||
<Animated.FlatList | ||
contentContainerStyle={{ ...insetStyle, paddingTop: 0 }} | ||
data={listData} | ||
extraData={extraData} | ||
keyboardDismissMode="on-drag" | ||
onEndReachedThreshold={1} | ||
onEndReached={handleEndReached} | ||
onScroll={handleScroll} | ||
renderItem={renderItem} | ||
scrollIndicatorInsets={SCROLL_INDICATOR_INSET_FIX} | ||
/> | ||
)} | ||
</View> | ||
</> | ||
)} | ||
|
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.
Does this fix an issue where the last item wasn't included for some reason? It's hard to know what the problem was and therefore review a fix if you don't know what the problem is. Could you maybe include the problem in the commit body?
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.
Yes, and this also fixes the issue where the query doesn't start immediately under some situations.
You can see in the changes you reviewed previously, I had a condition
startIndex === 1 || ...
which resolved the "not starting immediately" issue, but didn't fix the new page requests sometimes not fetching new rows.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.
I'll edit the commit message