Chat: Fix open new chat via t.me link (#4446)

This commit is contained in:
Alexander Zinchuk 2024-03-30 22:42:38 +01:00
parent aa588c8308
commit 0fe60bf533

View File

@ -8,7 +8,7 @@ import type { OriginRequest, ThenArg, WorkerMessageEvent } from './types';
import { DATA_BROADCAST_CHANNEL_NAME, DEBUG } from '../../../config'; import { DATA_BROADCAST_CHANNEL_NAME, DEBUG } from '../../../config';
import { logDebugMessage } from '../../../util/debugConsole'; import { logDebugMessage } from '../../../util/debugConsole';
import Deferred from '../../../util/Deferred'; import Deferred from '../../../util/Deferred';
import { getCurrentTabId, isCurrentTabMaster } from '../../../util/establishMultitabRole'; import { getCurrentTabId, subscribeToMasterChange } from '../../../util/establishMultitabRole';
import generateUniqueId from '../../../util/generateUniqueId'; import generateUniqueId from '../../../util/generateUniqueId';
import { pause } from '../../../util/schedulers'; import { pause } from '../../../util/schedulers';
import { IS_MULTITAB_SUPPORTED } from '../../../util/windowEnvironment'; import { IS_MULTITAB_SUPPORTED } from '../../../util/windowEnvironment';
@ -39,6 +39,11 @@ const savedLocalDb: LocalDb = {
channelPtsById: {}, channelPtsById: {},
}; };
let isMasterTab = true;
subscribeToMasterChange((isMasterTabNew) => {
isMasterTab = isMasterTabNew;
});
const channel = IS_MULTITAB_SUPPORTED const channel = IS_MULTITAB_SUPPORTED
? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel ? new BroadcastChannel(DATA_BROADCAST_CHANNEL_NAME) as TypedBroadcastChannel
: undefined; : undefined;
@ -62,7 +67,7 @@ let isInited = false;
export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
updateCallback = onUpdate; updateCallback = onUpdate;
if (!isCurrentTabMaster()) { if (!isMasterTab) {
initApiOnMasterTab(initialArgs); initApiOnMasterTab(initialArgs);
return Promise.resolve(); return Promise.resolve();
} }
@ -173,14 +178,14 @@ export function callApiLocal<T extends keyof Methods>(fnName: T, ...args: Method
} }
export function callApi<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>) { export function callApi<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>) {
if (!isInited && isCurrentTabMaster()) { if (!isInited && isMasterTab) {
const deferred = new Deferred(); const deferred = new Deferred();
apiRequestsQueue.push({ fnName, args, deferred }); apiRequestsQueue.push({ fnName, args, deferred });
return deferred.promise as MethodResponse<T>; return deferred.promise as MethodResponse<T>;
} }
const promise = isCurrentTabMaster() ? makeRequest({ const promise = isMasterTab ? makeRequest({
type: 'callMethod', type: 'callMethod',
name: fnName, name: fnName,
args, args,
@ -223,7 +228,7 @@ export function cancelApiProgress(progressCallback: ApiOnProgress) {
return; return;
} }
if (isCurrentTabMaster()) { if (isMasterTab) {
cancelApiProgressMaster(messageId); cancelApiProgressMaster(messageId);
} else { } else {
if (!channel) return; if (!channel) return;