Fix parallel calls for quotes in swaps (#12484)

feature/default_network_editable
Daniel 3 years ago committed by ryanml
parent 1c011b66aa
commit 429025380c
  1. 23
      app/scripts/controllers/swaps.js
  2. 2
      app/scripts/controllers/swaps.test.js

@ -79,7 +79,7 @@ const initialState = {
routeState: '',
swapsFeatureIsLive: true,
useNewSwapsApi: false,
isFetchingQuotes: false,
saveFetchedQuotes: false,
swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
},
@ -209,7 +209,11 @@ export default class SwapsController {
) {
const { chainId } = fetchParamsMetaData;
const {
swapsState: { useNewSwapsApi, quotesPollingLimitEnabled },
swapsState: {
useNewSwapsApi,
quotesPollingLimitEnabled,
saveFetchedQuotes,
},
} = this.store.getState();
if (!fetchParams) {
@ -230,7 +234,9 @@ export default class SwapsController {
const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1;
this.indexOfNewestCallInFlight = indexOfCurrentCall;
this.setIsFetchingQuotes(true);
if (!saveFetchedQuotes) {
this.setSaveFetchedQuotes(true);
}
let [newQuotes] = await Promise.all([
this._fetchTradesInfo(fetchParams, {
@ -241,18 +247,17 @@ export default class SwapsController {
]);
const {
swapsState: { isFetchingQuotes },
swapsState: { saveFetchedQuotes: saveFetchedQuotesAfterResponse },
} = 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.
if (!isFetchingQuotes) {
if (!saveFetchedQuotesAfterResponse) {
return [
{}, // quotes
null, // selectedAggId
];
}
this.setIsFetchingQuotes(false);
newQuotes = mapValues(newQuotes, (quote) => ({
...quote,
@ -559,10 +564,10 @@ export default class SwapsController {
this.store.updateState({ swapsState: { ...swapsState, routeState } });
}
setIsFetchingQuotes(status) {
setSaveFetchedQuotes(status) {
const { swapsState } = this.store.getState();
this.store.updateState({
swapsState: { ...swapsState, isFetchingQuotes: status },
swapsState: { ...swapsState, saveFetchedQuotes: status },
});
}

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

Loading…
Cancel
Save