authoravatarclover caruso<git@paperclover.net>2026-10-14 17:57:09-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-11 11:13:55-08:00
log002a02c932f1c7bde73e75b4177b2a6f50e3956d
treec4835c10b4aec95d2aa694c14c81437a118b1851
parente974dee2fb62fa115dc200354f85ff69145c36c7
signature Signed by SSH key SHA256:cOKiuRFOeSRxne6EWgHtdQQSlBxjOXm2hOCFnCdLQbQ

fix: clear the pending delay when useMutate resets

reset() replaced the state and dropped the subscription but left pendingTimer running. Swapping the mutation passed to useMutate within the 200ms delay let that timer set isPending on the fresh state, with no subscription left to clear it. Assisted-by: claude-opus-5.5 Assisted-by: claude-opus-5

3 files changed, 40 insertions(+), 0 deletions(-)

readme.changes.md+2
...@@ -7,6 +7,8 @@...@@ -7,6 +7,8 @@
7- resolve calls made while a failed mutation refetches never running7- resolve calls made while a failed mutation refetches never running
8- resolve a synchronous throw from `mutate` escaping to the caller and leaving8- resolve a synchronous throw from `mutate` escaping to the caller and leaving
9 the mutation stuck in its mutating state9 the mutation stuck in its mutating state
10- resolve `useMutate` setting `isPending` after its mutation is swapped within
11 the 200ms pending delay
1012
11## v3.0.213## v3.0.2
1214
src/react.ts+1
...@@ -241,6 +241,7 @@ class Observer<Args extends unknown[], Result> {...@@ -241,6 +241,7 @@ class Observer<Args extends unknown[], Result> {
241 this.unsubscribe = null;241 this.unsubscribe = null;
242 this.currentKey = null;242 this.currentKey = null;
243 this.state = initialState();243 this.state = initialState();
244 this.resetPending();
244 this.debounced = false;245 this.debounced = false;
245 }246 }
246247
test/useMutate.test.tsx+37
...@@ -136,6 +136,43 @@ test("useMutate - a failure before the pending delay never leaves isPending stuc...@@ -136,6 +136,43 @@ test("useMutate - a failure before the pending delay never leaves isPending stuc
136 });136 });
137});137});
138138
139test("useMutate - swapping the mutation within the pending delay never sets isPending", async () => {
140 vi.useFakeTimers({ shouldAdvanceTime: true });
141 const user = userEvent.setup({ delay: null });
142
143 const { client } = createTestMutationClient();
144 const s = new IterableStream<string>();
145 const define = (id: string) =>
146 client.define({
147 id,
148 mutate: async () => {
149 return (await s.next()).value;
150 },
151 describe: "Test the action",
152 describeResult: null,
153 optimistic: () => {},
154 });
155 const mutA = define("test-swap-a");
156 const mutB = define("test-swap-b");
157
158 let isPending = false;
159 function TestComponent({ mutation }: { mutation: typeof mutA }) {
160 const state = useMutate(mutation);
161 isPending = state.isPending;
162 return (
163 <button data-testid="a" onClick={() => state.run()}>
164 button
165 </button>
166 );
167 }
168
169 const view = render(<TestComponent mutation={mutA} />);
170 await act(() => user.click(screen.getByTestId("a")));
171 view.rerender(<TestComponent mutation={mutB} />);
172 await act(() => vi.advanceTimersByTime(500));
173 assertEquals(isPending, false);
174});
175
139test("useMutate - local error and success handling", async () => {176test("useMutate - local error and success handling", async () => {
140 vi.useFakeTimers({ shouldAdvanceTime: true });177 vi.useFakeTimers({ shouldAdvanceTime: true });
141 const user = userEvent.setup({ delay: null });178 const user = userEvent.setup({ delay: null });