Skip to content

Commit d088f81

Browse files
authored
feat(projects): support scheduled detection and update system. close #657 (#669)
1 parent 17d7e52 commit d088f81

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/plugins/app.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,33 @@ export function setupAppErrorHandle(app: App) {
1010
};
1111
}
1212

13+
// Update check interval in milliseconds
14+
const UPDATE_CHECK_INTERVAL = 3 * 60 * 1000;
15+
1316
export function setupAppVersionNotification() {
1417
const canAutoUpdateApp = import.meta.env.VITE_AUTOMATICALLY_DETECT_UPDATE === 'Y';
1518

1619
if (!canAutoUpdateApp) return;
1720

1821
let isShow = false;
22+
let updateInterval: ReturnType<typeof setInterval> | undefined;
1923

20-
document.addEventListener('visibilitychange', async () => {
21-
const preConditions = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV];
24+
// Check if updates should be checked
25+
const shouldCheckForUpdates = [!isShow, document.visibilityState === 'visible', !import.meta.env.DEV].every(Boolean);
2226

23-
if (!preConditions.every(Boolean)) return;
27+
const checkForUpdates = async () => {
28+
if (!shouldCheckForUpdates) return;
2429

2530
const buildTime = await getHtmlBuildTime();
2631

32+
// If build time hasn't changed, no update is needed
2733
if (buildTime === BUILD_TIME) {
2834
return;
2935
}
3036

3137
isShow = true;
3238

39+
// Show update notification
3340
const n = window.$notification?.create({
3441
title: $t('system.updateTitle'),
3542
content: $t('system.updateContent'),
@@ -60,7 +67,28 @@ export function setupAppVersionNotification() {
6067
isShow = false;
6168
}
6269
});
63-
});
70+
};
71+
72+
const startUpdateInterval = () => {
73+
if (updateInterval) {
74+
clearInterval(updateInterval);
75+
}
76+
updateInterval = setInterval(checkForUpdates, UPDATE_CHECK_INTERVAL);
77+
};
78+
79+
// If updates should be checked, set up the visibility change listener and start the update interval
80+
if (shouldCheckForUpdates) {
81+
// Check for updates when the document is visible
82+
document.addEventListener('visibilitychange', () => {
83+
if (document.visibilityState === 'visible') {
84+
checkForUpdates();
85+
startUpdateInterval();
86+
}
87+
});
88+
89+
// Start the update interval
90+
startUpdateInterval();
91+
}
6492
}
6593

6694
async function getHtmlBuildTime() {

0 commit comments

Comments
 (0)