Fix parallel calls for quotes in swaps (#12484)

feature/default_network_editable
Daniel 3 years ago committed by GitHub
parent b5e601bc87
commit e85248aaf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      app/scripts/controllers/swaps.js
  2. 2
      app/scripts/controllers/swaps.test.js

@ -79,7 +79,7 @@ const initialState = {
routeState: '', routeState: '',
swapsFeatureIsLive: true, swapsFeatureIsLive: true,
useNewSwapsApi: false, useNewSwapsApi: false,
isFetchingQuotes: false, saveFetchedQuotes: false,
swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
}, },
@ -209,7 +209,11 @@ export default class SwapsController {
) { ) {
const { chainId } = fetchParamsMetaData; const { chainId } = fetchParamsMetaData;
const { const {
swapsState: { useNewSwapsApi, quotesPollingLimitEnabled }, swapsState: {
useNewSwapsApi,
quotesPollingLimitEnabled,
saveFetchedQuotes,
},
} = this.store.getState(); } = this.store.getState();
if (!fetchParams) { if (!fetchParams) {
@ -230,7 +234,9 @@ export default class SwapsController {
const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1; const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1;
this.indexOfNewestCallInFlight = indexOfCurrentCall; this.indexOfNewestCallInFlight = indexOfCurrentCall;
this.setIsFetchingQuotes(true); if (!saveFetchedQuotes) {
this.setSaveFetchedQuotes(true);
}
let [newQuotes] = await Promise.all([ let [newQuotes] = await Promise.all([
this._fetchTradesInfo(fetchParams, { this._fetchTradesInfo(fetchParams, {
@ -241,18 +247,17 @@ export default class SwapsController {
]); ]);
const { const {
swapsState: { isFetchingQuotes }, swapsState: { saveFetchedQuotes: saveFetchedQuotesAfterResponse },
} = this.store.getState(); } = this.store.getState();
// If isFetchingQuotes is false, it means a user left Swaps (we cleaned the state) // If saveFetchedQuotesAfterResponse is false, it means a user left Swaps (we cleaned the state)
// and we don't want to set any API response with quotes into state. // and we don't want to set any API response with quotes into state.
if (!isFetchingQuotes) { if (!saveFetchedQuotesAfterResponse) {
return [ return [
{}, // quotes {}, // quotes
null, // selectedAggId null, // selectedAggId
]; ];
} }
this.setIsFetchingQuotes(false);
newQuotes = mapValues(newQuotes, (quote) => ({ newQuotes = mapValues(newQuotes, (quote) => ({
...quote, ...quote,
@ -559,10 +564,10 @@ export default class SwapsController {
this.store.updateState({ swapsState: { ...swapsState, routeState } }); this.store.updateState({ swapsState: { ...swapsState, routeState } });
} }
setIsFetchingQuotes(status) { setSaveFetchedQuotes(status) {
const { swapsState } = this.store.getState(); const { swapsState } = this.store.getState();
this.store.updateState({ this.store.updateState({
swapsState: { ...swapsState, isFetchingQuotes: status }, swapsState: { ...swapsState, saveFetchedQuotes: status },
}); });
} }

@ -135,7 +135,7 @@ const EMPTY_INIT_STATE = {
swapsQuoteRefreshTime: 60000, swapsQuoteRefreshTime: 60000,
swapsQuotePrefetchingRefreshTime: 60000, swapsQuotePrefetchingRefreshTime: 60000,
swapsUserFeeLevel: '', swapsUserFeeLevel: '',
isFetchingQuotes: false, saveFetchedQuotes: false,
}, },
}; };

Loading…
Cancel
Save