Skip USER_STATE_POPULATE when sync_hash is the same

## Changes
- doHandleSyncComplete: only call doGetAndPopulatePreferences when there is new data.
- But for that to work, we'll need to populate preferences at least once. We'll do that in doSignIn.
- We can also remove the "sync/prefs ready" mechanism that was mainly meant for Desktop.

Then came another problem: while trying to spark changes between 2 tabs, `sync/get` was saying "no change" despite the local and server hash being different. I think it is because the both `sync_hash + sync/get` combo is operating on server data, so the hash is the same. I'm guessing this is why we ended up just running doGetAndPopulatePreferences every time before PR, since this flag wasn't correct in this scenario.

- Updated `data.changed` to consider both API results and comparison with local hash.
This commit is contained in:
infinite-persistence 2021-11-15 11:28:30 +08:00 committed by infinite-persistence
parent 342fcb4024
commit 38c13cf5ef
4 changed files with 17 additions and 45 deletions

View file

@ -22,15 +22,9 @@ import {
selectIsReloadRequired,
} from 'redux/selectors/app';
import { selectUploadCount } from 'redux/selectors/publish';
import { doGetWalletSyncPreference, doSetLanguage } from 'redux/actions/settings';
import { doSetLanguage } from 'redux/actions/settings';
import { doSyncLoop } from 'redux/actions/sync';
import {
doDownloadUpgradeRequested,
doSignIn,
doGetAndPopulatePreferences,
doSetActiveChannel,
doSetIncognito,
} from 'redux/actions/app';
import { doDownloadUpgradeRequested, doSignIn, doSetActiveChannel, doSetIncognito } from 'redux/actions/app';
import { doFetchModBlockedList, doFetchCommentModAmIList } from 'redux/actions/comments';
import App from './view';
@ -62,8 +56,6 @@ const perform = (dispatch) => ({
setLanguage: (language) => dispatch(doSetLanguage(language)),
signIn: () => dispatch(doSignIn()),
requestDownloadUpgrade: () => dispatch(doDownloadUpgradeRequested()),
updatePreferences: () => dispatch(doGetAndPopulatePreferences()),
getWalletSyncPref: () => dispatch(doGetWalletSyncPreference()),
syncLoop: (noInterval) => dispatch(doSyncLoop(noInterval)),
setReferrer: (referrer, doClaim) => dispatch(doUserSetReferrer(referrer, doClaim)),
setActiveChannelIfNotSet: () => dispatch(doSetActiveChannel()),

View file

@ -65,8 +65,6 @@ type Props = {
isUpgradeAvailable: boolean,
isReloadRequired: boolean,
autoUpdateDownloaded: boolean,
updatePreferences: () => Promise<any>,
getWalletSyncPref: () => Promise<any>,
uploadCount: number,
balance: ?number,
syncError: ?string,
@ -106,8 +104,6 @@ function App(props: Props) {
language,
languages,
setLanguage,
updatePreferences,
getWalletSyncPref,
rewards,
setReferrer,
isAuthenticated,
@ -127,8 +123,6 @@ function App(props: Props) {
const appRef = useRef();
const isEnhancedLayout = useKonamiListener();
const [hasSignedIn, setHasSignedIn] = useState(false);
const [readyForSync, setReadyForSync] = useState(false);
const [readyForPrefs, setReadyForPrefs] = useState(false);
const hasVerifiedEmail = user && Boolean(user.has_verified_email);
const isRewardApproved = user && user.is_reward_approved;
const previousHasVerifiedEmail = usePrevious(hasVerifiedEmail);
@ -287,23 +281,11 @@ function App(props: Props) {
};
}, []);
// @if TARGET='app'
useEffect(() => {
if (updatePreferences && getWalletSyncPref && readyForPrefs) {
getWalletSyncPref()
.then(() => updatePreferences())
.then(() => {
setReadyForSync(true);
});
}
}, [updatePreferences, getWalletSyncPref, setReadyForSync, readyForPrefs, hasVerifiedEmail]);
// @endif
// ready for sync syncs, however after signin when hasVerifiedEmail, that syncs too.
useEffect(() => {
// signInSyncPref is cleared after sharedState loop.
const syncLoopWithoutInterval = () => syncLoop(true);
if (readyForSync && hasVerifiedEmail) {
if (hasSignedIn && hasVerifiedEmail) {
// In case we are syncing.
syncLoop();
window.addEventListener('focus', syncLoopWithoutInterval);
@ -311,16 +293,7 @@ function App(props: Props) {
return () => {
window.removeEventListener('focus', syncLoopWithoutInterval);
};
}, [readyForSync, hasVerifiedEmail, syncLoop]);
// We know someone is logging in or not when we get their user object
// We'll use this to determine when it's time to pull preferences
// This will no longer work if desktop users no longer get a user object from lbryinc
useEffect(() => {
if (user) {
setReadyForPrefs(true);
}
}, [user, setReadyForPrefs]);
}, [hasSignedIn, hasVerifiedEmail, syncLoop]);
useEffect(() => {
if (syncError && isAuthenticated && !pathname.includes(PAGES.AUTH_WALLET_PASSWORD) && !currentModal) {
@ -334,7 +307,6 @@ function App(props: Props) {
if (!hasSignedIn && hasVerifiedEmail) {
signIn();
setHasSignedIn(true);
if (IS_WEB) setReadyForSync(true);
}
}, [hasVerifiedEmail, signIn, hasSignedIn]);

View file

@ -543,6 +543,7 @@ export function doSignIn() {
pushNotifications.validate(user.id);
}
dispatch(doGetAndPopulatePreferences());
dispatch(doNotificationSocketConnect(true));
dispatch(doNotificationList());
dispatch(doCheckPendingClaims());
@ -674,9 +675,8 @@ export function doGetAndPopulatePreferences() {
export function doHandleSyncComplete(error, hasNewData) {
return (dispatch) => {
if (!error) {
dispatch(doGetAndPopulatePreferences());
if (hasNewData) {
dispatch(doGetAndPopulatePreferences());
// we just got sync data, better update our channels
dispatch(doFetchChannelListMine());
}

View file

@ -4,7 +4,12 @@ import * as SETTINGS from 'constants/settings';
import { Lbryio } from 'lbryinc';
import Lbry from 'lbry';
import { doWalletEncrypt, doWalletDecrypt } from 'redux/actions/wallet';
import { selectGetSyncIsPending, selectSetSyncIsPending, selectSyncIsLocked } from 'redux/selectors/sync';
import {
selectSyncHash,
selectGetSyncIsPending,
selectSetSyncIsPending,
selectSyncIsLocked,
} from 'redux/selectors/sync';
import { makeSelectClientSetting } from 'redux/selectors/settings';
import { getSavedPassword, getAuthToken } from 'util/saved-passwords';
import { doAnalyticsTagSync, doHandleSyncComplete } from 'redux/actions/app';
@ -166,7 +171,10 @@ export function doGetSync(passedPassword?: string, callback?: (any, ?boolean) =>
}
// @endif
return (dispatch: Dispatch) => {
return (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const localHash = selectSyncHash(state);
dispatch({
type: ACTIONS.GET_SYNC_STARTED,
});
@ -194,7 +202,7 @@ export function doGetSync(passedPassword?: string, callback?: (any, ?boolean) =>
const syncHash = response.hash;
data.syncHash = syncHash;
data.syncData = response.data;
data.changed = response.changed;
data.changed = response.changed || syncHash !== localHash;
data.hasSyncedWallet = true;
if (response.changed) {