Skip to content

Commit 18d73e5

Browse files
Azir-11honghuangdc
authored andcommitted
feat(projects): support scheduled detection and update system. close #657 (#669)
1 parent 4966ddc commit 18d73e5

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/plugins/app.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ 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
}
@@ -63,7 +69,28 @@ export function setupAppVersionNotification() {
6369
isShow = false;
6470
}
6571
});
66-
});
72+
};
73+
74+
const startUpdateInterval = () => {
75+
if (updateInterval) {
76+
clearInterval(updateInterval);
77+
}
78+
updateInterval = setInterval(checkForUpdates, UPDATE_CHECK_INTERVAL);
79+
};
80+
81+
// If updates should be checked, set up the visibility change listener and start the update interval
82+
if (shouldCheckForUpdates) {
83+
// Check for updates when the document is visible
84+
document.addEventListener('visibilitychange', () => {
85+
if (document.visibilityState === 'visible') {
86+
checkForUpdates();
87+
startUpdateInterval();
88+
}
89+
});
90+
91+
// Start the update interval
92+
startUpdateInterval();
93+
}
6794
}
6895

6996
async function getHtmlBuildTime() {

0 commit comments

Comments
 (0)