tanstack-cacher 1.5.1 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +3 -0
- package/index.js +26 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -36,6 +36,9 @@ declare class QueryCacheManager<TData, TItem> {
|
|
|
36
36
|
update(updatedItem: Partial<TItem>, matcher?: (item: TItem) => boolean): void;
|
|
37
37
|
updateWithCustomLogic(updater: (oldData: any) => any): void;
|
|
38
38
|
delete(itemOrId: TItem | string | number, matcher?: (item: TItem) => boolean): void;
|
|
39
|
+
updateArrayAtPath<TItem = any>(path: string, updater: TItem | TItem[] | ((items: TItem[]) => TItem[]), options?: {
|
|
40
|
+
position?: InsertPosition;
|
|
41
|
+
}): void;
|
|
39
42
|
replace(newData: TData): void;
|
|
40
43
|
clear(): void;
|
|
41
44
|
getItemsFromCache(): TItem[];
|
package/index.js
CHANGED
|
@@ -268,6 +268,32 @@ var QueryCacheManager = class {
|
|
|
268
268
|
this.invalidate();
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
|
+
updateArrayAtPath(path, updater, options) {
|
|
272
|
+
try {
|
|
273
|
+
this.config.queryClient.setQueryData(this.config.queryKey, (oldData) => {
|
|
274
|
+
if (!oldData) return oldData;
|
|
275
|
+
if (!updater) {
|
|
276
|
+
this.invalidate();
|
|
277
|
+
return oldData;
|
|
278
|
+
}
|
|
279
|
+
const current = getAtPath(oldData, path, []);
|
|
280
|
+
const safeCurrent = Array.isArray(current) ? current : [];
|
|
281
|
+
let updatedItems;
|
|
282
|
+
if (typeof updater === "function") {
|
|
283
|
+
updatedItems = updater(safeCurrent);
|
|
284
|
+
} else if (Array.isArray(updater)) {
|
|
285
|
+
updatedItems = updater;
|
|
286
|
+
} else {
|
|
287
|
+
const position = options?.position ?? "start";
|
|
288
|
+
updatedItems = position === "start" ? [updater, ...safeCurrent] : [...safeCurrent, updater];
|
|
289
|
+
}
|
|
290
|
+
return setAtPath(oldData, path, updatedItems);
|
|
291
|
+
});
|
|
292
|
+
} catch (error) {
|
|
293
|
+
console.error("[QueryCacheManager] updateArrayAtPath failed:", error);
|
|
294
|
+
this.invalidate();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
271
297
|
/**
|
|
272
298
|
* Replace full data
|
|
273
299
|
*
|
package/package.json
CHANGED