From 7d66362e0dbb074ed16b613b8bab554f5b231875 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Wed, 14 Oct 2026 17:54:46 -0700 Subject: [PATCH] fix: run calls queued during a post-failure refetch After a failed mutation, the channel sat in "refetching" while its refetch callbacks ran, then went straight to idle without draining its queue. A call made during that window was queued but never executed until some later call happened to drain the channel, so it ran late against stale state or was dropped with the channel. Assisted-by: claude-opus-5.5 Assisted-by: claude-opus-5 --- jsr.json | 2 +- readme.changes.md | 6 ++++++ src/mutation.ts | 2 +- test/ordering.test.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/jsr.json b/jsr.json index 05e8a948095e5526ba7bece4ecd4acd718427b8e..d2c7493f1d760a714d367e73f2032c6e2811ab07 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@clo/react-mutation", - "version": "3.0.2", + "version": "3.0.3", "exports": { ".": "./src/mod.ts", "./tanstack-query": "./src/tanstack-query.ts", diff --git a/readme.changes.md b/readme.changes.md index c5d4f1975073539f89e1b8e503c849977150e8fe..49e960abf2f4fa85d10ca0e5a3e9377abc75e6df 100644 --- a/readme.changes.md +++ b/readme.changes.md @@ -1,5 +1,11 @@ # notable changes in React Mutation +## v3.0.3 + +### bugfixes + +- resolve calls made while a failed mutation refetches never running + ## v3.0.2 ### bugfixes diff --git a/src/mutation.ts b/src/mutation.ts index ff3089a53c74ab193fbec0dbfe54da6b3b3aada6..a675028ce41cb009333cf7db4efd4860e7701120 100644 --- a/src/mutation.ts +++ b/src/mutation.ts @@ -662,7 +662,7 @@ export class BlockingMutation< } }); }).finally(() => { - this.#setIdle(key, channel); + this.#executeNext(key, channel); }); reject(wrappedError); diff --git a/test/ordering.test.ts b/test/ordering.test.ts index 3e2d589986c5a59ac20caa5b46d6223ad7372cd8..8300bd4726bcba6ed427060f684b3b9492adccec 100644 --- a/test/ordering.test.ts +++ b/test/ordering.test.ts @@ -435,3 +435,32 @@ test("optimistic update with no describeResult: no success message", async () => assertEquals(successMessages, [], "No success message when describeResult is null"); assertEquals(calls.includes("success"), true, "onSuccess callback still called"); }); + +test("a call made during the post-failure refetch runs once the refetch settles", async () => { + vi.useFakeTimers(); + const { client, successMessages } = createTestMutationClient(); + const calls: string[] = []; + + const mutTest = client.define({ + id: "test-10", + mutate: async (id: number) => { + calls.push(`mutate-${id}`); + await delay(100); + if (id === 1) throw new Error("First failed"); + return `result-${id}`; + }, + describe: "test", + describeResult: (ctx) => `Completed ${ctx.args[0]}`, + optimistic: ({ onRefetch }) => { + onRefetch(() => delay(100)); + }, + }); + + mutTest.run(1); + await vi.advanceTimersByTimeAsync(150); + mutTest.run(2); + await vi.advanceTimersByTimeAsync(500); + + assertEquals(calls, ["mutate-1", "mutate-2"]); + assertEquals(successMessages, ["Completed 2"]); +}); -- 2.54.1