Lock Screen: Fix page reload on iOS (#2898)

This commit is contained in:
Alexander Zinchuk 2023-03-30 18:26:27 -05:00
parent 2bdc5774ab
commit bd21dc05f7
3 changed files with 21 additions and 6 deletions

View File

@ -21,7 +21,7 @@ type RequestStates = {
const HEALTH_CHECK_TIMEOUT = 150; const HEALTH_CHECK_TIMEOUT = 150;
const HEALTH_CHECK_MIN_DELAY = 5 * 1000; // 5 sec const HEALTH_CHECK_MIN_DELAY = 5 * 1000; // 5 sec
let worker: Worker; let worker: Worker | undefined;
const requestStates = new Map<string, RequestStates>(); const requestStates = new Map<string, RequestStates>();
const requestStatesByCallback = new Map<AnyToVoidFunction, RequestStates>(); const requestStatesByCallback = new Map<AnyToVoidFunction, RequestStates>();
const savedLocalDb: LocalDb = { const savedLocalDb: LocalDb = {
@ -55,7 +55,11 @@ export function initApiOnMasterTab(initialArgs: ApiInitialArgs) {
}); });
} }
let updateCallback: OnApiUpdate;
export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) { export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
updateCallback = onUpdate;
if (!isMasterTab) { if (!isMasterTab) {
initApiOnMasterTab(initialArgs); initApiOnMasterTab(initialArgs);
return Promise.resolve(); return Promise.resolve();
@ -212,14 +216,14 @@ export function cancelApiProgress(progressCallback: ApiOnProgress) {
} }
export function cancelApiProgressMaster(messageId: string) { export function cancelApiProgressMaster(messageId: string) {
worker.postMessage({ worker?.postMessage({
type: 'cancelProgress', type: 'cancelProgress',
messageId, messageId,
}); });
} }
function subscribeToWorker(onUpdate: OnApiUpdate) { function subscribeToWorker(onUpdate: OnApiUpdate) {
worker.addEventListener('message', ({ data }: WorkerMessageEvent) => { worker?.addEventListener('message', ({ data }: WorkerMessageEvent) => {
if (data.type === 'update') { if (data.type === 'update') {
onUpdate(data.update); onUpdate(data.update);
} else if (data.type === 'methodResponse') { } else if (data.type === 'methodResponse') {
@ -330,7 +334,7 @@ function makeRequest(message: OriginRequest) {
} }
}); });
worker.postMessage(payload); worker?.postMessage(payload);
return promise; return promise;
} }
@ -360,7 +364,9 @@ async function ensureWorkerPing() {
console.error(err); console.error(err);
if (Date.now() - startedAt >= HEALTH_CHECK_MIN_DELAY) { if (Date.now() - startedAt >= HEALTH_CHECK_MIN_DELAY) {
window.location.reload(); worker?.terminate();
worker = undefined;
updateCallback({ '@type': 'requestInitApi' });
} }
} finally { } finally {
isResolved = true; isResolved = true;

View File

@ -609,6 +609,10 @@ export type ApiUpdateMessageTranslations = {
toLanguageCode: string; toLanguageCode: string;
}; };
export type ApiRequestInitApi = {
'@type': 'requestInitApi';
};
export type ApiUpdate = ( export type ApiUpdate = (
ApiUpdateReady | ApiUpdateSession | ApiUpdateWebAuthTokenFailed | ApiUpdateRequestUserUpdate | ApiUpdateReady | ApiUpdateSession | ApiUpdateWebAuthTokenFailed | ApiUpdateRequestUserUpdate |
ApiUpdateAuthorizationState | ApiUpdateAuthorizationError | ApiUpdateConnectionState | ApiUpdateCurrentUser | ApiUpdateAuthorizationState | ApiUpdateAuthorizationError | ApiUpdateConnectionState | ApiUpdateCurrentUser |
@ -634,7 +638,8 @@ export type ApiUpdate = (
ApiUpdatePhoneCall | ApiUpdatePhoneCallSignalingData | ApiUpdatePhoneCallMediaState | ApiUpdatePhoneCall | ApiUpdatePhoneCallSignalingData | ApiUpdatePhoneCallMediaState |
ApiUpdatePhoneCallConnectionState | ApiUpdateBotMenuButton | ApiUpdateTranscribedAudio | ApiUpdateUserEmojiStatus | ApiUpdatePhoneCallConnectionState | ApiUpdateBotMenuButton | ApiUpdateTranscribedAudio | ApiUpdateUserEmojiStatus |
ApiUpdateMessageExtendedMedia | ApiUpdateConfig | ApiUpdateTopicNotifyExceptions | ApiUpdatePinnedTopic | ApiUpdateMessageExtendedMedia | ApiUpdateConfig | ApiUpdateTopicNotifyExceptions | ApiUpdatePinnedTopic |
ApiUpdatePinnedTopicsOrder | ApiUpdateTopic | ApiUpdateTopics | ApiUpdateRecentEmojiStatuses ApiUpdatePinnedTopicsOrder | ApiUpdateTopic | ApiUpdateTopics | ApiUpdateRecentEmojiStatuses |
ApiRequestInitApi
); );
export type OnApiUpdate = (update: ApiUpdate) => void; export type OnApiUpdate = (update: ApiUpdate) => void;

View File

@ -57,6 +57,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
onUpdateCurrentUser(global, update); onUpdateCurrentUser(global, update);
break; break;
case 'requestInitApi':
actions.initApi();
break;
case 'error': { case 'error': {
if (update.error.message === 'SESSION_REVOKED') { if (update.error.message === 'SESSION_REVOKED') {
actions.signOut({ forceInitApi: true }); actions.signOut({ forceInitApi: true });