| ... | ... | @@ -47,10 +47,10 @@ class TanstackQueryOptimisticHelpers { |
| 47 | 47 | * Set the entire query data. |
| 48 | 48 | * If the query doesn't exist, the new query is created. |
| 49 | 49 | */ |
| 50 | | set<Data>( |
| 50 | set = <Data>( |
| 51 | 51 | queryKey: QueryKeyAndFn<Data>, |
| 52 | 52 | value: Data | ((prev: Data | undefined) => Data | undefined), |
| 53 | | ) { |
| 53 | ) => { |
| 54 | 54 | const prev = this.#get(queryKey); |
| 55 | 55 | |
| 56 | 56 | const newValue = typeof value === "function" |
| ... | ... | @@ -68,16 +68,16 @@ class TanstackQueryOptimisticHelpers { |
| 68 | 68 | this.#set(queryKey, prev); |
| 69 | 69 | } |
| 70 | 70 | }); |
| 71 | | } |
| 71 | }; |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | 74 | * Update the entire query data. |
| 75 | 75 | * If the query doesn't exist, it cancels |
| 76 | 76 | */ |
| 77 | | updateExisting<Data>( |
| 77 | updateExisting = <Data>( |
| 78 | 78 | queryKey: QueryKeyAndFn<Data>, |
| 79 | 79 | value: Data | ((prev: Data) => Data), |
| 80 | | ) { |
| 80 | ) => { |
| 81 | 81 | const prev = this.#get(queryKey); |
| 82 | 82 | if (!prev) return; |
| 83 | 83 | |
| ... | ... | @@ -89,14 +89,14 @@ class TanstackQueryOptimisticHelpers { |
| 89 | 89 | this.#onRestore(() => { |
| 90 | 90 | this.#set(queryKey, prev); |
| 91 | 91 | }); |
| 92 | | } |
| 92 | }; |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * Mark a query as changed and schedule a refetch. |
| 96 | 96 | * Does not modify any data. |
| 97 | 97 | * If the query doesn't exist, the updater is skipped. |
| 98 | 98 | */ |
| 99 | | refetchOnSettled(queryKey: QueryKeyAndFn) { |
| 99 | refetchOnSettled = (queryKey: QueryKeyAndFn) => { |
| 100 | 100 | if (!queryKey.queryFn) return; |
| 101 | 101 | const state = this.#client.getQueryCache().find({ |
| 102 | 102 | queryKey: queryKey.queryKey, |
| ... | ... | @@ -109,19 +109,19 @@ class TanstackQueryOptimisticHelpers { |
| 109 | 109 | await this.#client.invalidateQueries(queryKey); |
| 110 | 110 | }); |
| 111 | 111 | } catch { /* Ignore expiry */ } |
| 112 | | } |
| 112 | }; |
| 113 | 113 | |
| 114 | 114 | /** |
| 115 | 115 | * Set a property at an object path. |
| 116 | 116 | * If the query or path doesn't exist, the updater is skipped. |
| 117 | 117 | */ |
| 118 | | objSet<Data extends object, const Path extends AllObjectPaths<Data>>( |
| 118 | objSet = <Data extends object, const Path extends AllObjectPaths<Data>>( |
| 119 | 119 | queryKey: QueryKeyAndFn<Data>, |
| 120 | 120 | path: Path, |
| 121 | 121 | value: |
| 122 | 122 | | Exclude<GetObjectPath<Data, Path>, Function> |
| 123 | 123 | | ((prev: GetObjectPath<Data, Path>) => GetObjectPath<Data, Path>), |
| 124 | | ) { |
| 124 | ) => { |
| 125 | 125 | const prev = this.#get(queryKey); |
| 126 | 126 | if (!prev) return; |
| 127 | 127 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -145,20 +145,20 @@ class TanstackQueryOptimisticHelpers { |
| 145 | 145 | this.#onRestore(() => { |
| 146 | 146 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 147 | 147 | }); |
| 148 | | } |
| 148 | }; |
| 149 | 149 | |
| 150 | 150 | /** |
| 151 | 151 | * Increment a numeric property at an object path. |
| 152 | 152 | * If the query or path doesn't exist, the updater is skipped. |
| 153 | 153 | */ |
| 154 | | objIncrement< |
| 154 | objIncrement = < |
| 155 | 155 | Data extends object, |
| 156 | 156 | const Path extends AllObjectPaths<Data>, |
| 157 | 157 | >( |
| 158 | 158 | queryKey: QueryKeyAndFn<Data>, |
| 159 | 159 | path: Path, |
| 160 | 160 | amount: number = 1, |
| 161 | | ) { |
| 161 | ) => { |
| 162 | 162 | const prev = this.#get(queryKey); |
| 163 | 163 | if (!prev) return; |
| 164 | 164 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -171,20 +171,20 @@ class TanstackQueryOptimisticHelpers { |
| 171 | 171 | this.#onRestore(() => { |
| 172 | 172 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 173 | 173 | }); |
| 174 | | } |
| 174 | }; |
| 175 | 175 | |
| 176 | 176 | /** |
| 177 | 177 | * Decrement a numeric property at an object path. |
| 178 | 178 | * If the query or path doesn't exist, the updater is skipped. |
| 179 | 179 | */ |
| 180 | | objDecrement< |
| 180 | objDecrement = < |
| 181 | 181 | Data extends object, |
| 182 | 182 | const Path extends AllObjectPaths<Data>, |
| 183 | 183 | >( |
| 184 | 184 | queryKey: QueryKeyAndFn<Data>, |
| 185 | 185 | path: Path, |
| 186 | 186 | amount: number = 1, |
| 187 | | ) { |
| 187 | ) => { |
| 188 | 188 | const prev = this.#get(queryKey); |
| 189 | 189 | if (!prev) return; |
| 190 | 190 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -197,16 +197,16 @@ class TanstackQueryOptimisticHelpers { |
| 197 | 197 | this.#onRestore(() => { |
| 198 | 198 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 199 | 199 | }); |
| 200 | | } |
| 200 | }; |
| 201 | 201 | |
| 202 | 202 | /** |
| 203 | 203 | * Toggle a boolean property at an object path. |
| 204 | 204 | * If the query or path doesn't exist, the updater is skipped. |
| 205 | 205 | */ |
| 206 | | objToggle<Data extends object, const Path extends AllObjectPaths<Data>>( |
| 206 | objToggle = <Data extends object, const Path extends AllObjectPaths<Data>>( |
| 207 | 207 | queryKey: QueryKeyAndFn<Data>, |
| 208 | 208 | path: Path, |
| 209 | | ) { |
| 209 | ) => { |
| 210 | 210 | const prev = this.#get(queryKey); |
| 211 | 211 | if (!prev) return; |
| 212 | 212 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -219,17 +219,17 @@ class TanstackQueryOptimisticHelpers { |
| 219 | 219 | this.#onRestore(() => { |
| 220 | 220 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 221 | 221 | }); |
| 222 | | } |
| 222 | }; |
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | 225 | * Shallow merge multiple properties at an object path. |
| 226 | 226 | * If the query or path doesn't exist, the updater is skipped. |
| 227 | 227 | */ |
| 228 | | objSetMany<Data extends object, const Path extends AllObjectPaths<Data>>( |
| 228 | objSetMany = <Data extends object, const Path extends AllObjectPaths<Data>>( |
| 229 | 229 | queryKey: QueryKeyAndFn<Data>, |
| 230 | 230 | path: Path, |
| 231 | 231 | updates: Partial<GetObjectPath<Data, Path>>, |
| 232 | | ) { |
| 232 | ) => { |
| 233 | 233 | const prev = this.#get(queryKey); |
| 234 | 234 | if (!prev) return; |
| 235 | 235 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -248,13 +248,13 @@ class TanstackQueryOptimisticHelpers { |
| 248 | 248 | this.#onRestore(() => { |
| 249 | 249 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 250 | 250 | }); |
| 251 | | } |
| 251 | }; |
| 252 | 252 | |
| 253 | 253 | /** |
| 254 | 254 | * Push item(s) to the end of an array at an object path. |
| 255 | 255 | * If the query or path doesn't exist, the updater is skipped. |
| 256 | 256 | */ |
| 257 | | objArrayPush< |
| 257 | objArrayPush = < |
| 258 | 258 | Data extends object, |
| 259 | 259 | const Path extends AllObjectPaths<Data>, |
| 260 | 260 | >( |
| ... | ... | @@ -262,7 +262,7 @@ class TanstackQueryOptimisticHelpers { |
| 262 | 262 | path: Path, |
| 263 | 263 | ...items: GetObjectPath<Data, Path> extends Array<infer T> ? T[] |
| 264 | 264 | : never |
| 265 | | ) { |
| 265 | ) => { |
| 266 | 266 | const prev = this.#get(queryKey); |
| 267 | 267 | if (!prev) return; |
| 268 | 268 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -276,13 +276,13 @@ class TanstackQueryOptimisticHelpers { |
| 276 | 276 | this.#onRestore(() => { |
| 277 | 277 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 278 | 278 | }); |
| 279 | | } |
| 279 | }; |
| 280 | 280 | |
| 281 | 281 | /** |
| 282 | 282 | * Add item(s) to the beginning of an array at an object path. |
| 283 | 283 | * If the query or path doesn't exist, the updater is skipped. |
| 284 | 284 | */ |
| 285 | | objArrayUnshift< |
| 285 | objArrayUnshift = < |
| 286 | 286 | Data extends object, |
| 287 | 287 | const Path extends AllObjectPaths<Data>, |
| 288 | 288 | >( |
| ... | ... | @@ -290,7 +290,7 @@ class TanstackQueryOptimisticHelpers { |
| 290 | 290 | path: Path, |
| 291 | 291 | ...items: GetObjectPath<Data, Path> extends Array<infer T> ? T[] |
| 292 | 292 | : never |
| 293 | | ) { |
| 293 | ) => { |
| 294 | 294 | const prev = this.#get(queryKey); |
| 295 | 295 | if (!prev) return; |
| 296 | 296 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -304,13 +304,13 @@ class TanstackQueryOptimisticHelpers { |
| 304 | 304 | this.#onRestore(() => { |
| 305 | 305 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 306 | 306 | }); |
| 307 | | } |
| 307 | }; |
| 308 | 308 | |
| 309 | 309 | /** |
| 310 | 310 | * Remove items from an array that match `filter`. |
| 311 | 311 | * If the query or path doesn't exist, the updater is skipped. |
| 312 | 312 | */ |
| 313 | | objArrayRemove< |
| 313 | objArrayRemove = < |
| 314 | 314 | Data extends object, |
| 315 | 315 | const Path extends AllObjectPaths<Data>, |
| 316 | 316 | >( |
| ... | ... | @@ -320,7 +320,7 @@ class TanstackQueryOptimisticHelpers { |
| 320 | 320 | item: GetObjectPath<Data, Path> extends Array<infer T> ? T : never, |
| 321 | 321 | index: number, |
| 322 | 322 | ) => boolean, |
| 323 | | ) { |
| 323 | ) => { |
| 324 | 324 | const prev = this.#get(queryKey); |
| 325 | 325 | if (!prev) return; |
| 326 | 326 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -335,13 +335,13 @@ class TanstackQueryOptimisticHelpers { |
| 335 | 335 | // TODO: splice items back in case original changed |
| 336 | 336 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 337 | 337 | }); |
| 338 | | } |
| 338 | }; |
| 339 | 339 | |
| 340 | 340 | /** |
| 341 | 341 | * Filter items to just include items that match `filter`. This is the inverse of `objArrayRemove` |
| 342 | 342 | * If the query or path doesn't exist, the updater is skipped. |
| 343 | 343 | */ |
| 344 | | objArrayFilter< |
| 344 | objArrayFilter = < |
| 345 | 345 | Data extends object, |
| 346 | 346 | const Path extends AllObjectPaths<Data>, |
| 347 | 347 | >( |
| ... | ... | @@ -351,7 +351,7 @@ class TanstackQueryOptimisticHelpers { |
| 351 | 351 | item: GetObjectPath<Data, Path> extends Array<infer T> ? T : never, |
| 352 | 352 | index: number, |
| 353 | 353 | ) => boolean, |
| 354 | | ) { |
| 354 | ) => { |
| 355 | 355 | const prev = this.#get(queryKey); |
| 356 | 356 | if (!prev) return; |
| 357 | 357 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -366,13 +366,13 @@ class TanstackQueryOptimisticHelpers { |
| 366 | 366 | // TODO: splice items back in case original changed |
| 367 | 367 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 368 | 368 | }); |
| 369 | | } |
| 369 | }; |
| 370 | 370 | |
| 371 | 371 | /** |
| 372 | 372 | * Update items in an array that match a predicate. |
| 373 | 373 | * If the query or path doesn't exist, the updater is skipped. |
| 374 | 374 | */ |
| 375 | | objArrayUpdate< |
| 375 | objArrayUpdate = < |
| 376 | 376 | Data extends object, |
| 377 | 377 | const Path extends AllObjectPaths<Data>, |
| 378 | 378 | >( |
| ... | ... | @@ -387,7 +387,7 @@ class TanstackQueryOptimisticHelpers { |
| 387 | 387 | item: GetObjectPath<Data, Path> extends Array<infer T> ? T : never, |
| 388 | 388 | ) => GetObjectPath<Data, Path> extends Array<infer T> ? T : never; |
| 389 | 389 | }, |
| 390 | | ) { |
| 390 | ) => { |
| 391 | 391 | const prev = this.#get(queryKey); |
| 392 | 392 | if (!prev) return; |
| 393 | 393 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -402,13 +402,13 @@ class TanstackQueryOptimisticHelpers { |
| 402 | 402 | // TODO: splice items back in case original changed |
| 403 | 403 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 404 | 404 | }); |
| 405 | | } |
| 405 | }; |
| 406 | 406 | |
| 407 | 407 | /** |
| 408 | 408 | * Insert item(s) at a specific index in an array. |
| 409 | 409 | * If the query or path doesn't exist, the updater is skipped. |
| 410 | 410 | */ |
| 411 | | objArrayInsertIndex< |
| 411 | objArrayInsertIndex = < |
| 412 | 412 | Data extends object, |
| 413 | 413 | const Path extends AllObjectPaths<Data>, |
| 414 | 414 | >( |
| ... | ... | @@ -417,7 +417,7 @@ class TanstackQueryOptimisticHelpers { |
| 417 | 417 | index: number, |
| 418 | 418 | ...items: GetObjectPath<Data, Path> extends Array<infer T> ? T[] |
| 419 | 419 | : never |
| 420 | | ) { |
| 420 | ) => { |
| 421 | 421 | const prev = this.#get(queryKey); |
| 422 | 422 | if (!prev) return; |
| 423 | 423 | const { value: original, exists } = getPath(prev, path); |
| ... | ... | @@ -436,13 +436,13 @@ class TanstackQueryOptimisticHelpers { |
| 436 | 436 | // TODO: splice items back in case original changed |
| 437 | 437 | this.#set(queryKey, (obj) => obj ? setPath(obj, path, original) : obj); |
| 438 | 438 | }); |
| 439 | | } |
| 439 | }; |
| 440 | 440 | |
| 441 | 441 | /** |
| 442 | 442 | * Push item(s) to the end of an array at an object path. |
| 443 | 443 | * If the query or path doesn't exist, the updater is skipped. |
| 444 | 444 | */ |
| 445 | | arrayPush<Data>(queryKey: QueryKeyAndFn<Data[] | null>, ...items: Data[]) { |
| 445 | arrayPush = <Data>(queryKey: QueryKeyAndFn<Data[] | null>, ...items: Data[]) => { |
| 446 | 446 | const prev = this.#get(queryKey); |
| 447 | 447 | if (!prev || !Array.isArray(prev)) return; |
| 448 | 448 | |
| ... | ... | @@ -454,13 +454,13 @@ class TanstackQueryOptimisticHelpers { |
| 454 | 454 | (old) => old ? old.filter((x) => !items.includes(x)) : old, |
| 455 | 455 | ); |
| 456 | 456 | }); |
| 457 | | } |
| 457 | }; |
| 458 | 458 | |
| 459 | 459 | /** |
| 460 | 460 | * Add item(s) to the beginning of an array at an object path. |
| 461 | 461 | * If the query or path doesn't exist, the updater is skipped. |
| 462 | 462 | */ |
| 463 | | arrayUnshift<Data>(queryKey: QueryKeyAndFn<Data[] | null>, ...items: Data[]) { |
| 463 | arrayUnshift = <Data>(queryKey: QueryKeyAndFn<Data[] | null>, ...items: Data[]) => { |
| 464 | 464 | const prev = this.#get(queryKey); |
| 465 | 465 | if (!prev || !Array.isArray(prev)) return; |
| 466 | 466 | |
| ... | ... | @@ -472,19 +472,19 @@ class TanstackQueryOptimisticHelpers { |
| 472 | 472 | (old) => old ? old.filter((x) => !items.includes(x)) : old, |
| 473 | 473 | ); |
| 474 | 474 | }); |
| 475 | | } |
| 475 | }; |
| 476 | 476 | |
| 477 | 477 | /** |
| 478 | 478 | * Remove items from an array that match a `filter`. |
| 479 | 479 | * If the query or path doesn't exist, the updater is skipped. |
| 480 | 480 | */ |
| 481 | | arrayRemove<Data>( |
| 481 | arrayRemove = <Data>( |
| 482 | 482 | queryKey: QueryKeyAndFn<Data[] | null>, |
| 483 | 483 | filter: ( |
| 484 | 484 | item: Data, |
| 485 | 485 | index: number, |
| 486 | 486 | ) => boolean, |
| 487 | | ) { |
| 487 | ) => { |
| 488 | 488 | const prev = this.#get(queryKey); |
| 489 | 489 | if (!prev || !Array.isArray(prev)) return; |
| 490 | 490 | |
| ... | ... | @@ -494,19 +494,19 @@ class TanstackQueryOptimisticHelpers { |
| 494 | 494 | // TODO: splice items back in case original changed |
| 495 | 495 | this.#set(queryKey, prev); |
| 496 | 496 | }); |
| 497 | | } |
| 497 | }; |
| 498 | 498 | |
| 499 | 499 | /** |
| 500 | 500 | * Filter items to just include items that match `filter`. This is the inverse of `arrayRemove` |
| 501 | 501 | * If the query or path doesn't exist, the updater is skipped. |
| 502 | 502 | */ |
| 503 | | arrayFilter<Data>( |
| 503 | arrayFilter = <Data>( |
| 504 | 504 | queryKey: QueryKeyAndFn<Data[] | null>, |
| 505 | 505 | filter: ( |
| 506 | 506 | item: Data, |
| 507 | 507 | index: number, |
| 508 | 508 | ) => boolean, |
| 509 | | ) { |
| 509 | ) => { |
| 510 | 510 | const prev = this.#get(queryKey); |
| 511 | 511 | if (!prev || !Array.isArray(prev)) return; |
| 512 | 512 | |
| ... | ... | @@ -516,13 +516,13 @@ class TanstackQueryOptimisticHelpers { |
| 516 | 516 | // TODO: splice items back in case original changed |
| 517 | 517 | this.#set(queryKey, prev); |
| 518 | 518 | }); |
| 519 | | } |
| 519 | }; |
| 520 | 520 | |
| 521 | 521 | /** |
| 522 | 522 | * Update items in an array that match a `filter`. |
| 523 | 523 | * If the query or path doesn't exist, the updater is skipped. |
| 524 | 524 | */ |
| 525 | | arrayUpdate<Data>( |
| 525 | arrayUpdate = <Data>( |
| 526 | 526 | queryKey: QueryKeyAndFn<Data[] | null>, |
| 527 | 527 | { |
| 528 | 528 | filter, |
| ... | ... | @@ -531,7 +531,7 @@ class TanstackQueryOptimisticHelpers { |
| 531 | 531 | filter?: (item: Data, index: number) => boolean; |
| 532 | 532 | update: (item: Data) => Data; |
| 533 | 533 | }, |
| 534 | | ) { |
| 534 | ) => { |
| 535 | 535 | const prev = this.#get(queryKey); |
| 536 | 536 | if (!prev || !Array.isArray(prev)) return; |
| 537 | 537 | |
| ... | ... | @@ -541,17 +541,17 @@ class TanstackQueryOptimisticHelpers { |
| 541 | 541 | // TODO: splice items back in case original changed |
| 542 | 542 | this.#set(queryKey, prev); |
| 543 | 543 | }); |
| 544 | | } |
| 544 | }; |
| 545 | 545 | |
| 546 | 546 | /** |
| 547 | 547 | * Insert item(s) at a specific index in an array. |
| 548 | 548 | * If the query or path doesn't exist, the updater is skipped. |
| 549 | 549 | */ |
| 550 | | arrayInsertIndex<Data>( |
| 550 | arrayInsertIndex = <Data>( |
| 551 | 551 | queryKey: QueryKeyAndFn<Data[] | null>, |
| 552 | 552 | index: number, |
| 553 | 553 | ...items: Data[] |
| 554 | | ) { |
| 554 | ) => { |
| 555 | 555 | const prev = this.#get(queryKey); |
| 556 | 556 | if (!prev || !Array.isArray(prev)) return; |
| 557 | 557 | |
| ... | ... | @@ -565,12 +565,12 @@ class TanstackQueryOptimisticHelpers { |
| 565 | 565 | // TODO: splice items back in case original changed |
| 566 | 566 | this.#set(queryKey, prev); |
| 567 | 567 | }); |
| 568 | | } |
| 568 | }; |
| 569 | 569 | |
| 570 | 570 | /** |
| 571 | 571 | * Remove a query from the cache entirely. |
| 572 | 572 | */ |
| 573 | | removeQuery(queryKey: QueryKeyAndFn) { |
| 573 | removeQuery = (queryKey: QueryKeyAndFn) => { |
| 574 | 574 | const prev = this.#get(queryKey); |
| 575 | 575 | if (!prev) return; |
| 576 | 576 | |
| ... | ... | @@ -578,7 +578,7 @@ class TanstackQueryOptimisticHelpers { |
| 578 | 578 | this.#onRestore(() => { |
| 579 | 579 | this.#set(queryKey, prev); |
| 580 | 580 | }); |
| 581 | | } |
| 581 | }; |
| 582 | 582 | } |
| 583 | 583 | |
| 584 | 584 | export function queryClientOptimisticHelpers( |