signal-desktop/ts/setAppLoadingScreenMessage.ts

48 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { LocalizerType } from './types/Util';
2018-03-22 15:52:52 +00:00
const DISPLAY_THRESHOLD = 3000; // milliseconds
const SELECTOR = '.app-loading-screen .message';
2018-03-22 15:52:52 +00:00
let timeout: null | ReturnType<typeof setTimeout>;
let targetString: string;
let didTimeout = false;
const clear = () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
export function setAppLoadingScreenMessage(
loadingText: undefined | string,
i18n: LocalizerType
): () => void {
const message = document.querySelector<HTMLElement>(SELECTOR);
2018-03-22 15:52:52 +00:00
if (!message) {
return clear;
}
targetString = loadingText || i18n('optimizingApplication');
message.innerText = didTimeout ? targetString : i18n('loading');
if (timeout) {
return clear;
2018-03-22 15:52:52 +00:00
}
timeout = setTimeout(() => {
didTimeout = true;
const innerMessage = document.querySelector<HTMLElement>(SELECTOR);
2018-03-22 15:52:52 +00:00
if (!innerMessage) {
return;
}
innerMessage.innerText = targetString;
}, DISPLAY_THRESHOLD);
2018-03-22 15:52:52 +00:00
return clear;
}