Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEQ] Sergei / FEQ-2105 / Add try catch for exchange rates subscription #14673

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
};
57 changes: 37 additions & 20 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2767,21 +2767,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 };
heorhi-deriv marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
// eslint-disable-next-line no-console
console.warn(`Error: code = ${error?.error?.code}, message = ${error?.error?.message}`);
return {};
sergei-deriv marked this conversation as resolved.
Show resolved Hide resolved
}
};

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 +2805,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
Loading