From 0fe60bf5339118ed0c31513f51a6c2006327dd1a Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 30 Mar 2024 22:42:38 +0100 Subject: [PATCH] Chat: Fix open new chat via t.me link (#4446) --- src/api/gramjs/worker/connector.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/api/gramjs/worker/connector.ts b/src/api/gramjs/worker/connector.ts index ddfac7aa6..f1d6fa8a3 100644 --- a/src/api/gramjs/worker/connector.ts +++ b/src/api/gramjs/worker/connector.ts @@ -8,7 +8,7 @@ import type { OriginRequest, ThenArg, WorkerMessageEvent } from './types'; import { DATA_BROADCAST_CHANNEL_NAME, DEBUG } from '../../../config'; import { logDebugMessage } from '../../../util/debugConsole'; import Deferred from '../../../util/Deferred'; -import { getCurrentTabId, isCurrentTabMaster } from '../../../util/establishMultitabRole'; +import { getCurrentTabId, subscribeToMasterChange } from '../../../util/establishMultitabRole'; import generateUniqueId from '../../../util/generateUniqueId'; import { pause } from '../../../util/schedulers'; import { IS_MULTITAB_SUPPORTED } from '../../../util/windowEnvironment'; @@ -39,6 +39,11 @@ const savedLocalDb: LocalDb = { channelPtsById: {}, }; +let isMasterTab = true; +subscribeToMasterChange((isMasterTabNew) => { + isMasterTab = isMasterTabNew; +}); + const channel = IS_MULTITAB_SUPPORTED ? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel : undefined; @@ -62,7 +67,7 @@ let isInited = false; export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { updateCallback = onUpdate; - if (!isCurrentTabMaster()) { + if (!isMasterTab) { initApiOnMasterTab(initialArgs); return Promise.resolve(); } @@ -173,14 +178,14 @@ export function callApiLocal(fnName: T, ...args: Method } export function callApi(fnName: T, ...args: MethodArgs) { - if (!isInited && isCurrentTabMaster()) { + if (!isInited && isMasterTab) { const deferred = new Deferred(); apiRequestsQueue.push({ fnName, args, deferred }); return deferred.promise as MethodResponse; } - const promise = isCurrentTabMaster() ? makeRequest({ + const promise = isMasterTab ? makeRequest({ type: 'callMethod', name: fnName, args, @@ -223,7 +228,7 @@ export function cancelApiProgress(progressCallback: ApiOnProgress) { return; } - if (isCurrentTabMaster()) { + if (isMasterTab) { cancelApiProgressMaster(messageId); } else { if (!channel) return;