diff --git a/ui/component/app/index.js b/ui/component/app/index.js index 7e72ad791..cb4a74f17 100644 --- a/ui/component/app/index.js +++ b/ui/component/app/index.js @@ -14,13 +14,8 @@ import { } from 'redux/selectors/settings'; import { selectIsUpgradeAvailable, selectAutoUpdateDownloaded, selectModal } from 'redux/selectors/app'; import { doGetWalletSyncPreference, doSetLanguage } from 'redux/actions/settings'; -import { doSyncSubscribe } from 'redux/actions/sync'; -import { - doDownloadUpgradeRequested, - doSignIn, - doGetAndPopulatePreferences, - doAnalyticsTagSync, -} from 'redux/actions/app'; +import { doSyncLoop } from 'redux/actions/sync'; +import { doDownloadUpgradeRequested, doSignIn, doGetAndPopulatePreferences } from 'redux/actions/app'; import App from './view'; const select = state => ({ @@ -41,7 +36,6 @@ const select = state => ({ }); const perform = dispatch => ({ - analyticsTagSync: () => dispatch(doAnalyticsTagSync()), fetchAccessToken: () => dispatch(doFetchAccessToken()), fetchChannelListMine: () => dispatch(doFetchChannelListMine()), setLanguage: language => dispatch(doSetLanguage(language)), @@ -49,7 +43,7 @@ const perform = dispatch => ({ requestDownloadUpgrade: () => dispatch(doDownloadUpgradeRequested()), updatePreferences: () => dispatch(doGetAndPopulatePreferences()), getWalletSyncPref: () => dispatch(doGetWalletSyncPreference()), - syncSubscribe: () => dispatch(doSyncSubscribe()), + syncLoop: noInterval => dispatch(doSyncLoop(noInterval)), setReferrer: (referrer, doClaim) => dispatch(doUserSetReferrer(referrer, doClaim)), }); diff --git a/ui/component/app/view.jsx b/ui/component/app/view.jsx index 370c1fd2f..75e8bfd00 100644 --- a/ui/component/app/view.jsx +++ b/ui/component/app/view.jsx @@ -74,10 +74,9 @@ type Props = { syncEnabled: boolean, rewards: Array, setReferrer: (string, boolean) => void, - analyticsTagSync: () => void, isAuthenticated: boolean, socketConnect: () => void, - syncSubscribe: () => void, + syncLoop: (?boolean) => void, syncEnabled: boolean, currentModal: any, syncFatalError: boolean, @@ -104,7 +103,7 @@ function App(props: Props) { rewards, setReferrer, isAuthenticated, - syncSubscribe, + syncLoop, currentModal, syncFatalError, } = props; @@ -147,6 +146,7 @@ function App(props: Props) { function handleAnalyticsDismiss() { setShowAnalyticsNag(false); } + // @endif useEffect(() => { if (userId) { @@ -270,11 +270,20 @@ function App(props: Props) { // 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) { // In case we are syncing. - syncSubscribe(); + syncLoop(); + // @if TARGET='web' + window.addEventListener('focus', syncLoopWithoutInterval); + // @endif } - }, [readyForSync, hasVerifiedEmail, syncSubscribe]); + // @if TARGET='web' + return () => { + window.removeEventListener('focus', syncLoopWithoutInterval); + }; + // @endif + }, [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 diff --git a/ui/redux/actions/app.js b/ui/redux/actions/app.js index f490b5f19..49c0926a7 100644 --- a/ui/redux/actions/app.js +++ b/ui/redux/actions/app.js @@ -49,7 +49,7 @@ import { import { selectDaemonSettings, makeSelectClientSetting } from 'redux/selectors/settings'; import { selectUser, selectUserVerifiedEmail } from 'redux/selectors/user'; // import { selectDaemonSettings } from 'redux/selectors/settings'; -import { doSyncSubscribe, doSetPrefsReady } from 'redux/actions/sync'; +import { doSyncLoop, doSetPrefsReady } from 'redux/actions/sync'; import { doAuthenticate } from 'redux/actions/user'; import { lbrySettings as config, version as appVersion } from 'package.json'; import analytics, { SHARE_INTERNAL } from 'analytics'; @@ -679,7 +679,7 @@ export function doHandleSyncComplete(error, hasNewData) { } export function doSyncWithPreferences() { - return dispatch => dispatch(doSyncSubscribe()); + return dispatch => dispatch(doSyncLoop()); } export function doToggleInterestedInYoutubeSync() { diff --git a/ui/redux/actions/settings.js b/ui/redux/actions/settings.js index df4f3b728..7bde002d8 100644 --- a/ui/redux/actions/settings.js +++ b/ui/redux/actions/settings.js @@ -5,7 +5,7 @@ import analytics from 'analytics'; import SUPPORTED_LANGUAGES from 'constants/supported_languages'; import { launcher } from 'util/autoLaunch'; import { makeSelectClientSetting } from 'redux/selectors/settings'; -import { doGetSyncDesktop, doSyncUnsubscribe, doSetSyncLock } from 'redux/actions/sync'; +import { doSyncLoop, doSyncUnsubscribe, doSetSyncLock } from 'redux/actions/sync'; import { doAlertWaitingForSync, doGetAndPopulatePreferences } from 'redux/actions/app'; import { selectPrefsReady } from 'redux/selectors/sync'; import { Lbryio } from 'lbryinc'; @@ -246,7 +246,7 @@ export function doEnterSettingsPage() { } dispatch(doSyncUnsubscribe()); if (syncEnabled && hasVerifiedEmail) { - await dispatch(doGetSyncDesktop()); + await dispatch(doSyncLoop(true)); } else { await dispatch(doGetAndPopulatePreferences()); } @@ -263,7 +263,7 @@ export function doExitSettingsPage() { } dispatch(doSetSyncLock(false)); dispatch(doPushSettingsToPrefs()); - // syncSubscribe is restarted in store.js sharedStateCB if necessary + // syncLoop is restarted in store.js sharedStateCB if necessary }; } diff --git a/ui/redux/actions/sync.js b/ui/redux/actions/sync.js index 3a482806a..57757cf29 100644 --- a/ui/redux/actions/sync.js +++ b/ui/redux/actions/sync.js @@ -103,9 +103,9 @@ export const doGetSyncDesktop = (cb?, password) => (dispatch, getState) => { }); }; -export function doSyncSubscribe() { +export function doSyncLoop(noInterval) { return (dispatch, getState) => { - if (syncTimer) clearInterval(syncTimer); + if (!noInterval && syncTimer) clearInterval(syncTimer); const state = getState(); const hasVerifiedEmail = selectUserVerifiedEmail(state); const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state); @@ -113,14 +113,16 @@ export function doSyncSubscribe() { if (hasVerifiedEmail && syncEnabled && !syncLocked) { dispatch(doGetSyncDesktop((error, hasNewData) => dispatch(doHandleSyncComplete(error, hasNewData)))); dispatch(doAnalyticsTagSync()); - syncTimer = setInterval(() => { - const state = getState(); - const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state); - if (syncEnabled) { - dispatch(doGetSyncDesktop((error, hasNewData) => dispatch(doHandleSyncComplete(error, hasNewData)))); - dispatch(doAnalyticsTagSync()); - } - }, SYNC_INTERVAL); + if (!noInterval) { + syncTimer = setInterval(() => { + const state = getState(); + const syncEnabled = makeSelectClientSetting(SETTINGS.ENABLE_SYNC)(state); + if (syncEnabled) { + dispatch(doGetSyncDesktop((error, hasNewData) => dispatch(doHandleSyncComplete(error, hasNewData)))); + dispatch(doAnalyticsTagSync()); + } + }, SYNC_INTERVAL); + } } }; } diff --git a/ui/store.js b/ui/store.js index 440cb2f0f..71edffa00 100644 --- a/ui/store.js +++ b/ui/store.js @@ -10,7 +10,7 @@ import { createMemoryHistory, createBrowserHistory } from 'history'; import { routerMiddleware } from 'connected-react-router'; import createRootReducer from './reducers'; import { Lbry, buildSharedStateMiddleware, ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux'; -import { doSyncSubscribe } from 'redux/actions/sync'; +import { doSyncLoop } from 'redux/actions/sync'; import { getAuthToken } from 'util/saved-passwords'; import { generateInitialUrl } from 'util/url'; import { X_LBRY_AUTH_TOKEN } from 'constants/token'; @@ -159,7 +159,7 @@ const sharedStateFilters = { }; const sharedStateCb = ({ dispatch, getState }) => { - dispatch(doSyncSubscribe()); + dispatch(doSyncLoop()); }; const populateAuthTokenHeader = () => {