fix: refresh active purge progress without idle delay

This commit is contained in:
WuYongJie 2026-10-09 15:48:01 +08:00
parent 477ff64b07
commit c27d920527
3 changed files with 15 additions and 3 deletions

View File

@ -15,9 +15,13 @@ test('刷新后重新接管的清理任务也显示完成结果', async () => {
.mockImplementation(() => response({ jobs: [{ ...job, status: 'completed', processedFiles: 2 }] }))
await act(async () => { render(<Fixture />) })
expect(screen.getByText('正在清理 1 条素材')).toBeInTheDocument()
await act(async () => { jest.advanceTimersByTime(3000) })
await act(async () => { jest.advanceTimersByTime(499) })
expect(fetch).toHaveBeenCalledTimes(1)
await act(async () => { jest.advanceTimersByTime(1) })
expect((fetch as jest.Mock).mock.calls[1][0]).toContain(`id=${id}`)
expect(screen.getByText('已彻底删除 1 条')).toBeInTheDocument()
await act(async () => { jest.advanceTimersByTime(2500) })
expect(fetch).toHaveBeenCalledTimes(2)
})
test('后台进度轮询成功不能抹掉人工重试失败的提示', async () => {

View File

@ -52,10 +52,16 @@ export function useMaterialPurgeJobs() {
useEffect(() => {
mounted.current = true
void refresh()
const timer = setInterval(() => { if (!document.hidden) void refresh() }, 3000)
return () => { mounted.current = false; clearInterval(timer) }
return () => { mounted.current = false }
}, [refresh])
const hasPending = jobs.some(job => job.status === 'queued' || job.status === 'processing')
const pollMs = hasPending && !error ? 500 : 3000
useEffect(() => {
const timer = setInterval(() => { if (!document.hidden) void refresh() }, pollMs)
return () => clearInterval(timer)
}, [refresh, pollMs])
const retry = async (id: string) => {
setRetrying(id)
setRetryError(null)

View File

@ -8,6 +8,8 @@
提交后立即唤醒已经启动的清理 worker。空队列检查与入队交错时保留唤醒信号,每轮满 20 条立即接着处理下一轮;5 秒定时器只用于恢复和到期重试。没有把排队或未完成的物理清理显示成成功。
前端只在任务进行中且进度接口正常时每 500ms 更新状态,完成后及接口报错时回到 3 秒间隔;页面隐藏时停止请求,已有请求未完成时不重叠请求。完成提示仍由服务端真实完成状态驱动,避免后台已经完成但界面固定多等 3 秒。
## 验证
- 21 个相关 Jest 测试,包括入队与空队列检查交错时不会丢失唤醒。