authoravatarclover caruso<git@paperclover.net>2026-10-14 18:27:38-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-11 11:13:55-08:00
logcf0988729b9a3695215b40bc5090ed5cebde810e
tree0baad9211b2a013fcb3cc4db9aef7e1250de7e75
parent753ee8624109c92d55060573e22caa23be8d6ce9
signature Signed by SSH key SHA256:cOKiuRFOeSRxne6EWgHtdQQSlBxjOXm2hOCFnCdLQbQ

fix: queue debounced calls behind an in-flight request

The first debounced call in a burst set the channel to "waiting" even while a request was mutating or refetching. When its timer fired, #enqueueDebouncedCall saw "waiting" and started it concurrently, and the first request finishing then marked the channel idle while the second was still in flight. Assisted-by: claude-opus-5.5 Assisted-by: claude-opus-5

3 files changed, 38 insertions(+), 3 deletions(-)

readme.changes.md+2
...@@ -12,6 +12,8 @@...@@ -12,6 +12,8 @@
12- resolve `runWithOptions` (and so `MutationButton`) setting `isPending` and12- resolve `runWithOptions` (and so `MutationButton`) setting `isPending` and
13 disabling controls for debounced mutations, and leaving a pending delay from13 disabling controls for debounced mutations, and leaving a pending delay from
14 an earlier `run` to set `isPending` later14 an earlier `run` to set `isPending` later
15- resolve a debounced call made during an in-flight request running alongside
16 it, which let the first request mark the mutation idle mid-flight
1517
16## v3.0.218## v3.0.2
1719
src/mutation.ts+5-3
...@@ -817,9 +817,11 @@ export class BlockingMutation<...@@ -817,9 +817,11 @@ export class BlockingMutation<
817 shouldCallGlobalHandler,817 shouldCallGlobalHandler,
818 };818 };
819819
820 // Set status to waiting820 // An in-flight request picks the call up when it finishes
821 channel.status = "waiting";821 if (channel.status === "idle") {
822 this.#notify(channel, "waiting");822 channel.status = "waiting";
823 this.#notify(channel, "waiting");
824 }
823 } else {825 } else {
824 // Subsequent debounced call - update state826 // Subsequent debounced call - update state
825 channel.pendingDebounced.args = args;827 channel.pendingDebounced.args = args;
test/ordering.test.ts+31
...@@ -491,3 +491,34 @@ test("a synchronous throw from mutate fails the call without wedging the channel...@@ -491,3 +491,34 @@ test("a synchronous throw from mutate fails the call without wedging the channel
491 assertEquals(errorMessages.map((e) => e.message), ["Could not test: Sync failure"]);491 assertEquals(errorMessages.map((e) => e.message), ["Could not test: Sync failure"]);
492 assertEquals(successMessages, ["Completed 2"]);492 assertEquals(successMessages, ["Completed 2"]);
493});493});
494
495test("a debounced call made during an in-flight request waits for it", async () => {
496 vi.useFakeTimers();
497 const { client } = createTestMutationClient();
498 const calls: string[] = [];
499
500 const mutTest = client.define({
501 id: "test-12",
502 mutate: async (id: number) => {
503 calls.push(`start-${id}`);
504 await delay(100);
505 calls.push(`end-${id}`);
506 return `result-${id}`;
507 },
508 describe: "test",
509 describeResult: null,
510 optimistic: () => {},
511 refetchOnSuccess: false,
512 debounceMs: 10,
513 });
514 mutTest.subscribe(mutTest.key([1]), ({ status }) => {
515 if (status === "idle") calls.push("idle");
516 });
517
518 mutTest.run(1);
519 await vi.advanceTimersByTimeAsync(50);
520 mutTest.run(2);
521 await vi.advanceTimersByTimeAsync(500);
522
523 assertEquals(calls, ["start-1", "end-1", "start-2", "end-2", "idle"]);
524});