Fix sync after sleep mode (#2060)

This commit is contained in:
Alexander Zinchuk 2022-10-03 16:43:49 +02:00
parent 87062b53ad
commit 403a72312f
3 changed files with 17 additions and 16 deletions

View File

@ -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<StateProps> = ({
connectionState,
authState,
lastSyncTime,
isLeftColumnOpen,
isRightColumnOpen,
@ -174,7 +170,6 @@ const Main: FC<StateProps> = ({
deleteFolderDialogId,
}) => {
const {
sync,
loadAnimatedEmojis,
loadNotificationSettings,
loadNotificationExceptions,
@ -206,12 +201,6 @@ const Main: FC<StateProps> = ({
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),

View File

@ -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();

15
src/global/sync.ts Normal file
View File

@ -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;
});