From 55dbc323aad2b3072f8969430885b2b5fbeb367d Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 9 Apr 2023 04:02:53 +0200 Subject: [PATCH] Fix online status (#2948) --- src/global/actions/ui/misc.ts | 38 +++++++++++++++++++++-------------- src/global/initialState.ts | 1 - src/global/types.ts | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/global/actions/ui/misc.ts b/src/global/actions/ui/misc.ts index 441a40d28..f16d3dfc9 100644 --- a/src/global/actions/ui/misc.ts +++ b/src/global/actions/ui/misc.ts @@ -611,18 +611,6 @@ const NOTIFICATION_INTERVAL = 500; addActionHandler('onTabFocusChange', (global, actions, payload): ActionReturnType => { const { isBlurred, tabId = getCurrentTabId() } = payload; - if (!isBlurred) { - actions.updateIsOnline(true); - } - - const blurredTabTokens = unique(isBlurred - ? [...global.blurredTabTokens, tabId] - : global.blurredTabTokens.filter((t) => t !== tabId)); - - if (blurredTabTokens.length === getAllMultitabTokens().length) { - actions.updateIsOnline(false); - } - if (isBlurred) { if (notificationInterval) clearInterval(notificationInterval); @@ -636,9 +624,12 @@ addActionHandler('onTabFocusChange', (global, actions, payload): ActionReturnTyp notificationInterval = undefined; } + global = updateTabState(global, { + isBlurred, + }, tabId); + return { ...global, - blurredTabTokens, initialUnreadNotifications: isBlurred ? getAllNotificationsCount() : undefined, }; }); @@ -689,14 +680,31 @@ addActionHandler('updatePageTitle', (global, actions, payload): ActionReturnType }); let prevIsScreenLocked: boolean | undefined; +let prevBlurredTabsCount: number = 0; +let onlineTimeout: number | undefined; +const ONLINE_TIMEOUT = 100; addCallback((global: GlobalState) => { // eslint-disable-next-line eslint-multitab-tt/no-getactions-in-actions - const { updatePageTitle } = getActions(); + const { updatePageTitle, updateIsOnline } = getActions(); const isLockedUpdated = global.passcode.isScreenLocked !== prevIsScreenLocked; - prevIsScreenLocked = global.passcode.isScreenLocked; + const blurredTabsCount = Object.values(global.byTabId).filter((l) => l.isBlurred).length; + const isMasterTab = selectTabState(global, getCurrentTabId()).isMasterTab; if (isLockedUpdated) { updatePageTitle(); } + + if (blurredTabsCount !== prevBlurredTabsCount && isMasterTab) { + if (onlineTimeout) clearTimeout(onlineTimeout); + + onlineTimeout = window.setTimeout(() => { + global = getGlobal(); + const newBlurredTabsCount = Object.values(global.byTabId).filter((l) => l.isBlurred).length; + updateIsOnline(newBlurredTabsCount !== getAllMultitabTokens().length); + }, ONLINE_TIMEOUT); + } + + prevIsScreenLocked = global.passcode.isScreenLocked; + prevBlurredTabsCount = blurredTabsCount; }); diff --git a/src/global/initialState.ts b/src/global/initialState.ts index 1b9e5d2f9..5ba8256d1 100644 --- a/src/global/initialState.ts +++ b/src/global/initialState.ts @@ -11,7 +11,6 @@ import { IS_IOS, IS_MAC_OS } from '../util/windowEnvironment'; export const INITIAL_GLOBAL_STATE: GlobalState = { attachMenu: { bots: {} }, - blurredTabTokens: [], passcode: {}, twoFaSettings: {}, isUpdateAvailable: false, diff --git a/src/global/types.ts b/src/global/types.ts index 0fc9b6e2a..edfebf16c 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -176,6 +176,7 @@ export type ChatRequestedTranslations = { export type TabState = { id: number; + isBlurred?: boolean; isMasterTab: boolean; isInactive?: boolean; inviteHash?: string; @@ -552,7 +553,6 @@ export type GlobalState = { isSyncing?: boolean; isUpdateAvailable?: boolean; lastSyncTime?: number; - blurredTabTokens: number[]; leftColumnWidth?: number; lastIsChatInfoShown?: boolean; initialUnreadNotifications?: number;