diff --git a/apps/api/src/app/workers/task-advancer.ts b/apps/api/src/app/workers/task-advancer.ts index 01bdde3..2a37b5f 100644 --- a/apps/api/src/app/workers/task-advancer.ts +++ b/apps/api/src/app/workers/task-advancer.ts @@ -33,10 +33,26 @@ export async function advanceOnce(options: AdvancerOptions): Promise { * 抢不到就跳过 —— 那边正推着,这边再推就是两条线同时改一份运行时状态。 */ const ran = await taskMutex.tryRun(`task:${row.id}`, async () => { + /** 这一轮之前它是什么样 —— 变了才广播,不然每两秒给在线浏览器发一次噪声 */ + const before = await container.task.get(row.id as never).catch(() => undefined); for (let tick = 0; tick < (options.maxTicks ?? 3); tick += 1) { try { const outcome = await container.task.tick(row.id as never); advanced += 1; + + /* + * 推进器改了状态就**广播**:任务从在办走到终态、或挂起去等人, + * 都是在线的人该立刻看见的事。 + * 从前只有派发器(事件投递)广播,而任务是被**这里**推到终态的 —— + * 于是「办完的活不会自己挪到已完成」,得刷页面才认(用户实测报的)。 + */ + if (outcome.task.runtime !== before?.runtime || outcome.task.terminal !== before?.terminal) { + container.broadcaster.publish({ kind: 'task', id: row.id, reason: 'advanced' }); + if (outcome.task.workflowId !== undefined) { + container.broadcaster.publish({ kind: 'workflow', id: outcome.task.workflowId, reason: 'task-advanced' }); + } + } + // 进入等待或终结就停手 —— 挂起在等的任务由事件唤醒,不空转 if (outcome.task.runtime !== 'running' || outcome.task.terminal !== undefined) break; if (!outcome.progressed) break; diff --git a/apps/web/src/app/app-shell.tsx b/apps/web/src/app/app-shell.tsx index efd9743..2e0b9bb 100644 --- a/apps/web/src/app/app-shell.tsx +++ b/apps/web/src/app/app-shell.tsx @@ -64,7 +64,7 @@ export function AppShell() { queryFn: () => call<{ messages: Array<{ id: string; read: boolean }> }>(listMessagesRoute), }); const approvals = useQuery({ - queryKey: ['approvals'], + queryKey: queryKeys.approvals, queryFn: () => call<{ approvals: Array<{ state: string }> }>(listApprovalsForMeRoute), }); diff --git a/apps/web/src/app/query.ts b/apps/web/src/app/query.ts index 2e89cbb..8967ba9 100644 --- a/apps/web/src/app/query.ts +++ b/apps/web/src/app/query.ts @@ -20,4 +20,5 @@ export const queryKeys = { tasks: ['desk', 'tasks'] as const, task: (id: string) => ['desk', 'task', id] as const, messages: ['desk', 'messages'] as const, + approvals: ['desk', 'approvals'] as const, }; diff --git a/apps/web/src/app/subscribe.ts b/apps/web/src/app/subscribe.ts index d037440..a2d15dc 100644 --- a/apps/web/src/app/subscribe.ts +++ b/apps/web/src/app/subscribe.ts @@ -8,6 +8,7 @@ * 断线由 `EventSource` 自行重连;重连后整体失效一次,补上断线期间错过的变更。 */ import type { QueryClient } from '@tanstack/react-query'; +import { queryKeys } from './query.ts'; type Touch = { kind: 'task' | 'workflow' | 'staff' | 'system'; id: string; reason: string }; @@ -17,20 +18,30 @@ export function connectStream(queryClient: QueryClient): () => void { source.addEventListener('touch', (event) => { const touch = JSON.parse((event as MessageEvent).data) as Touch; switch (touch.kind) { + /* + * **键名一律取自 `queryKeys`**,不手写字面量:TanStack 的失效按**前缀**匹配, + * 而工作台的键都以 `desk` 打头(`['desk','tasks']`)。从前这里写的是 `['tasks']` —— + * 前缀对不上,一条也匹配不到:推送照发,画面纹丝不动 + * (实测:任务办完了,列表不挪窝、「已完成」也不长出来)。 + */ case 'task': // 工作台三处 + 治理台任务面都以任务为轴,一起失效 - void queryClient.invalidateQueries({ queryKey: ['task', touch.id] }); - void queryClient.invalidateQueries({ queryKey: ['tasks'] }); + void queryClient.invalidateQueries({ queryKey: queryKeys.task(touch.id) }); + void queryClient.invalidateQueries({ queryKey: queryKeys.tasks }); + void queryClient.invalidateQueries({ queryKey: queryKeys.approvals }); void queryClient.invalidateQueries({ queryKey: ['gov', 'tasks'] }); void queryClient.invalidateQueries({ queryKey: ['gov', 'tasklog', touch.id] }); break; case 'workflow': + // 流动了,自己名下的任务与审批都可能变 —— 工作台一并失效 + void queryClient.invalidateQueries({ queryKey: queryKeys.tasks }); + void queryClient.invalidateQueries({ queryKey: queryKeys.approvals }); void queryClient.invalidateQueries({ queryKey: ['gov', 'workflow', touch.id] }); void queryClient.invalidateQueries({ queryKey: ['gov', 'tasks'] }); void queryClient.invalidateQueries({ queryKey: ['gov', 'approvals'] }); break; case 'staff': - void queryClient.invalidateQueries({ queryKey: ['messages'] }); + void queryClient.invalidateQueries({ queryKey: queryKeys.messages }); void queryClient.invalidateQueries({ queryKey: ['gov', 'staff'] }); break; case 'system':