Files
UHD_Media_Tool/apps/web/app/components/tasks/live.ts
T

31 lines
891 B
TypeScript

import { useQueryClient } from "@tanstack/react-query";
import { useRef } from "react";
import { qk } from "~/lib/query-keys.ts";
import { useSse } from "~/lib/sse.ts";
/** 引擎本身不发事件;业务事件(outbox → SSE 桥)足以代表任务在动,2 秒节流刷新任务相关查询 */
const TASK_EVENTS = [
"task.created",
"task.status_changed",
"download.status_changed",
"download.threshold",
"organization.changed",
"feed.fetched",
"decision.created",
"decision.updated",
"agent.run_status",
"schedule.changed",
];
export function useLiveTasks() {
const qc = useQueryClient();
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
useSse(TASK_EVENTS, () => {
if (timer.current) return;
timer.current = setTimeout(() => {
timer.current = null;
qc.invalidateQueries({ queryKey: qk.all("engine") });
}, 2000);
});
}