mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-20 13:53:32 +08:00
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:
parent
f10396e5bf
commit
e9ae8c93ae
@ -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 })
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user