Skip to content

Commit

Permalink
feat(projects): support scheduled detection and update system. close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Azir-11 authored Nov 17, 2024
1 parent 17d7e52 commit d088f81
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/plugins/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof setInterval> | 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'),
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit d088f81

Please sign in to comment.