Files
ComfyUI_frontend/src/platform/remote/comfyui/useQueuePolling.ts
Johnpaul Chiwetelu 78fe639540 feat: periodically re-poll queue progress state (#9136)
## Summary
- Add `useQueuePolling` composable that polls `queueStore.update()`
every 5s while jobs are active
- Calls `update()` immediately on creation so the UI is current after
page reload
- Uses `useIntervalFn` + `watch` pattern (same as `assetDownloadStore`)
to pause/resume based on `activeJobsCount`

## Related Issue
- Related to #8136

## QA
- Queue a prompt, reload page mid-execution, verify queue UI updates
every ~5s
- Verify polling stops when queue empties
- Verify polling resumes when new jobs are queued

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-9136-feat-periodically-re-poll-queue-progress-state-3106d73d36508119a32fc5b9c8eda21c)
by [Unito](https://www.unito.io)
2026-02-23 21:27:24 -08:00

48 lines
1.2 KiB
TypeScript

import { useTimeoutFn } from '@vueuse/core'
import { ref, watch } from 'vue'
import { useQueueStore } from '@/stores/queueStore'
const BASE_INTERVAL_MS = 8_000
const MAX_INTERVAL_MS = 32_000
const BACKOFF_MULTIPLIER = 1.5
export function useQueuePolling() {
const queueStore = useQueueStore()
const delay = ref(BASE_INTERVAL_MS)
const { start, stop } = useTimeoutFn(
() => {
if (queueStore.activeJobsCount !== 1 || queueStore.isLoading) return
delay.value = Math.min(delay.value * BACKOFF_MULTIPLIER, MAX_INTERVAL_MS)
void queueStore.update()
},
delay,
{ immediate: false }
)
function scheduleNextPoll() {
if (queueStore.activeJobsCount === 1 && !queueStore.isLoading) start()
else stop()
}
watch(
() => queueStore.activeJobsCount,
() => {
delay.value = BASE_INTERVAL_MS
scheduleNextPoll()
},
{ immediate: true }
)
// Reschedule after any update completes (whether from polling or
// WebSocket events) to avoid redundant requests.
watch(
() => queueStore.isLoading,
(loading, wasLoading) => {
if (wasLoading && !loading) scheduleNextPoll()
},
{ flush: 'sync' }
)
}