From c3a3597b7d48e6ed896b04a31d045a2701e15f53 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 10 Oct 2022 14:37:56 +0200 Subject: [PATCH] Fix calling missing actions too early (#2066) --- src/global/actions/api/sync.ts | 15 ++++++++++++++- src/global/init.ts | 1 - src/global/sync.ts | 15 --------------- src/util/setupServiceWorker.ts | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) delete mode 100644 src/global/sync.ts diff --git a/src/global/actions/api/sync.ts b/src/global/actions/api/sync.ts index a45ae5ba0..f2ef7a783 100644 --- a/src/global/actions/api/sync.ts +++ b/src/global/actions/api/sync.ts @@ -1,10 +1,11 @@ import { addActionHandler, getGlobal, setGlobal, getActions, } from '../../index'; +import { addCallback } from '../../../lib/teact/teactn'; import type { ApiChat, ApiMessage } from '../../../api/types'; import { MAIN_THREAD_ID } from '../../../api/types'; -import type { Thread } from '../../types'; +import type { GlobalState, Thread } from '../../types'; import { DEBUG, MESSAGE_LIST_SLICE, SERVICE_NOTIFICATIONS_USER_ID, @@ -205,3 +206,15 @@ function loadTopMessages(chat: ApiChat) { limit: MESSAGE_LIST_SLICE, }); } + +let previousGlobal: GlobalState | undefined; +// 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; +}); diff --git a/src/global/init.ts b/src/global/init.ts index 6e22735ee..3d8967ed8 100644 --- a/src/global/init.ts +++ b/src/global/init.ts @@ -6,7 +6,6 @@ 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 deleted file mode 100644 index 10f99204e..000000000 --- a/src/global/sync.ts +++ /dev/null @@ -1,15 +0,0 @@ -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; -}); diff --git a/src/util/setupServiceWorker.ts b/src/util/setupServiceWorker.ts index b8608eaa1..8749d0944 100644 --- a/src/util/setupServiceWorker.ts +++ b/src/util/setupServiceWorker.ts @@ -72,7 +72,7 @@ if (IS_SERVICE_WORKER_SUPPORTED) { } if (!IS_IOS && !IS_ANDROID && !IS_TEST) { - getActions().showDialog({ data: { message: 'SERVICE_WORKER_DISABLED', hasErrorKey: true } }); + getActions().showDialog?.({ data: { message: 'SERVICE_WORKER_DISABLED', hasErrorKey: true } }); } } } catch (err) {