task-82: 为进度响应 Map 增加 TTL 清理与最大条目数

新增纯 TS 进度响应缓存 progress-response-cache:写入后 ttlMs 过期、
条目数超 maxEntries 驱逐最旧、读取/枚举/清理惰性移除过期条目,时钟
可注入以便测试 TTL 边界。set 对非法 key fail-fast 抛错,时钟故障时
零部分写入,clear 释放全部条目。useTaskProgressLoop 新增可选
progressCache 选项,每轮响应快照写入有界缓存(默认不启用)。
This commit is contained in:
2026-08-30 22:42:08 +08:00
parent c19f59bb9e
commit 83b93eb39b
3 changed files with 361 additions and 0 deletions
@@ -5,6 +5,10 @@ import {
createTaskPollingBaseline,
type TaskPollingBaseline,
} from '@/shared/task-polling-baseline'
import {
createProgressResponseCache,
type ProgressResponseCache,
} from '@/shared/progress-response-cache'
/**
* 通用任务进度轮询组合式函数。
@@ -56,6 +60,12 @@ export interface TaskProgressLoopOptions<TDetail> {
* 响应体大小与缓存内存占用。
*/
onBaseline?: (baseline: TaskPollingBaseline) => void
/**
* 可选进度响应缓存(Task 82):传入后每轮响应快照按 taskId 写入
* TTL+最大条目数的有界缓存(读取时惰性清理过期条目),供页面在
* 轮询间隙复用最近一次进度快照。
*/
progressCache?: ProgressResponseCache<TDetail>
}
export interface TaskProgressLoopHandle<TDetail> {
@@ -167,6 +177,7 @@ export function useTaskProgressLoop<TDetail>(
} catch {
/* onUpdate 抛错不应中断本轮 */
}
options.progressCache?.set(id, detail)
if (status) {
nextStatuses[id] = status
if (isTerminal(status)) {