From d5c11b294ef2257d31778609caddf9980e24e6f6 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Thu, 29 Jan 2026 16:56:46 -0800 Subject: [PATCH] feat: arrayFilter and useMutate args --- jsr.json | 2 +- src/react.ts | 13 +++++++++++++ src/tanstack-query.ts | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/jsr.json b/jsr.json index ad9150526795d168d4f2fdc11f2ef5589fa1cca9..835a348d014e3df039d914cd2fa553a6c81c73d8 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@clo/react-mutation", - "version": "1.0.0-beta.7", + "version": "1.0.0-beta.8", "exports": { ".": "./src/mod.ts", "./tanstack-query.ts": "./src/tanstack-query.ts", diff --git a/src/react.ts b/src/react.ts index 2d8dac06a9d527d9b9d6246c2e1fb0854c709eed..15caaee76e046ecc7823d371e48eacc462e5094a 100644 --- a/src/react.ts +++ b/src/react.ts @@ -45,6 +45,7 @@ export interface UseMutateResultBase { ..._: [...args: Args, options: RunOptions] ) => Promise; clear: () => void; + args: Args | undefined; } export interface UseMutateSuccess { @@ -175,7 +176,11 @@ class Observer { run(...args: Args) { const mutation = this.mutation; if (!mutation) return; + const argsChanged = this.currentArgs !== args; this.currentArgs = args; + if (argsChanged && this.watched.has("args")) { + this.setRerender(Math.random()); + } const key = mutation.key(args); if (key !== this.currentKey) { this.currentKey = key; @@ -249,7 +254,11 @@ class Observer { const args = array.slice() as Args; const options = args.pop() as RunOptions; + const argsChanged = this.currentArgs !== args; this.currentArgs = args; + if (argsChanged && this.watched.has("args")) { + this.setRerender(Math.random()); + } const key = mutation.key(args); // Set up subscription if key changed @@ -347,6 +356,10 @@ class Observer { self.watched.add("isOptimisticData"); return self.state.isOptimisticData; }, + get args() { + self.watched.add("args"); + return self.currentArgs ?? undefined; + }, } as UseMutateResult))(this); } diff --git a/src/tanstack-query.ts b/src/tanstack-query.ts index a4a5b0d51f0d7dff893ad7e3a0283f41b5ef8ce0..a670aa02d4651b17e578a5cc496074dfff10383d 100644 --- a/src/tanstack-query.ts +++ b/src/tanstack-query.ts @@ -509,6 +509,28 @@ class TanstackQueryOptimisticHelpers { }); } + /** + * Filter items to just include items that match `filter`. This is the inverse of `arrayRemove` + * If the query or path doesn't exist, the updater is skipped. + */ + arrayFilter( + queryKey: QueryKeyAndFn, + filter: ( + item: Data, + index: number, + ) => boolean, + ) { + const prev = this.#get(queryKey); + if (!prev || !Array.isArray(prev)) return; + + const newArray = prev.filter(filter); + this.#set(queryKey, newArray); + this.#onRestore(() => { + // TODO: splice items back in case original changed + this.#set(queryKey, prev); + }); + } + /** * Update items in an array that match a `filter`. * If the query or path doesn't exist, the updater is skipped. -- 2.54.0