Skip to content

Commit ebdab0c

Browse files
committed
feat(suite): chunk the fetch requests of the token fiat rates
1 parent 6e71bf5 commit ebdab0c

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

suite-common/wallet-core/src/fiat-rates/fiatRatesThunks.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,31 @@ export const fetchFiatRatesThunk = createThunk(
184184
// NOTE: do not await it here, leave it just to return
185185
// updateFiatRatesThunk is handled in the reducer and we don't need to wait for
186186
// all the token fiat rates to be loaded as it slows down start of the app massively
187-
dispatch(
188-
updateFiatRatesThunk({
189-
tickers,
190-
localCurrency,
191-
rateType,
192-
fetchAttemptTimestamp: Date.now() as Timestamp,
193-
}),
187+
// Because of that, let's chunk the number of fiat rates to be loaded
188+
// and have then loaded by chunks to not overload the API
189+
const FIAT_RATES_FETCH_CHUNK_SIZE = 4;
190+
const tickerChunks = Array.from(
191+
{ length: Math.ceil(tickers.length / FIAT_RATES_FETCH_CHUNK_SIZE) },
192+
(_, i) =>
193+
tickers.slice(
194+
i * FIAT_RATES_FETCH_CHUNK_SIZE,
195+
(i + 1) * FIAT_RATES_FETCH_CHUNK_SIZE,
196+
),
197+
);
198+
199+
tickerChunks.reduce<Promise<any>>(
200+
(chain, chunk) =>
201+
chain.then(() =>
202+
dispatch(
203+
updateFiatRatesThunk({
204+
tickers: chunk,
205+
localCurrency,
206+
rateType,
207+
fetchAttemptTimestamp: Date.now() as Timestamp,
208+
}),
209+
),
210+
),
211+
Promise.resolve(),
194212
);
195213

196214
return;

0 commit comments

Comments
 (0)