From 571f3bc9535b432e28aa94a02c04645820615a91 Mon Sep 17 00:00:00 2001 From: Azir-11 <2075125282@qq.com> Date: Fri, 15 Nov 2024 22:29:16 +0800 Subject: [PATCH] feat(projects): support scheduled detection and update system. close #657 --- src/plugins/app.ts | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/plugins/app.ts b/src/plugins/app.ts index f2080ef7f..7087d219b 100644 --- a/src/plugins/app.ts +++ b/src/plugins/app.ts @@ -10,26 +10,33 @@ export function setupAppErrorHandle(app: App) { }; } +// Update check interval in milliseconds +const UPDATE_CHECK_INTERVAL = 3 * 60 * 1000; + export function setupAppVersionNotification() { const canAutoUpdateApp = import.meta.env.VITE_AUTOMATICALLY_DETECT_UPDATE === 'Y'; if (!canAutoUpdateApp) return; let isShow = false; + let updateInterval: ReturnType | undefined; - document.addEventListener('visibilitychange', async () => { - const preConditions = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV]; + // Check if updates should be checked + const shouldCheckForUpdates = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV].every(Boolean); - if (!preConditions.every(Boolean)) return; + const checkForUpdates = async () => { + if (!shouldCheckForUpdates) return; const buildTime = await getHtmlBuildTime(); + // If build time hasn't changed, no update is needed if (buildTime === BUILD_TIME) { return; } isShow = true; + // Show update notification const n = window.$notification?.create({ title: $t('system.updateTitle'), content: $t('system.updateContent'), @@ -60,7 +67,28 @@ export function setupAppVersionNotification() { isShow = false; } }); - }); + }; + + const startUpdateInterval = () => { + if (updateInterval) { + clearInterval(updateInterval); + } + updateInterval = setInterval(checkForUpdates, UPDATE_CHECK_INTERVAL); + }; + + // If updates should be checked, set up the visibility change listener and start the update interval + if (shouldCheckForUpdates) { + // Check for updates when the document is visible + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'visible') { + checkForUpdates(); + startUpdateInterval(); + } + }); + + // Start the update interval + startUpdateInterval(); + } } async function getHtmlBuildTime() {