diff --git a/src/api/gramjs/worker/provider.ts b/src/api/gramjs/worker/provider.ts index 362aea83d..1138fcb02 100644 --- a/src/api/gramjs/worker/provider.ts +++ b/src/api/gramjs/worker/provider.ts @@ -21,7 +21,7 @@ type RequestStates = { const HEALTH_CHECK_TIMEOUT = 150; const HEALTH_CHECK_MIN_DELAY = 5 * 1000; // 5 sec -let worker: Worker; +let worker: Worker | undefined; const requestStates = new Map(); const requestStatesByCallback = new Map(); const savedLocalDb: LocalDb = { @@ -55,7 +55,11 @@ export function initApiOnMasterTab(initialArgs: ApiInitialArgs) { }); } +let updateCallback: OnApiUpdate; + export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { + updateCallback = onUpdate; + if (!isMasterTab) { initApiOnMasterTab(initialArgs); return Promise.resolve(); @@ -212,14 +216,14 @@ export function cancelApiProgress(progressCallback: ApiOnProgress) { } export function cancelApiProgressMaster(messageId: string) { - worker.postMessage({ + worker?.postMessage({ type: 'cancelProgress', messageId, }); } function subscribeToWorker(onUpdate: OnApiUpdate) { - worker.addEventListener('message', ({ data }: WorkerMessageEvent) => { + worker?.addEventListener('message', ({ data }: WorkerMessageEvent) => { if (data.type === 'update') { onUpdate(data.update); } else if (data.type === 'methodResponse') { @@ -330,7 +334,7 @@ function makeRequest(message: OriginRequest) { } }); - worker.postMessage(payload); + worker?.postMessage(payload); return promise; } @@ -360,7 +364,9 @@ async function ensureWorkerPing() { console.error(err); if (Date.now() - startedAt >= HEALTH_CHECK_MIN_DELAY) { - window.location.reload(); + worker?.terminate(); + worker = undefined; + updateCallback({ '@type': 'requestInitApi' }); } } finally { isResolved = true; diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index 6d2cfe20f..0bcd1ea57 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -609,6 +609,10 @@ export type ApiUpdateMessageTranslations = { toLanguageCode: string; }; +export type ApiRequestInitApi = { + '@type': 'requestInitApi'; +}; + export type ApiUpdate = ( ApiUpdateReady | ApiUpdateSession | ApiUpdateWebAuthTokenFailed | ApiUpdateRequestUserUpdate | ApiUpdateAuthorizationState | ApiUpdateAuthorizationError | ApiUpdateConnectionState | ApiUpdateCurrentUser | @@ -634,7 +638,8 @@ export type ApiUpdate = ( ApiUpdatePhoneCall | ApiUpdatePhoneCallSignalingData | ApiUpdatePhoneCallMediaState | ApiUpdatePhoneCallConnectionState | ApiUpdateBotMenuButton | ApiUpdateTranscribedAudio | ApiUpdateUserEmojiStatus | ApiUpdateMessageExtendedMedia | ApiUpdateConfig | ApiUpdateTopicNotifyExceptions | ApiUpdatePinnedTopic | - ApiUpdatePinnedTopicsOrder | ApiUpdateTopic | ApiUpdateTopics | ApiUpdateRecentEmojiStatuses + ApiUpdatePinnedTopicsOrder | ApiUpdateTopic | ApiUpdateTopics | ApiUpdateRecentEmojiStatuses | + ApiRequestInitApi ); export type OnApiUpdate = (update: ApiUpdate) => void; diff --git a/src/global/actions/apiUpdaters/initial.ts b/src/global/actions/apiUpdaters/initial.ts index bf83636d1..58d9e5847 100644 --- a/src/global/actions/apiUpdaters/initial.ts +++ b/src/global/actions/apiUpdaters/initial.ts @@ -57,6 +57,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => { onUpdateCurrentUser(global, update); break; + case 'requestInitApi': + actions.initApi(); + break; + case 'error': { if (update.error.message === 'SESSION_REVOKED') { actions.signOut({ forceInitApi: true });