121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
import { SvelteMap, SvelteSet } from "svelte/reactivity";
|
|
import type { QueryClient } from "@tanstack/svelte-query";
|
|
import { apiErrorMessage, playgroundApi } from "$lib/api";
|
|
import type { Job } from "$lib/comfy/types";
|
|
import { showToast } from "$lib/toast";
|
|
import { t } from "$lib/i18n.svelte";
|
|
|
|
/**
|
|
* 撤销删除 (spec-v2 §四.4 删除两级制 · HEL-654 D 批): deleting a single
|
|
* take/task never opens a dialog. The row leaves the view right away, a
|
|
* 5-second Toast offers 撤销, and the backend DELETE is only sent when
|
|
* that window closes without an undo — the backend removes result files,
|
|
* so an undo after the call would be impossible. Leaving the page (SPA
|
|
* route change or hard unload) flushes pending deletes instead of
|
|
* dropping them.
|
|
*/
|
|
|
|
export interface TakeDeleteHandlers {
|
|
/** Localized toast message for this take. */
|
|
message: (job: Job) => string;
|
|
/** Undo: the row comes back — reselect / replay its entrance here. */
|
|
onUndo?: (job: Job) => void;
|
|
/** Called after the delete really hit the backend. */
|
|
onCommitted?: (job: Job) => void;
|
|
}
|
|
|
|
export interface TakeDeleteQueue {
|
|
/** Ids whose rows should be hidden (commit pending). */
|
|
readonly pending: SvelteSet<number>;
|
|
request: (job: Job, handlers: TakeDeleteHandlers) => void;
|
|
destroy: () => void;
|
|
}
|
|
|
|
const UNDO_MS = 5000;
|
|
|
|
export function createTakeDeleteQueue(client: QueryClient): TakeDeleteQueue {
|
|
const pending = new SvelteSet<number>();
|
|
const entries = new SvelteMap<
|
|
number,
|
|
{
|
|
job: Job;
|
|
handlers: TakeDeleteHandlers;
|
|
timer: ReturnType<typeof setTimeout>;
|
|
}
|
|
>();
|
|
let destroyed = false;
|
|
|
|
async function invalidate() {
|
|
await Promise.allSettled([
|
|
client.invalidateQueries({ queryKey: ["playground-jobs"] }),
|
|
client.invalidateQueries({ queryKey: ["jobs"] }),
|
|
]);
|
|
}
|
|
|
|
async function commit(id: number) {
|
|
const entry = entries.get(id);
|
|
if (!entry) return;
|
|
entries.delete(id);
|
|
try {
|
|
await playgroundApi.deleteJob(id);
|
|
pending.delete(id);
|
|
entry.handlers.onCommitted?.(entry.job);
|
|
} catch (err) {
|
|
// Delete failed: the take stays — bring the row back honestly.
|
|
pending.delete(id);
|
|
showToast(
|
|
t("studio.deleteFailedToast", { detail: apiErrorMessage(err) }),
|
|
"error",
|
|
);
|
|
}
|
|
await invalidate();
|
|
}
|
|
|
|
/** Hard unload (tab close / external nav): fire-and-forget with keepalive. */
|
|
function onPageHide() {
|
|
for (const [id] of entries) {
|
|
void fetch(`/api/playground/jobs/${id}`, {
|
|
method: "DELETE",
|
|
keepalive: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
function destroy() {
|
|
if (destroyed) return;
|
|
destroyed = true;
|
|
if (inBrowser) window.removeEventListener("pagehide", onPageHide);
|
|
// SPA route change: the JS context survives, normal commits are fine.
|
|
for (const [id] of entries) {
|
|
const entry = entries.get(id)!;
|
|
clearTimeout(entry.timer);
|
|
void commit(id);
|
|
}
|
|
}
|
|
|
|
const inBrowser = typeof window !== "undefined";
|
|
if (inBrowser) window.addEventListener("pagehide", onPageHide);
|
|
|
|
return {
|
|
pending,
|
|
request(job, handlers) {
|
|
if (destroyed || pending.has(job.id)) return;
|
|
pending.add(job.id);
|
|
const timer = setTimeout(() => void commit(job.id), UNDO_MS);
|
|
entries.set(job.id, { job, handlers, timer });
|
|
showToast(handlers.message(job), "info", UNDO_MS, {
|
|
label: t("toast.undo"),
|
|
run: () => {
|
|
const e = entries.get(job.id);
|
|
if (!e) return;
|
|
clearTimeout(e.timer);
|
|
entries.delete(job.id);
|
|
pending.delete(job.id);
|
|
handlers.onUndo?.(job);
|
|
},
|
|
});
|
|
},
|
|
destroy,
|
|
};
|
|
}
|