diff --git a/src/components/main/Main.tsx b/src/components/main/Main.tsx index f7d1078f6..6a93e65bc 100644 --- a/src/components/main/Main.tsx +++ b/src/components/main/Main.tsx @@ -6,7 +6,7 @@ import { getActions, getGlobal, withGlobal } from '../../global'; import type { AnimationLevel, LangCode } from '../../types'; import type { - ApiChat, ApiMessage, ApiUpdateAuthorizationStateType, ApiUpdateConnectionStateType, ApiUser, + ApiChat, ApiMessage, ApiUser, } from '../../api/types'; import type { ApiLimitTypeWithModal, GlobalState } from '../../global/types'; @@ -79,8 +79,6 @@ import './Main.scss'; type StateProps = { chat?: ApiChat; - connectionState?: ApiUpdateConnectionStateType; - authState?: ApiUpdateAuthorizationStateType; lastSyncTime?: number; isLeftColumnOpen: boolean; isRightColumnOpen: boolean; @@ -131,8 +129,6 @@ let notificationInterval: number | undefined; let DEBUG_isLogged = false; const Main: FC = ({ - connectionState, - authState, lastSyncTime, isLeftColumnOpen, isRightColumnOpen, @@ -174,7 +170,6 @@ const Main: FC = ({ deleteFolderDialogId, }) => { const { - sync, loadAnimatedEmojis, loadNotificationSettings, loadNotificationExceptions, @@ -206,12 +201,6 @@ const Main: FC = ({ console.log('>>> RENDER MAIN'); } - useEffect(() => { - if (connectionState === 'connectionStateReady' && authState === 'authorizationStateReady') { - sync(); - } - }, [connectionState, authState, sync]); - useInterval(checkAppVersion, APP_OUTDATED_TIMEOUT_MS, true); // Initial API calls @@ -502,7 +491,6 @@ export default memo(withGlobal( animationLevel, language, wasTimeFormatSetManually, }, }, - connectionState, botTrustRequest, requestedAttachBotInstall, requestedAttachBotInChat, @@ -510,7 +498,6 @@ export default memo(withGlobal( urlAuth, webApp, safeLinkModalUrl, - authState, lastSyncTime, openedStickerSetShortName, openedCustomEmojiSetIds, @@ -526,8 +513,6 @@ export default memo(withGlobal( const currentUser = global.currentUserId ? selectUser(global, global.currentUserId) : undefined; return { - connectionState, - authState, lastSyncTime, isLeftColumnOpen: global.isLeftColumnShown, isRightColumnOpen: selectIsRightColumnShown(global), diff --git a/src/global/init.ts b/src/global/init.ts index 3d8967ed8..6e22735ee 100644 --- a/src/global/init.ts +++ b/src/global/init.ts @@ -6,6 +6,7 @@ import { initCache, loadCache } from './cache'; import { cloneDeep } from '../util/iteratees'; import { updatePasscodeSettings } from './reducers'; import { clearStoredSession } from '../util/sessions'; +import './sync'; initCache(); diff --git a/src/global/sync.ts b/src/global/sync.ts new file mode 100644 index 000000000..10f99204e --- /dev/null +++ b/src/global/sync.ts @@ -0,0 +1,15 @@ +import { getActions } from '.'; +import { addCallback, getGlobal } from '../lib/teact/teactn'; +import type { GlobalState } from './types'; + +let previousGlobal = getGlobal(); +// RAF can be unreliable when device goes into sleep mode, so sync logic is handled outside any component +addCallback((global: GlobalState) => { + const { connectionState, authState } = global; + if (previousGlobal.connectionState === connectionState && previousGlobal.authState === authState) return; + if (connectionState === 'connectionStateReady' && authState === 'authorizationStateReady') { + getActions().sync(); + } + + previousGlobal = global; +});