From 753ee8624109c92d55060573e22caa23be8d6ce9 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Wed, 14 Oct 2026 17:57:46 -0700 Subject: [PATCH] fix: respect debounce and clear stale delays in runWithOptions The runWithOptions subscription never recorded `debounced` and set isPending for debounced mutations, so a debounced mutation behind a MutationButton showed as pending and disabled the button while its request ran, contrary to the documented behaviour. Switching keys from a run() subscription also left that subscription's pendingTimer alive to set isPending after the new key went idle. Assisted-by: claude-opus-5.5 Assisted-by: claude-opus-5 --- readme.changes.md | 3 +++ src/react.ts | 7 +++++-- test/debounce.test.tsx | 43 ++++++++++++++++++++++++++++++++++++++++- test/useMutate.test.tsx | 35 +++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/readme.changes.md b/readme.changes.md index 54038395de1ce49134428a33e7710f9c8b0c325e..77081b7aa1a5e76ab4266a390da9d07ca03d9af0 100644 --- a/readme.changes.md +++ b/readme.changes.md @@ -9,6 +9,9 @@ the mutation stuck in its mutating state - resolve `useMutate` setting `isPending` after its mutation is swapped within the 200ms pending delay +- resolve `runWithOptions` (and so `MutationButton`) setting `isPending` and + disabling controls for debounced mutations, and leaving a pending delay from + an earlier `run` to set `isPending` later ## v3.0.2 diff --git a/src/react.ts b/src/react.ts index c54193dd8ddf187e1182174151438628a25c2f54..d4cc1317b5b590af53b96a78843e2bb7d613b209 100644 --- a/src/react.ts +++ b/src/react.ts @@ -350,9 +350,12 @@ class Observer { if (key !== this.currentKey) { this.currentKey = key; this.unsubscribe?.(); + this.resetPending(); this.unsubscribe = mutation.subscribe( mutation.key(args), - ({ status, error, result }) => { + ({ status, error, result, debounced }) => { + this.debounced = debounced; + if (status === "idle") { this.setState({ isMutating: false, @@ -378,7 +381,7 @@ class Observer { : this.computeErrorMessage(error ?? undefined), result: result ?? undefined, isMutating: status === "mutating", - isPending: status === "mutating" || status === "refetching", + isPending: !debounced && (status === "mutating" || status === "refetching"), isSuccess: hasResult && !hasError, isError: hasError, isOptimisticData: status === "waiting" || status === "mutating" diff --git a/test/debounce.test.tsx b/test/debounce.test.tsx index 606fbaa18b5249026b131cdbc64988d39de71959..c1eb2fc89e9e0b380cea1718fc599ef9829fbd33 100644 --- a/test/debounce.test.tsx +++ b/test/debounce.test.tsx @@ -1,7 +1,8 @@ -import { useMutate } from "@clo/react-mutation"; +import { createMutationButton, useMutate } from "@clo/react-mutation"; import { assertEquals } from "@std/assert"; import { act, render, screen } from "@testing-library/react"; import { userEvent } from "@testing-library/user-event"; +import type { FC, MouseEventHandler, ReactNode } from "react"; import { test, vi } from "vitest"; import { createTestMutationClient, IterableStream } from "./share.ts"; @@ -181,6 +182,46 @@ test("debounceMs: batch multiple calls together", async () => { renders = []; }); +test("debounceMs: a mutation button stays enabled and not pending while mutating", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + const user = userEvent.setup({ delay: null }); + + const { client } = createTestMutationClient(); + const s = new IterableStream(); + + const mutTest = client.define({ + id: "test-button", + mutate: async () => { + return (await s.next()).value; + }, + describe: "Test the action", + describeResult: null, + optimistic: () => {}, + debounceMs: 500, + }); + + let last = {}; + const MutationButtonBase: FC<{ + children?: ReactNode; + disabled?: boolean; + isPending: boolean; + onClick: MouseEventHandler | undefined; + }> = function MutationButtonBase({ children, disabled, isPending, onClick }) { + last = { disabled, isPending }; + return ( + + ); + }; + const MutationButton = createMutationButton(MutationButtonBase); + + render(button); + await act(() => user.click(screen.getByTestId("a"))); + await act(() => vi.advanceTimersByTime(1000)); + assertEquals(last, { disabled: false, isPending: false }); +}); + test.todo("debounceImmediate runs the first one right away", async () => { vi.useFakeTimers({ shouldAdvanceTime: true }); const user = userEvent.setup({ delay: null }); diff --git a/test/useMutate.test.tsx b/test/useMutate.test.tsx index f4b6f8bdcdbcf52aeeb62867c37128a585a9d6e4..b7b63d6789b42063d85a7f5bf09153236ef53c9e 100644 --- a/test/useMutate.test.tsx +++ b/test/useMutate.test.tsx @@ -173,6 +173,41 @@ test("useMutate - swapping the mutation within the pending delay never sets isPe assertEquals(isPending, false); }); +test("useMutate - runWithOptions on another key clears the pending delay from run", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + const user = userEvent.setup({ delay: null }); + + const { client } = createTestMutationClient(); + + const mutTest = client.define({ + id: "test-pending-handoff", + mutate: (id: number) => id === 1 ? new Promise(() => {}) : Promise.resolve("ok"), + key: ({ args: [id] }) => String(id), + describe: "Test the action", + describeResult: null, + optimistic: () => {}, + refetchOnSuccess: false, + }); + + let isPending = false; + function TestComponent() { + const state = useMutate(mutTest); + isPending = state.isPending; + return ( + <> + + + + ); + } + + render(); + await act(() => user.click(screen.getByTestId("a"))); + await act(() => user.click(screen.getByTestId("b"))); + await act(() => vi.advanceTimersByTime(500)); + assertEquals(isPending, false); +}); + test("useMutate - local error and success handling", async () => { vi.useFakeTimers({ shouldAdvanceTime: true }); const user = userEvent.setup({ delay: null }); -- 2.54.1