diff --git a/readme.changes.md b/readme.changes.md index 49e960abf2f4fa85d10ca0e5a3e9377abc75e6df..adc3c5f7e7ee573dc805cca94f31259129615368 100644 --- a/readme.changes.md +++ b/readme.changes.md @@ -5,6 +5,8 @@ ### bugfixes - resolve calls made while a failed mutation refetches never running +- resolve a synchronous throw from `mutate` escaping to the caller and leaving + the mutation stuck in its mutating state ## v3.0.2 diff --git a/src/mutation.ts b/src/mutation.ts index a675028ce41cb009333cf7db4efd4860e7701120..16d06bd3af23e65b6baf14e09ed35f7e88dfae85 100644 --- a/src/mutation.ts +++ b/src/mutation.ts @@ -599,7 +599,7 @@ export class BlockingMutation< channel.status = "mutating"; this.#notify(channel, "mutating"); - this.#options.mutate.call(context, ...args).then((result) => { + new Promise((resolve) => resolve(this.#options.mutate.call(context, ...args))).then((result) => { // remove rollbacks and apply optimistic success handlers channel.rollbacks.splice(0, item.rollbacks); onSuccess.forEach((cb) => cb(result)); diff --git a/test/ordering.test.ts b/test/ordering.test.ts index 8300bd4726bcba6ed427060f684b3b9492adccec..dac4168bdf5f367c2fa52c46372f7abc47ff243a 100644 --- a/test/ordering.test.ts +++ b/test/ordering.test.ts @@ -464,3 +464,30 @@ test("a call made during the post-failure refetch runs once the refetch settles" assertEquals(calls, ["mutate-1", "mutate-2"]); assertEquals(successMessages, ["Completed 2"]); }); + +test("a synchronous throw from mutate fails the call without wedging the channel", async () => { + vi.useFakeTimers(); + const { client, errorMessages, successMessages } = createTestMutationClient(); + const calls: string[] = []; + + const mutTest = client.define({ + id: "test-11", + mutate: (id: number) => { + calls.push(`mutate-${id}`); + if (id === 1) throw new Error("Sync failure"); + return Promise.resolve(`result-${id}`); + }, + describe: "test", + describeResult: (ctx) => `Completed ${ctx.args[0]}`, + optimistic: () => {}, + }); + + mutTest.run(1); + await vi.advanceTimersByTimeAsync(0); + mutTest.run(2); + await vi.advanceTimersByTimeAsync(0); + + assertEquals(calls, ["mutate-1", "mutate-2"]); + assertEquals(errorMessages.map((e) => e.message), ["Could not test: Sync failure"]); + assertEquals(successMessages, ["Completed 2"]); +});