Skip to content

Commit 133dc78

Browse files
committed
Add periodic polling for extension updates
- when user stays on 'home' should check for new version every 5 minutes └── apps └── editor └── src └── views └── prompt └── frontend ├── Home │ ├── Home.tsx │ └── hooks │ └── use-update-check.ts └── Prompt.tsx
1 parent 8470f4e commit 133dc78

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

‎apps/editor/src/views/prompt/frontend/Home/hooks/use-update-check.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ type UpdateInfo = {
66
current_version_age_days?: number
77
}
88

9-
let has_checked = false
109
let global_update_info: UpdateInfo = { is_available: false }
10+
let last_checked_time = 0
11+
const CHECK_INTERVAL = 5 * 60 * 1000
1112

1213
const is_newer = (latest: string, current: string) => {
1314
const l = latest.split('.').map(Number)
@@ -25,16 +26,24 @@ export const use_update_check = (current_version: string, is_active: boolean) =>
2526
const [update_info, set_update_info] = useState<UpdateInfo>(global_update_info)
2627

2728
useEffect(() => {
28-
if (!is_active || !current_version || has_checked) {
29-
if (has_checked) {
30-
set_update_info(global_update_info)
31-
}
29+
if (!is_active || !current_version) {
3230
return
3331
}
3432
let is_mounted = true
35-
has_checked = true
33+
let timeout_id: ReturnType<typeof setTimeout>
3634

3735
const check = async () => {
36+
const now = Date.now()
37+
if (last_checked_time !== 0 && now - last_checked_time < CHECK_INTERVAL) {
38+
if (is_mounted) {
39+
set_update_info(global_update_info)
40+
timeout_id = setTimeout(check, CHECK_INTERVAL - (now - last_checked_time))
41+
}
42+
return
43+
}
44+
45+
last_checked_time = Date.now()
46+
3847
try {
3948
let page = 1
4049
while (page <= 10) {
@@ -83,12 +92,17 @@ export const use_update_check = (current_version: string, is_active: boolean) =>
8392
} catch (e) {
8493
console.error('Failed to check for updates:', e)
8594
}
95+
96+
if (is_mounted) {
97+
timeout_id = setTimeout(check, CHECK_INTERVAL)
98+
}
8699
}
87100

88101
check()
89102

90103
return () => {
91104
is_mounted = false
105+
clearTimeout(timeout_id)
92106
}
93107
}, [current_version, is_active])
94108

0 commit comments

Comments
 (0)