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