Skip to content

Commit

Permalink
[FEQ] Sergei / FEQ-2105 / Add try catch for exchange rates subscripti…
Browse files Browse the repository at this point in the history
…on (#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
  • Loading branch information
sergei-deriv authored and likhith-deriv committed Apr 25, 2024
1 parent b1e97f2 commit 55e562c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('MainTitleBar', () => {
},
client: {
is_landing_company_loaded: false,
exchange_rates: {},
},
feature_flags: { data: { wallet: false } },
});
Expand Down
7 changes: 6 additions & 1 deletion packages/appstore/src/helpers/total-assets-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
};
58 changes: 38 additions & 20 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export default class ClientStore extends BaseStore {
subscribeToExchangeRate: action.bound,
unsubscribeFromExchangeRate: action.bound,
unsubscribeFromAllExchangeRates: action.bound,
setExchangeRates: action.bound,
});

reaction(
Expand Down Expand Up @@ -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}`);
}
}
};

Expand All @@ -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) => {
Expand Down

0 comments on commit 55e562c

Please sign in to comment.