Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(projects): support scheduled detection and update system. #669

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading