@tanstack/preact-query 5.104.3 → 5.104.5

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.
Files changed (49) hide show
  1. package/build/legacy/errorBoundaryUtils.cjs +1 -1
  2. package/build/legacy/errorBoundaryUtils.cjs.map +1 -1
  3. package/build/legacy/errorBoundaryUtils.d.cts.map +1 -1
  4. package/build/legacy/errorBoundaryUtils.d.ts.map +1 -1
  5. package/build/legacy/errorBoundaryUtils.js +1 -1
  6. package/build/legacy/errorBoundaryUtils.js.map +1 -1
  7. package/build/legacy/index.cjs +0 -1
  8. package/build/legacy/index.cjs.map +1 -1
  9. package/build/legacy/index.d.cts +1 -1
  10. package/build/legacy/index.d.ts +1 -1
  11. package/build/legacy/index.js +0 -1
  12. package/build/legacy/index.js.map +1 -1
  13. package/build/legacy/useBaseQuery.cjs +3 -2
  14. package/build/legacy/useBaseQuery.cjs.map +1 -1
  15. package/build/legacy/useBaseQuery.js +3 -2
  16. package/build/legacy/useBaseQuery.js.map +1 -1
  17. package/build/legacy/useMutationState.cjs +23 -20
  18. package/build/legacy/useMutationState.cjs.map +1 -1
  19. package/build/legacy/useMutationState.d.cts.map +1 -1
  20. package/build/legacy/useMutationState.d.ts.map +1 -1
  21. package/build/legacy/useMutationState.js +24 -21
  22. package/build/legacy/useMutationState.js.map +1 -1
  23. package/build/modern/errorBoundaryUtils.cjs +1 -1
  24. package/build/modern/errorBoundaryUtils.cjs.map +1 -1
  25. package/build/modern/errorBoundaryUtils.d.cts.map +1 -1
  26. package/build/modern/errorBoundaryUtils.d.ts.map +1 -1
  27. package/build/modern/errorBoundaryUtils.js +1 -1
  28. package/build/modern/errorBoundaryUtils.js.map +1 -1
  29. package/build/modern/index.cjs +0 -1
  30. package/build/modern/index.cjs.map +1 -1
  31. package/build/modern/index.d.cts +1 -1
  32. package/build/modern/index.d.ts +1 -1
  33. package/build/modern/index.js +0 -1
  34. package/build/modern/index.js.map +1 -1
  35. package/build/modern/useBaseQuery.cjs +3 -2
  36. package/build/modern/useBaseQuery.cjs.map +1 -1
  37. package/build/modern/useBaseQuery.js +3 -2
  38. package/build/modern/useBaseQuery.js.map +1 -1
  39. package/build/modern/useMutationState.cjs +23 -20
  40. package/build/modern/useMutationState.cjs.map +1 -1
  41. package/build/modern/useMutationState.d.cts.map +1 -1
  42. package/build/modern/useMutationState.d.ts.map +1 -1
  43. package/build/modern/useMutationState.js +24 -21
  44. package/build/modern/useMutationState.js.map +1 -1
  45. package/package.json +3 -3
  46. package/src/errorBoundaryUtils.ts +6 -2
  47. package/src/index.ts +1 -1
  48. package/src/useBaseQuery.ts +6 -2
  49. package/src/useMutationState.ts +52 -41
@@ -1,6 +1,6 @@
1
- import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
2
- import { useCallback, useEffect, useRef } from 'preact/hooks'
1
+ import { useCallback, useState } from 'preact/hooks'
3
2
 
3
+ import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
4
4
  import { useQueryClient } from './QueryClientProvider'
5
5
  import { useSyncExternalStore } from './utils'
6
6
  import type {
@@ -62,28 +62,53 @@ type MutationStateOptions<
62
62
  }
63
63
 
64
64
  /**
65
- * Collects the mutations in the cache that match `options.filters`, mapped with `options.select`
66
- * (or to their state, by default).
67
- * @param mutationCache - The mutation cache to read.
68
- * @param options - The `filters` to match and the `select` function to map each mutation with.
69
- * @returns The selected value of every matching mutation.
65
+ * Creates snapshot getters that share equal selected results across renders.
66
+ * A new getter is created on each render. Each getter caches its snapshot until the matching mutations
67
+ * or their states change.
68
+ * @returns A factory for snapshot getters with a shared result cache.
70
69
  */
71
- function getResult<
70
+ function createSnapshotFactory<
72
71
  TResult = MutationState,
73
72
  TMutation extends Mutation<any, any, any, any> =
74
73
  MutationTypeFromResult<TResult>,
75
- >(
76
- mutationCache: MutationCache,
77
- options: MutationStateOptions<TResult, TMutation>,
78
- ): Array<TResult> {
79
- return mutationCache
80
- .findAll(options.filters)
81
- .map(
82
- (mutation): TResult =>
83
- (options.select
84
- ? options.select(mutation as TMutation)
85
- : mutation.state) as TResult,
86
- )
74
+ >() {
75
+ let previousResult: Array<TResult> | undefined
76
+
77
+ return (
78
+ mutationCache: MutationCache,
79
+ options: MutationStateOptions<TResult, TMutation>,
80
+ ) => {
81
+ let inputs: Array<unknown> = []
82
+ let snapshot: Array<TResult> | undefined
83
+
84
+ return (): Array<TResult> => {
85
+ const mutations = mutationCache.findAll(options.filters)
86
+ const nextInputs = mutations.flatMap((mutation) => [
87
+ mutation,
88
+ mutation.state,
89
+ ])
90
+
91
+ if (
92
+ !snapshot ||
93
+ nextInputs.length !== inputs.length ||
94
+ nextInputs.some((input, index) => !Object.is(input, inputs[index]))
95
+ ) {
96
+ inputs = nextInputs
97
+ snapshot = replaceEqualDeep(
98
+ previousResult,
99
+ mutations.map(
100
+ (mutation): TResult =>
101
+ (options.select
102
+ ? options.select(mutation as TMutation)
103
+ : mutation.state) as TResult,
104
+ ),
105
+ )
106
+ previousResult = snapshot
107
+ }
108
+
109
+ return snapshot
110
+ }
111
+ }
87
112
  }
88
113
 
89
114
  /**
@@ -164,31 +189,17 @@ export function useMutationState<
164
189
  queryClient?: QueryClient,
165
190
  ): Array<TResult> {
166
191
  const mutationCache = useQueryClient(queryClient).getMutationCache()
167
- const optionsRef = useRef(options)
168
- const resultRef = useRef<Array<TResult>>(null)
169
- if (resultRef.current === null) {
170
- resultRef.current = getResult(mutationCache, options)
171
- }
172
-
173
- useEffect(() => {
174
- optionsRef.current = options
175
- })
192
+ const [createSnapshot] = useState(() =>
193
+ createSnapshotFactory<TResult, TMutation>(),
194
+ )
195
+ const getSnapshot = createSnapshot(mutationCache, options)
176
196
 
177
197
  return useSyncExternalStore(
178
198
  useCallback(
179
199
  (onStoreChange) =>
180
- mutationCache.subscribe(() => {
181
- const nextResult = replaceEqualDeep(
182
- resultRef.current,
183
- getResult(mutationCache, optionsRef.current),
184
- )
185
- if (resultRef.current !== nextResult) {
186
- resultRef.current = nextResult
187
- notifyManager.schedule(onStoreChange)
188
- }
189
- }),
200
+ mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),
190
201
  [mutationCache],
191
202
  ),
192
- () => resultRef.current,
193
- )!
203
+ getSnapshot,
204
+ )
194
205
  }