authorgravatar for git@paperclover.netclover caruso <git@paperclover.net> 2026-01-29 16:49:46-08:00
committergravatar for git@paperclover.netclover caruso <git@paperclover.net> 2026-01-29 16:57:23-08:00
logb51077c59854543b2c70ea2be825d68cfb567d8c
tree3449d3519625c9bb4232125795e3004322a32d05
parentf62d1ee915bf4e77c8e2d55d1286211289fbdd51
signaturelock-open Commit is signed but in an unrecognized format.

chore: swap arg order


4 files changed, 18 insertions(+), 21 deletions(-)

jsr.json+1-1
......@@ -1,6 +1,6 @@
11{
22 "name": "@clo/react-mutation",
3 "version": "1.0.0-beta.6",
3 "version": "1.0.0-beta.7",
44 "exports": {
55 ".": "./src/mod.ts",
66 "./tanstack-query.ts": "./src/tanstack-query.ts",
package.json-1
......@@ -1,6 +1,5 @@
11{
22 "name": "@clo/react-mutation",
3 "version": "0.1.0",
43 "private": true,
54 "description": "",
65 "license": "ISC",
readme.md+9-9
......@@ -21,7 +21,7 @@ The primary gains React Mutation provides are
2121 this power in more detail.
2222- Easy debouncing and batching utilities.
2323
24## Usage
24## Setup
2525
2626React Mutation starts with a `MutationClient`, which shares global state for an application.
2727
......@@ -61,7 +61,7 @@ export const mutations = new MutationClient({
6161});
6262```
6363
64### Declaring Mutations
64## Declaring Mutations
6565
6666With a mutation client, you can declare mutations with `mutations.define()`.
6767Start with the API call code, and then add an optimistic updater function.
......@@ -121,7 +121,7 @@ export function Example({ id }: { id: string }) {
121121```
122122
123123
124### Optimistic Updates
124## Optimistic Updates
125125
126126The `optimistic` function is given an object with the following APIs
127127
......@@ -132,7 +132,7 @@ The `optimistic` function is given an object with the following APIs
132132- `onRestore` - add a callback to revert your optimistic update
133133- `onRefetch` - add a callback to fetch data after a success
134134
135#### React Query Optimistic Helpers
135### React Query Optimistic Helpers
136136
137137When using React Query, you can opt into some incredible helpers for making it
138138very easy to write Optimistic Updates. Our setup at work is with this client
......@@ -191,7 +191,7 @@ automatically implement `onRefetch` and `onRestore` callbacks. The current list
191191 - `objArrayUpdate` - update items in array by `filter` + `update`
192192 - `objArrayInsertIndex` - insert an item in an array at an index
193193
194### Debouncing
194## Debouncing
195195
196196By default, a mutation will block the UI (by setting isPending). If you add
197197`debounceMs`, the mutation will no longer set isPending. Multiple mutations
......@@ -226,7 +226,7 @@ function Item({ id }: { id: string }) {
226226}
227227```
228228
229### Calling Mutations
229## Calling Mutations
230230
231231Three methods exist for calling mutations:
232232
......@@ -234,7 +234,7 @@ Three methods exist for calling mutations:
234234- From a React component: `useMutate(mutDoAction)`
235235- From a React Element: `<MutationButton>`
236236
237#### The `useMutate` Hook
237### The `useMutate` Hook
238238
239239The `useMutate(null | Mutation)` react hook returns an object with the following properties.
240240
......@@ -269,7 +269,7 @@ return (
269269);
270270```
271271
272#### Mutation Buttons
272### Mutation Buttons
273273
274274You can wrap your button component with `createMutationButton` to make it support mutations
275275
......@@ -312,7 +312,7 @@ It can now be used for easy mutations:
312312
313313
314314
315### Batched Mutations
315## Batched Mutations
316316
317317Each call to the mutation applies new optimistic state on top of the previous,
318318and after a debounce / throttle, the new optimistic state is committed to the
src/react.ts+8-10
......@@ -42,8 +42,7 @@ export type UseMutateResult<Args extends unknown[], Result> =
4242export interface UseMutateResultBase<Args extends unknown[], Result> {
4343 run: (...args: Args) => void;
4444 runWithOptions: (
45 options: RunOptions<Result>,
46 ...args: Args
45 ..._: [...args: Args, options: RunOptions<Result>]
4746 ) => Promise<Result>;
4847 clear: () => void;
4948}
......@@ -243,10 +242,13 @@ class Observer<Args extends unknown[], Result> {
243242 return promise;
244243 }
245244
246 runWithOptions(options: RunOptions<Result>, ...args: Args): void {
245 runWithOptions(...array: [...args: Args, options: RunOptions<Result>]): void {
247246 const mutation = this.mutation;
248247 if (!mutation) return;
249248
249 const args = array.slice() as Args;
250 const options = args.pop() as RunOptions<Result>;
251
250252 this.currentArgs = args;
251253 const key = mutation.key(args);
252254
......@@ -295,12 +297,8 @@ class Observer<Args extends unknown[], Result> {
295297 }
296298
297299 binding: UseMutateResult<Args, Result> = ((self: this) => ({
298 run(...args) {
299 return self.run(...args);
300 },
301 runWithOptions(options, ...args) {
302 return self.runWithOptions(options, ...args);
303 },
300 run: self.run.bind(self),
301 runWithOptions: self.runWithOptions.bind(self),
304302 clear() {
305303 self.setState({
306304 status: ["error", "success"].includes(self.state.status)
......@@ -459,8 +457,8 @@ function GenericMutationButton<
459457 const computedArgs = typeof args === "function" ? args(e) : args;
460458 if (!computedArgs || e.defaultPrevented) return;
461459 state.runWithOptions(
462 { onSuccess, onError, onSettled },
463460 ...computedArgs,
461 { onSuccess, onError, onSettled },
464462 );
465463 }, [state]),
466464 isPending: state.isPending,