Skip to content

Commit b198405

Browse files
authored
feat: optimize balance and history api query (#3088)
1 parent 51cef2c commit b198405

File tree

5 files changed

+77
-44
lines changed

5 files changed

+77
-44
lines changed

packages/engine/src/apiProxyUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';
12
import { RestfulRequest } from '@onekeyhq/shared/src/request/RestfulRequest';
23

34
import { getFiatEndpoint } from './endpoint';
@@ -41,6 +42,7 @@ export const getBalancesFromApi = async ({
4142
if (tokenAddresses?.length) {
4243
query.contract_addresses = tokenAddresses;
4344
}
45+
debugLogger.http.info('getBalancesFromApi', query);
4446
return (await req
4547
.get('/token/balances', query)
4648
.then((res) => res.json())) as TokenBalancesResponse;

packages/kit-bg/src/services/ServiceHistory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
backgroundMethod,
2121
} from '@onekeyhq/shared/src/background/backgroundDecorators';
2222
import { HISTORY_CONSTS } from '@onekeyhq/shared/src/engine/engineConsts';
23+
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';
2324

2425
import ServiceBase from './ServiceBase';
2526

@@ -107,6 +108,12 @@ class ServiceHistory extends ServiceBase {
107108
this.backgroundApi.dispatch(setIsPasswordLoadedInVault(isLoaded));
108109
}
109110

111+
debugLogger.http.info('fetchOnChainHistory', {
112+
networkId,
113+
accountId,
114+
tokenIdOnNetwork,
115+
});
116+
110117
return vault.fetchOnChainHistory({
111118
// TODO limit=50
112119
localHistory,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useCallback, useState } from 'react';
2+
3+
import { useFocusEffect } from '@react-navigation/core';
4+
5+
import platformEnv from '@onekeyhq/shared/src/platformEnv';
6+
7+
export const useVisibilityFocused = () => {
8+
const [isFocused, setIsFocused] = useState(false);
9+
10+
const onBlur = useCallback(() => setIsFocused(false), []);
11+
const onFocus = useCallback(() => setIsFocused(true), []);
12+
13+
const visibilityStateListener = useCallback(() => {
14+
if (document.visibilityState === 'hidden') {
15+
onBlur();
16+
}
17+
if (document.visibilityState === 'visible') {
18+
onFocus();
19+
}
20+
}, [onFocus, onBlur]);
21+
22+
useFocusEffect(
23+
useCallback(() => {
24+
onFocus();
25+
26+
if (platformEnv.isRuntimeBrowser) {
27+
document.addEventListener('visibilitychange', visibilityStateListener);
28+
if (!platformEnv.isDesktop) {
29+
window.addEventListener('blur', onBlur);
30+
window.addEventListener('focus', onFocus);
31+
}
32+
}
33+
34+
return () => {
35+
onBlur();
36+
if (platformEnv.isRuntimeBrowser) {
37+
document.removeEventListener(
38+
'visibilitychange',
39+
visibilityStateListener,
40+
);
41+
if (!platformEnv.isDesktop) {
42+
window.removeEventListener('blur', onBlur);
43+
window.removeEventListener('focus', onFocus);
44+
}
45+
}
46+
};
47+
}, [visibilityStateListener, onBlur, onFocus]),
48+
);
49+
50+
return isFocused;
51+
};

packages/kit/src/views/TxHistory/TxHistoryListView.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
33

4-
import { useIsFocused } from '@react-navigation/native';
54
import { useIntl } from 'react-intl';
65
import useSWR from 'swr';
76

@@ -24,6 +23,7 @@ import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';
2423
import backgroundApiProxy from '../../background/instance/backgroundApiProxy';
2524
import { useAppSelector } from '../../hooks';
2625
import useFormatDate from '../../hooks/useFormatDate';
26+
import { useVisibilityFocused } from '../../hooks/useVisibilityFocused';
2727
import { wait } from '../../utils/helper';
2828
import { useIsAtHomeTab } from '../../utils/routeUtils';
2929
import { TxListItemView } from '../TxDetail/TxListItemView';
@@ -245,6 +245,8 @@ function TxHistoryListViewComponent({
245245
const { serviceHistory } = backgroundApiProxy;
246246
const refreshHistoryTs = useAppSelector((s) => s.refresher.refreshHistoryTs);
247247

248+
const isFocused = useVisibilityFocused();
249+
248250
const fetchHistoryTx = useCallback(
249251
async (options: { refresh?: boolean } = {}): Promise<IHistoryTx[]> => {
250252
const { refresh = true } = options;
@@ -279,7 +281,6 @@ function TxHistoryListViewComponent({
279281
() => fetchHistoryTx({ refresh: false }),
280282
[fetchHistoryTx],
281283
);
282-
const isFocused = useIsFocused();
283284

284285
const shouldDoRefresh = useMemo((): boolean => {
285286
if (!accountId || !networkId) {

packages/kit/src/views/Wallet/AssetsList/index.tsx

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useEffect, useMemo } from 'react';
22

3-
import { useFocusEffect, useNavigation } from '@react-navigation/core';
3+
import { useNavigation } from '@react-navigation/core';
44
import { omit } from 'lodash';
55
import { useDebounce } from 'use-debounce';
66

@@ -27,8 +27,11 @@ import platformEnv from '@onekeyhq/shared/src/platformEnv';
2727

2828
import backgroundApiProxy from '../../../background/instance/backgroundApiProxy';
2929
import { useAccountTokens, useActiveSideAccount } from '../../../hooks';
30+
import { useStatus } from '../../../hooks/redux';
3031
import { useAccountTokenLoading } from '../../../hooks/useTokens';
32+
import { useVisibilityFocused } from '../../../hooks/useVisibilityFocused';
3133
import { OverviewDefiThumbnal } from '../../Overview/Thumbnail';
34+
import { WalletHomeTabEnum } from '../type';
3235

3336
import AssetsListHeader from './AssetsListHeader';
3437
import { EmptyListOfAccount } from './EmptyList';
@@ -127,15 +130,6 @@ function AssetsList({
127130
},
128131
);
129132

130-
const visibilityStateListener = useCallback(() => {
131-
if (document.visibilityState === 'hidden') {
132-
stopRefresh();
133-
}
134-
if (document.visibilityState === 'visible') {
135-
startRefresh();
136-
}
137-
}, [startRefresh, stopRefresh]);
138-
139133
useEffect(() => {
140134
const { serviceOverview } = backgroundApiProxy;
141135
serviceOverview.subscribe();
@@ -145,42 +139,20 @@ function AssetsList({
145139
}
146140
}, [networkId, accountId, startRefresh, stopRefresh]);
147141

148-
useFocusEffect(
149-
useCallback(() => {
150-
const { serviceToken } = backgroundApiProxy;
142+
const { homeTabName } = useStatus();
143+
144+
const isFocused = useVisibilityFocused();
145+
146+
useEffect(() => {
147+
if (isFocused && homeTabName === WalletHomeTabEnum.Tokens) {
151148
if (!account || !network) {
152149
return;
153150
}
154-
serviceToken.fetchAccountTokens({
155-
includeTop50TokensQuery: true,
156-
networkId: network?.id,
157-
accountId: account?.id,
158-
});
159-
160151
startRefresh();
161-
if (platformEnv.isRuntimeBrowser) {
162-
document.addEventListener('visibilitychange', visibilityStateListener);
163-
if (!platformEnv.isDesktop) {
164-
window.addEventListener('blur', stopRefresh);
165-
window.addEventListener('focus', startRefresh);
166-
}
167-
}
168-
169-
return () => {
170-
stopRefresh();
171-
if (platformEnv.isRuntimeBrowser) {
172-
document.removeEventListener(
173-
'visibilitychange',
174-
visibilityStateListener,
175-
);
176-
if (!platformEnv.isDesktop) {
177-
window.removeEventListener('blur', stopRefresh);
178-
window.removeEventListener('focus', startRefresh);
179-
}
180-
}
181-
};
182-
}, [account, network, visibilityStateListener, startRefresh, stopRefresh]),
183-
);
152+
} else {
153+
stopRefresh();
154+
}
155+
}, [isFocused, startRefresh, stopRefresh, account, network, homeTabName]);
184156

185157
const onTokenCellPress = useCallback(
186158
(item: Token) => {

0 commit comments

Comments
 (0)