From 55e562c9b451e6a67fe92f7d67f1359873eb4f8c Mon Sep 17 00:00:00 2001 From: Sergei Baranovski <120570511+sergei-deriv@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:04:47 +0300 Subject: [PATCH] [FEQ] Sergei / FEQ-2105 / Add try catch for exchange rates subscription (#14673) * feat: add try catch * fix: repair tests * feat: add console.warn as error handler for now * feat: to prevent warning in the console add setExchangeRates as action to the client store --- .../main-title-bar/__tests__/index.spec.tsx | 1 + .../src/helpers/total-assets-helper.ts | 7 ++- packages/core/src/Stores/client-store.js | 58 ++++++++++++------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/packages/appstore/src/components/main-title-bar/__tests__/index.spec.tsx b/packages/appstore/src/components/main-title-bar/__tests__/index.spec.tsx index 48dabc37e820..1e5baa2e9ffa 100644 --- a/packages/appstore/src/components/main-title-bar/__tests__/index.spec.tsx +++ b/packages/appstore/src/components/main-title-bar/__tests__/index.spec.tsx @@ -15,6 +15,7 @@ describe('MainTitleBar', () => { }, client: { is_landing_company_loaded: false, + exchange_rates: {}, }, feature_flags: { data: { wallet: false } }, }); diff --git a/packages/appstore/src/helpers/total-assets-helper.ts b/packages/appstore/src/helpers/total-assets-helper.ts index b6345c4220b4..160f9f343e1e 100644 --- a/packages/appstore/src/helpers/total-assets-helper.ts +++ b/packages/appstore/src/helpers/total-assets-helper.ts @@ -24,11 +24,16 @@ export const isRatesLoaded = ( } }); + const exchange_rates_keys = Object.keys(exchange_rates?.[total_assets_real_currency ?? ''] ?? {}); + const is_all_currencies_inside = currencies_need_exchange_rates.every(currency => + exchange_rates_keys.includes(currency) + ); + return ( currencies_need_exchange_rates.length === 0 || (total_assets_real_currency && exchange_rates?.[total_assets_real_currency] && currencies_need_exchange_rates.length && - currencies_need_exchange_rates.length === Object.keys(exchange_rates?.[total_assets_real_currency]).length) + is_all_currencies_inside) ); }; diff --git a/packages/core/src/Stores/client-store.js b/packages/core/src/Stores/client-store.js index 5bb9785bf0b3..d0309ce6e63c 100644 --- a/packages/core/src/Stores/client-store.js +++ b/packages/core/src/Stores/client-store.js @@ -417,6 +417,7 @@ export default class ClientStore extends BaseStore { subscribeToExchangeRate: action.bound, unsubscribeFromExchangeRate: action.bound, unsubscribeFromAllExchangeRates: action.bound, + setExchangeRates: action.bound, }); reaction( @@ -2767,21 +2768,32 @@ export default class ClientStore extends BaseStore { const matchingSubscription = this.subscriptions?.[name]?.[key]?.sub; if (matchingSubscription) return { key, subscription: matchingSubscription }; - const subscription = WS?.subscribe({ - [name]: 1, - subscribe: 1, - ...(payload ?? {}), - }); + try { + const subscription = await WS?.subscribe({ + [name]: 1, + subscribe: 1, + ...(payload ?? {}), + }); - this.addSubscription(name, key, subscription); - return { name, key, subscription }; + this.addSubscription(name, key, subscription); + return { key, subscription }; + } catch (error) { + // eslint-disable-next-line no-console + console.warn(`Error: code = ${error?.error?.code}, message = ${error?.error?.message}`); + return {}; + } }; unsubscribeByKey = async (name, key) => { const matchingSubscription = this.subscriptions?.[name]?.[key]?.sub; if (matchingSubscription) { - await WS?.forget(this.subscriptions?.[name]?.[key]?.id); - delete this.subscriptions?.[name]?.[key]; + try { + await WS?.forget(this.subscriptions?.[name]?.[key]?.id); + delete this.subscriptions?.[name]?.[key]; + } catch (error) { + // eslint-disable-next-line no-console + console.warn(`Error: code = ${error?.error?.code}, message = ${error?.error?.message}`); + } } }; @@ -2794,21 +2806,27 @@ export default class ClientStore extends BaseStore { target_currency, }); - subscription.subscribe(response => { - const rates = response.exchange_rates?.rates; - const subscription_id = String(response.subscription?.id); + subscription.subscribe( + response => { + const rates = response.exchange_rates?.rates; + const subscription_id = String(response.subscription?.id); - if (rates) { - this.addSubscription('exchange_rates', key, undefined, subscription_id); + if (rates) { + this.addSubscription('exchange_rates', key, undefined, subscription_id); - const currentData = { ...this.exchange_rates }; - if (currentData) { - currentData[base_currency] = { ...currentData[base_currency], ...rates }; - } else currentData = { [base_currency]: rates }; + const currentData = { ...this.exchange_rates }; + if (currentData) { + currentData[base_currency] = { ...currentData[base_currency], ...rates }; + } else currentData = { [base_currency]: rates }; - this.setExchangeRates(currentData); + this.setExchangeRates(currentData); + } + }, + error => { + // eslint-disable-next-line no-console + console.warn(`Error: code = ${error?.error?.code}, message = ${error?.error?.message}`); } - }); + ); }; unsubscribeFromExchangeRate = async (base_currency, target_currency) => {