|
| 1 | +/** |
| 2 | + * Serial enforcement queue. Every policy enforcement — before export, before |
| 3 | + * print, before a convert/extract, and (later) as files arrive — runs through |
| 4 | + * here, one at a time. The backend rejects concurrent policy runs under load, |
| 5 | + * and a single in-flight run keeps the queue the user sees honest. |
| 6 | + * |
| 7 | + * Jobs carry their {@link EnforcementTrigger} and a status so the UI can show |
| 8 | + * what's pending/running. Input enforcement reuses this contract unchanged: it |
| 9 | + * just submits jobs with `trigger: "input"`. |
| 10 | + */ |
| 11 | +import { useSyncExternalStore } from "react"; |
| 12 | + |
| 13 | +export type EnforcementTrigger = "export" | "print" | "convert" | "input"; |
| 14 | +export type EnforcementStatus = "pending" | "running" | "done" | "failed"; |
| 15 | + |
| 16 | +export interface EnforcementJob { |
| 17 | + id: string; |
| 18 | + /** Human-readable label, e.g. the policy/file name, shown in the queue UI. */ |
| 19 | + label: string; |
| 20 | + trigger: EnforcementTrigger; |
| 21 | + status: EnforcementStatus; |
| 22 | +} |
| 23 | + |
| 24 | +/** How long a finished job lingers in the list before it's dropped. */ |
| 25 | +const DONE_LINGER_MS = 2500; |
| 26 | + |
| 27 | +type Listener = () => void; |
| 28 | +const listeners = new Set<Listener>(); |
| 29 | +let jobs: EnforcementJob[] = []; |
| 30 | +// The tail of the serial chain — each new job runs after this resolves. |
| 31 | +let tail: Promise<unknown> = Promise.resolve(); |
| 32 | +let seq = 0; |
| 33 | + |
| 34 | +function emit() { |
| 35 | + for (const listener of listeners) listener(); |
| 36 | +} |
| 37 | + |
| 38 | +function setStatus(id: string, status: EnforcementStatus) { |
| 39 | + jobs = jobs.map((j) => (j.id === id ? { ...j, status } : j)); |
| 40 | + emit(); |
| 41 | +} |
| 42 | + |
| 43 | +function scheduleRemoval(id: string) { |
| 44 | + setTimeout(() => { |
| 45 | + jobs = jobs.filter((j) => j.id !== id); |
| 46 | + emit(); |
| 47 | + }, DONE_LINGER_MS); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Run `task` after every job queued before it has finished, tracking its status |
| 52 | + * for the UI. The returned promise resolves/rejects with the task's result, so |
| 53 | + * callers can `await runQueued(...)` exactly as they would the bare work. |
| 54 | + */ |
| 55 | +export function runQueued<T>( |
| 56 | + meta: { label: string; trigger: EnforcementTrigger }, |
| 57 | + task: () => Promise<T>, |
| 58 | +): Promise<T> { |
| 59 | + const id = `enf-${++seq}`; |
| 60 | + jobs = [ |
| 61 | + ...jobs, |
| 62 | + { id, label: meta.label, trigger: meta.trigger, status: "pending" }, |
| 63 | + ]; |
| 64 | + emit(); |
| 65 | + |
| 66 | + const run = tail.then(async () => { |
| 67 | + setStatus(id, "running"); |
| 68 | + try { |
| 69 | + const result = await task(); |
| 70 | + setStatus(id, "done"); |
| 71 | + return result; |
| 72 | + } catch (error) { |
| 73 | + setStatus(id, "failed"); |
| 74 | + throw error; |
| 75 | + } finally { |
| 76 | + scheduleRemoval(id); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + // Keep the chain alive when a task rejects so the next job still runs; callers |
| 81 | + // still see the rejection through `run`. |
| 82 | + tail = run.catch(() => {}); |
| 83 | + return run; |
| 84 | +} |
| 85 | + |
| 86 | +export function getQueueJobs(): EnforcementJob[] { |
| 87 | + return jobs; |
| 88 | +} |
| 89 | + |
| 90 | +export function subscribeQueue(listener: Listener): () => void { |
| 91 | + listeners.add(listener); |
| 92 | + return () => { |
| 93 | + listeners.delete(listener); |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +/** React view of the live queue (pending + running + briefly-lingering jobs). */ |
| 98 | +export function useEnforcementQueue(): EnforcementJob[] { |
| 99 | + return useSyncExternalStore(subscribeQueue, getQueueJobs, getQueueJobs); |
| 100 | +} |
0 commit comments