From 429025380c1a145aeacdc621faa6287a208056f9 Mon Sep 17 00:00:00 2001 From: Daniel <80175477+dan437@users.noreply.github.com> Date: Thu, 28 Oct 2021 08:53:26 +0200 Subject: [PATCH] Fix parallel calls for quotes in swaps (#12484) --- app/scripts/controllers/swaps.js | 23 ++++++++++++++--------- app/scripts/controllers/swaps.test.js | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index d48eea028..0d69dd77c 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.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 }, }); } diff --git a/app/scripts/controllers/swaps.test.js b/app/scripts/controllers/swaps.test.js index 978c705a8..7bc26a5aa 100644 --- a/app/scripts/controllers/swaps.test.js +++ b/app/scripts/controllers/swaps.test.js @@ -135,7 +135,7 @@ const EMPTY_INIT_STATE = { swapsQuoteRefreshTime: 60000, swapsQuotePrefetchingRefreshTime: 60000, swapsUserFeeLevel: '', - isFetchingQuotes: false, + saveFetchedQuotes: false, }, };