Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: drain pending description saves before sign out

Tin1a4487f2e955
diff --git a/apps/web/src/hooks/mutations/use-sign-out.ts b/apps/web/src/hooks/mutations/use-sign-out.ts
index cf6626a78..85e3cf146 100644
--- a/apps/web/src/hooks/mutations/use-sign-out.ts
+++ b/apps/web/src/hooks/mutations/use-sign-out.ts
@@ -2,14 +2,17 @@ import { useMutation } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
import { getIdToken } from "@/fetchers/oauth/get-id-token";
import { descriptionSaveQueue } from "@/lib/description-save-queue";
+import { i18n } from "@/lib/i18n";
import { authClient } from "@/lib/auth-client";
function useSignOut(idpLogoutUrl?: string | null) {
const navigate = useNavigate();
+ const { data: session } = authClient.useSession();
return useMutation({
mutationFn: async () => {
- descriptionSaveQueue.clear();
+ if (!(await descriptionSaveQueue.drain(session?.user.id ?? "")))
+ throw new Error(i18n.t("tasks:detail.editor.saveFailed"));
let idToken: string | null = null;
if (idpLogoutUrl) {
@@ -24,6 +27,7 @@ function useSignOut(idpLogoutUrl?: string | null) {
const result = await authClient.signOut({
fetchOptions: {
onSuccess: () => {
+ descriptionSaveQueue.clear();
if (idpLogoutUrl) {
const redirectUri = `${window.location.origin}/auth/sign-in`;
const url = new URL(idpLogoutUrl);
diff --git a/apps/web/src/lib/description-save-queue.test.ts b/apps/web/src/lib/description-save-queue.test.ts
index 1735638cf..071ebf47e 100644
--- a/apps/web/src/lib/description-save-queue.test.ts
+++ b/apps/web/src/lib/description-save-queue.test.ts
@@ -94,3 +94,20 @@ it("isolates owners and prevents a cleared in-flight request from draining anoth
expect(newSave).toHaveBeenCalledExactlyOnceWith("bob's edit");
expect(queue.get("task", "alice")).toBeUndefined();
});
+
+it("drains an unexpired debounce before sign-out and retains a failed drain for retry", async () => {
+ vi.useFakeTimers();
+ const queue = createDescriptionSaveQueue(700);
+ const save = vi.fn().mockResolvedValue(undefined);
+ queue.schedule("task", "recent edit", save, "alice");
+ expect(await queue.drain("alice")).toBe(true);
+ expect(save).toHaveBeenCalledExactlyOnceWith("recent edit");
+ const failing = vi.fn().mockRejectedValue(new Error("offline"));
+ queue.schedule("task", "unsaved edit", failing, "alice");
+ expect(await queue.drain("alice")).toBe(false);
+ expect(queue.get("task", "alice")).toMatchObject({
+ value: "unsaved edit",
+ state: "failed",
+ });
+ queue.clear();
+});
diff --git a/apps/web/src/lib/description-save-queue.ts b/apps/web/src/lib/description-save-queue.ts
index dc36b48c4..fd6861d1e 100644
--- a/apps/web/src/lib/description-save-queue.ts
+++ b/apps/web/src/lib/description-save-queue.ts
@@ -7,6 +7,7 @@ type Entry = {
state: DescriptionSaveState;
timer?: ReturnType<typeof setTimeout>;
running: boolean;
+ promise?: Promise<void>;
version: number;
};
@@ -15,7 +16,7 @@ export function createDescriptionSaveQueue(delay = 700) {
const entries = new Map<string, Entry>();
const listeners = new Set<() => void>();
const notify = () => listeners.forEach((listener) => listener());
- const flush = async (id: string) => {
+ const run = async (id: string) => {
const entry = entries.get(id);
if (!entry || entry.running) return;
if (entry.timer) clearTimeout(entry.timer);
@@ -52,7 +53,28 @@ export function createDescriptionSaveQueue(delay = 700) {
}
}
};
+ const flush = (id: string): Promise<void> => {
+ const entry = entries.get(id);
+ if (entry?.running) return entry.promise ?? Promise.resolve();
+ const promise = run(id);
+ if (entry) entry.promise = promise;
+ return promise;
+ };
return {
+ async drain(ownerId: string) {
+ while (true) {
+ const pending = [...entries.entries()].filter(
+ ([, entry]) => entry.ownerId === ownerId,
+ );
+ if (!pending.length) return true;
+ for (const [id] of pending) {
+ await flush(id);
+ const current = entries.get(id);
+ if (current?.ownerId === ownerId && current.state === "failed")
+ return false;
+ }
+ }
+ },
schedule(id: string, value: string, save: Entry["save"], ownerId = "") {
const current = entries.get(id);
if (current?.timer && current.ownerId !== ownerId)