From 2486cbb9d095be3d97251f02f076d0e2a1cb0033 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Wed, 28 Jan 2026 17:35:45 -0800 Subject: [PATCH] fix: bring back the success message --- jsr.json | 2 +- readme.md | 6 +++--- src/blocking.ts | 5 +++-- src/debounced.ts | 20 ++++++++++++++------ src/types.ts | 2 +- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/jsr.json b/jsr.json index 89cf8b9a0e8c3bc7d4c131025d5dee86efc3c478..e81b90dfeda66733df5e35779c87cb29297c387c 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@clo/react-mutation", - "version": "1.0.0-beta.3", + "version": "1.0.0-beta.4", "exports": { ".": "./src/mod.ts", "./tanstack-query.ts": "./src/tanstack-query.ts", diff --git a/readme.md b/readme.md index b1ed3256e5fed7486aec6b2c64c661190256b584..c96353f60368c38aebf0b234fb85a3a0559423d9 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ The primary gains React Mutation provides are `isError`, unhandled errors will be propagated to a global handler, which can display a UI toast. Otherwise, the component can display the error locally. - Optimistic helpers allow defining rollbacks and refetching logic independant - of the actual mutation. The [built in helpers for React Query](#React-Query-Optimistic-Helpers) show this power in more detail. + of the actual mutation. The [built in helpers for React Query](#react-query-optimistic-helpers) show this power in more detail. - Debounced Mutations are just so awesome to use. ## Usage @@ -22,11 +22,11 @@ The primary gains React Mutation provides are This library declares two kinds of mutations. Each kind has different behavior around concurrent operations. -- [**Blocking Mutations**](#Blocking-Mutations): A mutation blocks the UI until it +- [**Blocking Mutations**](#blocking-mutations): A mutation blocks the UI until it is complete. You press a button, a pending state appears, then it completes. This works great for forms, creations and deletions, and is similar to React Query's mutation system. -- [**Debounced Mutations**](#Debounced-Mutations): Each call to the mutation applies +- [**Debounced Mutations**](#debounced-mutations): Each call to the mutation applies new optimistic state, and after a debounce (or throttle) the new optimistic state is committed to the API. UI never shows a pending state for these. This works great for auto-saving input fields, follow buttons, and is diff --git a/src/blocking.ts b/src/blocking.ts index 0c06e3e148c1f4a64f0bbe2892999e1f65beaa5a..8aa5174acf658dd15e1dd94bc4e748a5c8cdfe85 100644 --- a/src/blocking.ts +++ b/src/blocking.ts @@ -34,7 +34,7 @@ export interface BlockingMutationOptions< * Used in success messages. * Phrase it as a complete success message, e.g., "Deleted item successfully" */ - describeResult: string | ((context: Config["context"] & { args: Args; result: Result }) => string); + describeResult: string | ((context: Config["context"] & { args: Args; result: Result }) => string) | null; /** * Specifying the optimistic strategy is required. To disable, pass an empty * function with a comment to document why it isn't needed. @@ -169,8 +169,9 @@ export class BlockingMutation< : describe; } - describeResult(args: Args, result: Result): string { + describeResult(args: Args, result: Result): string | undefined { const { describeResult } = this.#options; + if (describeResult === null) return undefined; return typeof describeResult === "function" ? describeResult({ ...this.#client.context, args, result }) : describeResult; diff --git a/src/debounced.ts b/src/debounced.ts index fb8869a3826b8ccfc080e1d7a01c7ae20ed92c33..0470fe39ee2562df3e91734beff31b1966fc0e42 100644 --- a/src/debounced.ts +++ b/src/debounced.ts @@ -14,7 +14,10 @@ export interface DebouncedMutationOptions< * A rest params type is used to allow type inference. Place this function first to * ensure TypeScript correctly infers the argument type for the rest of the functions. */ - optimistic: (context: DebouncedOptimisticContext, ...args: Args) => void; + optimistic: ( + context: DebouncedOptimisticContext, + ...args: Args + ) => void; /** * Retrieve the current/optimistic value of the mutation. When this returns * the same thing as when the mutation started, it means that `mutate` does @@ -56,7 +59,7 @@ export interface DebouncedMutationOptions< ) => string); /** * Used in success messages. - * Phrase it as a complete success message, e.g., "Renamed item successfully" + * Phrase it as a complete success message: "Renamed item successfully" */ describeResult: | string @@ -64,7 +67,8 @@ export interface DebouncedMutationOptions< context: DebouncedCommitContext, Optimistic, Config> & { result: Result; }, - ) => string); + ) => string) + | null; /** * Refetch all of the data this mutation could have affected. */ @@ -251,15 +255,16 @@ export class DebouncedMutation< } // Not available for debounced mutations - success reporting happens during commit - describeResult: undefined = undefined; + describeResult: null = null; #describeResult( args: Args, initial: Optimistic, current: Optimistic, result: Result, - ): string { + ): string | undefined { const { describeResult } = this.#options; + if (describeResult === null) return undefined; return typeof describeResult === "function" ? describeResult({ ...this.#client.context, @@ -302,7 +307,10 @@ export class DebouncedMutation< // If this is the first call in the debounced run, take a snapshot and create shared helpers if (channel.initial === null) { - channel.initial = this.#options.getValue({ ...this.#client.context, args }); + channel.initial = this.#options.getValue({ + ...this.#client.context, + args, + }); channel.firstArgs = args; // Create shared onRefetch handler for the debounced run diff --git a/src/types.ts b/src/types.ts index 0013dbba186c39bdfa2aa8ffe8dd6f5e8dc3782a..b58f8e1d0983019b8dc2529c4e94f310673e6076 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,7 +14,7 @@ export interface Mutation { cb: (update: MutationEvent) => void, ): () => void; describe(...args: Args): string; - describeResult?: (args: Args, result: Result) => string | undefined; + describeResult: ((args: Args, result: Result) => string | undefined) | null; client: MutationClient; } -- 2.54.0