fix(desktop): haha-oauth store — logout 停 polling + recursive setTimeout 避免 overlap

Code review follow-ups on f10396e:
- logout() 首行调 stopPolling, 避免登出后 poll timer 还在跑
- setInterval 改为 recursive setTimeout, fetchStatus >2s 时不会产生 parallel 请求

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-04-17 20:26:47 +08:00
parent f10396e5bf
commit e9ae8c93ae

View File

@ -19,7 +19,7 @@ type HahaOAuthState = {
}
export const useHahaOAuthStore = create<HahaOAuthState>((set, get) => {
let pollTimer: ReturnType<typeof setInterval> | null = null
let pollTimer: ReturnType<typeof setTimeout> | null = null
return {
status: null,
@ -53,6 +53,7 @@ export const useHahaOAuthStore = create<HahaOAuthState>((set, get) => {
},
logout: async () => {
get().stopPolling()
set({ isLoading: true })
try {
await hahaOAuthApi.logout()
@ -69,18 +70,26 @@ export const useHahaOAuthStore = create<HahaOAuthState>((set, get) => {
startPolling: () => {
if (pollTimer) return
set({ isPolling: true })
pollTimer = setInterval(async () => {
await get().fetchStatus()
const cur = get().status
if (cur && cur.loggedIn) {
get().stopPolling()
}
}, POLL_INTERVAL_MS)
const scheduleNext = () => {
pollTimer = setTimeout(async () => {
await get().fetchStatus()
const cur = get().status
if (cur && cur.loggedIn) {
get().stopPolling()
return
}
if (get().isPolling) {
scheduleNext()
}
}, POLL_INTERVAL_MS)
}
scheduleNext()
},
stopPolling: () => {
if (pollTimer) {
clearInterval(pollTimer)
clearTimeout(pollTimer)
pollTimer = null
}
set({ isPolling: false })