@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.
- package/build/legacy/errorBoundaryUtils.cjs +1 -1
- package/build/legacy/errorBoundaryUtils.cjs.map +1 -1
- package/build/legacy/errorBoundaryUtils.d.cts.map +1 -1
- package/build/legacy/errorBoundaryUtils.d.ts.map +1 -1
- package/build/legacy/errorBoundaryUtils.js +1 -1
- package/build/legacy/errorBoundaryUtils.js.map +1 -1
- package/build/legacy/index.cjs +0 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +1 -1
- package/build/legacy/index.d.ts +1 -1
- package/build/legacy/index.js +0 -1
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/useBaseQuery.cjs +3 -2
- package/build/legacy/useBaseQuery.cjs.map +1 -1
- package/build/legacy/useBaseQuery.js +3 -2
- package/build/legacy/useBaseQuery.js.map +1 -1
- package/build/legacy/useMutationState.cjs +23 -20
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +24 -21
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/modern/errorBoundaryUtils.cjs +1 -1
- package/build/modern/errorBoundaryUtils.cjs.map +1 -1
- package/build/modern/errorBoundaryUtils.d.cts.map +1 -1
- package/build/modern/errorBoundaryUtils.d.ts.map +1 -1
- package/build/modern/errorBoundaryUtils.js +1 -1
- package/build/modern/errorBoundaryUtils.js.map +1 -1
- package/build/modern/index.cjs +0 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +1 -1
- package/build/modern/index.d.ts +1 -1
- package/build/modern/index.js +0 -1
- package/build/modern/index.js.map +1 -1
- package/build/modern/useBaseQuery.cjs +3 -2
- package/build/modern/useBaseQuery.cjs.map +1 -1
- package/build/modern/useBaseQuery.js +3 -2
- package/build/modern/useBaseQuery.js.map +1 -1
- package/build/modern/useMutationState.cjs +23 -20
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +24 -21
- package/build/modern/useMutationState.js.map +1 -1
- package/package.json +3 -3
- package/src/errorBoundaryUtils.ts +6 -2
- package/src/index.ts +1 -1
- package/src/useBaseQuery.ts +6 -2
- package/src/useMutationState.ts +52 -41
package/src/useMutationState.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
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
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
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
|
|
70
|
+
function createSnapshotFactory<
|
|
72
71
|
TResult = MutationState,
|
|
73
72
|
TMutation extends Mutation<any, any, any, any> =
|
|
74
73
|
MutationTypeFromResult<TResult>,
|
|
75
|
-
>(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
193
|
-
)
|
|
203
|
+
getSnapshot,
|
|
204
|
+
)
|
|
194
205
|
}
|