FIX: Don't send data if its not new content.

This commit is contained in:
Marcos Rodriguez Vélez 2021-04-12 22:44:49 -04:00 committed by GitHub
parent f52daf6601
commit 17c52a11d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { useContext, useEffect } from 'react';
import { useContext, useEffect, useRef } from 'react';
import {
updateApplicationContext,
watchEvents,
@ -11,6 +11,7 @@ import { Chain } from './models/bitcoinUnits';
import loc, { formatBalance, transactionTimeToReadable } from './loc';
import { BlueStorageContext } from './blue_modules/storage-context';
import Notifications from './blue_modules/notifications';
import { FiatUnit } from './models/fiatUnit';
function WatchConnectivity() {
const { walletsInitialized, wallets, fetchWalletTransactions, saveToDisk, txMetadata, preferredFiatCurrency } = useContext(
@ -18,11 +19,22 @@ function WatchConnectivity() {
);
const isReachable = useReachability();
const isInstalled = useInstalled(); // true | false
const messagesListenerActive = useRef(false);
const lastPreferredCurrency = useRef(FiatUnit.USD.endPointKey);
useEffect(() => {
if (isInstalled && isReachable && walletsInitialized) {
watchEvents.on('message', handleMessages);
let messagesListener = () => {};
if (isInstalled && isReachable && walletsInitialized && messagesListenerActive.current === false) {
messagesListener = watchEvents.addListener('message', handleMessages);
messagesListenerActive.current = true;
} else {
messagesListener();
messagesListenerActive.current = false;
}
return () => {
messagesListener();
messagesListenerActive.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [walletsInitialized, isReachable, isInstalled]);
@ -35,11 +47,16 @@ function WatchConnectivity() {
useEffect(() => {
if (isInstalled && isReachable && walletsInitialized && preferredFiatCurrency) {
const preferredFiatCurrencyParsed = JSON.parse(preferredFiatCurrency);
try {
transferCurrentComplicationUserInfo({
preferredFiatCurrency: JSON.parse(preferredFiatCurrency).endPointKey,
});
sendWalletsToWatch();
if (lastPreferredCurrency.current !== preferredFiatCurrencyParsed.endPointKey) {
transferCurrentComplicationUserInfo({
preferredFiatCurrency: preferredFiatCurrencyParsed.endPointKey,
});
lastPreferredCurrency.current = preferredFiatCurrency.endPointKey;
} else {
console.log('WatchConnectivity lastPreferredCurrency has not changed');
}
} catch (e) {
console.log('WatchConnectivity useEffect preferredFiatCurrency error');
console.log(e);