authoravatarclover caruso<git@paperclover.net>2026-10-14 17:54:46-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-11 11:13:55-08:00
log7d66362e0dbb074ed16b613b8bab554f5b231875
treedd6085a9ac1bb54362b5e788ceb1009023eee15b
parent02fca8a6248c2898025748f41c9bf5252094fd9e
signature Signed by SSH key SHA256:cOKiuRFOeSRxne6EWgHtdQQSlBxjOXm2hOCFnCdLQbQ

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

4 files changed, 37 insertions(+), 2 deletions(-)

jsr.json+1-1
......@@ -1,6 +1,6 @@
11{
22 "name": "@clo/react-mutation",
3 "version": "3.0.2",
3 "version": "3.0.3",
44 "exports": {
55 ".": "./src/mod.ts",
66 "./tanstack-query": "./src/tanstack-query.ts",
readme.changes.md+6
......@@ -1,5 +1,11 @@
11# notable changes in React Mutation
22
3## v3.0.3
4
5### bugfixes
6
7- resolve calls made while a failed mutation refetches never running
8
39## v3.0.2
410
511### bugfixes
src/mutation.ts+1-1
......@@ -662,7 +662,7 @@ export class BlockingMutation<
662662 }
663663 });
664664 }).finally(() => {
665 this.#setIdle(key, channel);
665 this.#executeNext(key, channel);
666666 });
667667
668668 reject(wrappedError);
test/ordering.test.ts+29
......@@ -435,3 +435,32 @@ test("optimistic update with no describeResult: no success message", async () =>
435435 assertEquals(successMessages, [], "No success message when describeResult is null");
436436 assertEquals(calls.includes("success"), true, "onSuccess callback still called");
437437});
438
439test("a call made during the post-failure refetch runs once the refetch settles", async () => {
440 vi.useFakeTimers();
441 const { client, successMessages } = createTestMutationClient();
442 const calls: string[] = [];
443
444 const mutTest = client.define({
445 id: "test-10",
446 mutate: async (id: number) => {
447 calls.push(`mutate-${id}`);
448 await delay(100);
449 if (id === 1) throw new Error("First failed");
450 return `result-${id}`;
451 },
452 describe: "test",
453 describeResult: (ctx) => `Completed ${ctx.args[0]}`,
454 optimistic: ({ onRefetch }) => {
455 onRefetch(() => delay(100));
456 },
457 });
458
459 mutTest.run(1);
460 await vi.advanceTimersByTimeAsync(150);
461 mutTest.run(2);
462 await vi.advanceTimersByTimeAsync(500);
463
464 assertEquals(calls, ["mutate-1", "mutate-2"]);
465 assertEquals(successMessages, ["Completed 2"]);
466});