@tanstack/query-core 5.0.0-alpha.3 → 5.0.0-alpha.32
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/lib/_virtual/_rollupPluginBabelHelpers.esm.js +13 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +16 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/lib/focusManager.d.ts +1 -0
- package/build/lib/focusManager.d.ts.map +1 -0
- package/build/lib/focusManager.esm.js +31 -15
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +31 -15
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/hydration.d.ts +3 -6
- package/build/lib/hydration.d.ts.map +1 -0
- package/build/lib/hydration.esm.js +9 -20
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +9 -20
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +4 -18
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +4 -3
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/infiniteQueryBehavior.d.ts +3 -2
- package/build/lib/infiniteQueryBehavior.d.ts.map +1 -0
- package/build/lib/infiniteQueryBehavior.esm.js +52 -48
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +52 -48
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +48 -45
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +7 -6
- package/build/lib/infiniteQueryObserver.d.ts.map +1 -0
- package/build/lib/infiniteQueryObserver.esm.js +7 -8
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +7 -8
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +4 -6
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +1 -0
- package/build/lib/mutation.d.ts.map +1 -0
- package/build/lib/mutation.esm.js +127 -102
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +127 -102
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutationCache.d.ts +1 -0
- package/build/lib/mutationCache.d.ts.map +1 -0
- package/build/lib/mutationCache.esm.js +34 -20
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +34 -20
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +5 -4
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.d.ts +1 -0
- package/build/lib/mutationObserver.d.ts.map +1 -0
- package/build/lib/mutationObserver.esm.js +82 -52
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +82 -52
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +1 -1
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/notifyManager.d.ts +1 -0
- package/build/lib/notifyManager.d.ts.map +1 -0
- package/build/lib/onlineManager.d.ts +1 -0
- package/build/lib/onlineManager.d.ts.map +1 -0
- package/build/lib/onlineManager.esm.js +29 -14
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +29 -14
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/queriesObserver.d.ts +1 -0
- package/build/lib/queriesObserver.d.ts.map +1 -0
- package/build/lib/queriesObserver.esm.js +98 -68
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +98 -68
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +7 -6
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +5 -3
- package/build/lib/query.d.ts.map +1 -0
- package/build/lib/query.esm.js +211 -162
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +211 -162
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +2 -2
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +1 -1
- package/build/lib/queryCache.d.ts.map +1 -0
- package/build/lib/queryCache.esm.js +20 -13
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +20 -13
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +6 -5
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.d.ts +1 -0
- package/build/lib/queryClient.d.ts.map +1 -0
- package/build/lib/queryClient.esm.js +113 -71
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +113 -71
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +17 -13
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +3 -4
- package/build/lib/queryObserver.d.ts.map +1 -0
- package/build/lib/queryObserver.esm.js +303 -214
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +301 -212
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +16 -28
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.d.ts +1 -0
- package/build/lib/removable.d.ts.map +1 -0
- package/build/lib/removable.esm.js +13 -6
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +13 -6
- package/build/lib/removable.js.map +1 -1
- package/build/lib/retryer.d.ts +1 -0
- package/build/lib/retryer.d.ts.map +1 -0
- package/build/lib/retryer.esm.js +15 -14
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +15 -14
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/subscribable.d.ts +2 -1
- package/build/lib/subscribable.d.ts.map +1 -0
- package/build/lib/subscribable.esm.js +4 -4
- package/build/lib/subscribable.esm.js.map +1 -1
- package/build/lib/subscribable.js +4 -4
- package/build/lib/subscribable.js.map +1 -1
- package/build/lib/subscribable.mjs +4 -4
- package/build/lib/subscribable.mjs.map +1 -1
- package/build/lib/tests/focusManager.test.d.ts +1 -0
- package/build/lib/tests/focusManager.test.d.ts.map +1 -0
- package/build/lib/tests/hydration.test.d.ts +1 -0
- package/build/lib/tests/hydration.test.d.ts.map +1 -0
- package/build/lib/tests/infiniteQueryBehavior.test.d.ts +1 -0
- package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +1 -0
- package/build/lib/tests/infiniteQueryObserver.test.d.ts +1 -0
- package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +1 -0
- package/build/lib/tests/mutationCache.test.d.ts +1 -0
- package/build/lib/tests/mutationCache.test.d.ts.map +1 -0
- package/build/lib/tests/mutationObserver.test.d.ts +1 -0
- package/build/lib/tests/mutationObserver.test.d.ts.map +1 -0
- package/build/lib/tests/mutations.test.d.ts +1 -0
- package/build/lib/tests/mutations.test.d.ts.map +1 -0
- package/build/lib/tests/notifyManager.test.d.ts +1 -0
- package/build/lib/tests/notifyManager.test.d.ts.map +1 -0
- package/build/lib/tests/onlineManager.test.d.ts +1 -0
- package/build/lib/tests/onlineManager.test.d.ts.map +1 -0
- package/build/lib/tests/queriesObserver.test.d.ts +1 -0
- package/build/lib/tests/queriesObserver.test.d.ts.map +1 -0
- package/build/lib/tests/query.test.d.ts +1 -0
- package/build/lib/tests/query.test.d.ts.map +1 -0
- package/build/lib/tests/queryCache.test.d.ts +1 -0
- package/build/lib/tests/queryCache.test.d.ts.map +1 -0
- package/build/lib/tests/queryClient.test.d.ts +1 -0
- package/build/lib/tests/queryClient.test.d.ts.map +1 -0
- package/build/lib/tests/queryObserver.test.d.ts +1 -0
- package/build/lib/tests/queryObserver.test.d.ts.map +1 -0
- package/build/lib/tests/utils.d.ts +5 -5
- package/build/lib/tests/utils.d.ts.map +1 -0
- package/build/lib/tests/utils.test.d.ts +1 -0
- package/build/lib/tests/utils.test.d.ts.map +1 -0
- package/build/lib/types.d.ts +12 -21
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/lib/utils.esm.js +1 -1
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/umd/index.development.js +113 -131
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/hydration.ts +18 -37
- package/src/index.ts +2 -4
- package/src/infiniteQueryBehavior.ts +52 -60
- package/src/infiniteQueryObserver.ts +15 -10
- package/src/mutationCache.ts +4 -4
- package/src/mutationObserver.ts +1 -1
- package/src/queriesObserver.ts +11 -11
- package/src/query.ts +8 -6
- package/src/queryCache.ts +5 -8
- package/src/queryClient.ts +18 -19
- package/src/queryObserver.ts +32 -41
- package/src/subscribable.ts +5 -5
- package/src/tests/focusManager.test.tsx +12 -14
- package/src/tests/hydration.test.tsx +22 -17
- package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
- package/src/tests/infiniteQueryObserver.test.tsx +62 -1
- package/src/tests/mutationCache.test.tsx +11 -10
- package/src/tests/mutationObserver.test.tsx +3 -2
- package/src/tests/mutations.test.tsx +11 -10
- package/src/tests/notifyManager.test.tsx +7 -6
- package/src/tests/onlineManager.test.tsx +12 -17
- package/src/tests/queriesObserver.test.tsx +18 -17
- package/src/tests/query.test.tsx +19 -18
- package/src/tests/queryCache.test.tsx +12 -28
- package/src/tests/queryClient.test.tsx +82 -49
- package/src/tests/queryObserver.test.tsx +131 -9
- package/src/tests/utils.test.tsx +2 -1
- package/src/tests/utils.ts +5 -4
- package/src/types.ts +22 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.32",
|
|
4
4
|
"description": "The framework agnostic core that powers TanStack Query",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"clean": "rimraf ./build",
|
|
32
32
|
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
33
33
|
"test:types": "tsc",
|
|
34
|
-
"test:lib": "
|
|
34
|
+
"test:lib": "vitest run --coverage",
|
|
35
35
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
36
36
|
"build:types": "tsc --build"
|
|
37
37
|
}
|
package/src/hydration.ts
CHANGED
|
@@ -11,10 +11,8 @@ import type { Mutation, MutationState } from './mutation'
|
|
|
11
11
|
// TYPES
|
|
12
12
|
|
|
13
13
|
export interface DehydrateOptions {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
shouldDehydrateMutation?: ShouldDehydrateMutationFunction
|
|
17
|
-
shouldDehydrateQuery?: ShouldDehydrateQueryFunction
|
|
14
|
+
shouldDehydrateMutation?: (mutation: Mutation) => boolean
|
|
15
|
+
shouldDehydrateQuery?: (query: Query) => boolean
|
|
18
16
|
}
|
|
19
17
|
|
|
20
18
|
export interface HydrateOptions {
|
|
@@ -40,10 +38,6 @@ export interface DehydratedState {
|
|
|
40
38
|
queries: DehydratedQuery[]
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
export type ShouldDehydrateQueryFunction = (query: Query) => boolean
|
|
44
|
-
|
|
45
|
-
export type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean
|
|
46
|
-
|
|
47
41
|
// FUNCTIONS
|
|
48
42
|
|
|
49
43
|
function dehydrateMutation(mutation: Mutation): DehydratedMutation {
|
|
@@ -77,36 +71,23 @@ export function dehydrate(
|
|
|
77
71
|
client: QueryClient,
|
|
78
72
|
options: DehydrateOptions = {},
|
|
79
73
|
): DehydratedState {
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.getAll()
|
|
90
|
-
.forEach((mutation) => {
|
|
91
|
-
if (shouldDehydrateMutation(mutation)) {
|
|
92
|
-
mutations.push(dehydrateMutation(mutation))
|
|
93
|
-
}
|
|
94
|
-
})
|
|
95
|
-
}
|
|
74
|
+
const filterMutation =
|
|
75
|
+
options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation
|
|
76
|
+
|
|
77
|
+
const mutations = client
|
|
78
|
+
.getMutationCache()
|
|
79
|
+
.getAll()
|
|
80
|
+
.flatMap((mutation) =>
|
|
81
|
+
filterMutation(mutation) ? [dehydrateMutation(mutation)] : [],
|
|
82
|
+
)
|
|
96
83
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.forEach((query) => {
|
|
105
|
-
if (shouldDehydrateQuery(query)) {
|
|
106
|
-
queries.push(dehydrateQuery(query))
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
}
|
|
84
|
+
const filterQuery =
|
|
85
|
+
options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery
|
|
86
|
+
|
|
87
|
+
const queries = client
|
|
88
|
+
.getQueryCache()
|
|
89
|
+
.getAll()
|
|
90
|
+
.flatMap((query) => (filterQuery(query) ? [dehydrateQuery(query)] : []))
|
|
110
91
|
|
|
111
92
|
return { mutations, queries }
|
|
112
93
|
}
|
package/src/index.ts
CHANGED
|
@@ -23,18 +23,16 @@ export { isCancelledError } from './retryer'
|
|
|
23
23
|
export {
|
|
24
24
|
dehydrate,
|
|
25
25
|
hydrate,
|
|
26
|
-
defaultShouldDehydrateMutation,
|
|
27
26
|
defaultShouldDehydrateQuery,
|
|
27
|
+
defaultShouldDehydrateMutation,
|
|
28
28
|
} from './hydration'
|
|
29
29
|
|
|
30
30
|
// Types
|
|
31
31
|
export * from './types'
|
|
32
32
|
export type { Query, QueryState } from './query'
|
|
33
|
-
export type { Mutation } from './mutation'
|
|
33
|
+
export type { Mutation, MutationState } from './mutation'
|
|
34
34
|
export type {
|
|
35
35
|
DehydrateOptions,
|
|
36
36
|
DehydratedState,
|
|
37
37
|
HydrateOptions,
|
|
38
|
-
ShouldDehydrateMutationFunction,
|
|
39
|
-
ShouldDehydrateQueryFunction,
|
|
40
38
|
} from './hydration'
|
|
@@ -14,12 +14,12 @@ export function infiniteQueryBehavior<
|
|
|
14
14
|
>(): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
|
|
15
15
|
return {
|
|
16
16
|
onFetch: (context) => {
|
|
17
|
-
context.fetchFn = () => {
|
|
17
|
+
context.fetchFn = async () => {
|
|
18
18
|
const options = context.options as InfiniteQueryPageParamsOptions<TData>
|
|
19
19
|
const direction = context.fetchOptions?.meta?.fetchMore?.direction
|
|
20
20
|
const oldPages = context.state.data?.pages || []
|
|
21
21
|
const oldPageParams = context.state.data?.pageParams || []
|
|
22
|
-
|
|
22
|
+
const empty = { pages: [], pageParams: [] }
|
|
23
23
|
let cancelled = false
|
|
24
24
|
|
|
25
25
|
const addSignalProperty = (object: unknown) => {
|
|
@@ -43,35 +43,18 @@ export function infiniteQueryBehavior<
|
|
|
43
43
|
context.options.queryFn ||
|
|
44
44
|
(() => Promise.reject(new Error('Missing queryFn')))
|
|
45
45
|
|
|
46
|
-
const buildNewPages = (
|
|
47
|
-
pages: unknown[],
|
|
48
|
-
param: unknown,
|
|
49
|
-
page: unknown,
|
|
50
|
-
previous?: boolean,
|
|
51
|
-
) => {
|
|
52
|
-
const { maxPages } = context.options
|
|
53
|
-
|
|
54
|
-
if (previous) {
|
|
55
|
-
newPageParams = addToStart(newPageParams, param, maxPages)
|
|
56
|
-
return addToStart(pages, page, maxPages)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
newPageParams = addToEnd(newPageParams, param, maxPages)
|
|
60
|
-
return addToEnd(pages, page, maxPages)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
46
|
// Create function to fetch a page
|
|
64
|
-
const fetchPage = (
|
|
65
|
-
|
|
47
|
+
const fetchPage = async (
|
|
48
|
+
data: InfiniteData<unknown>,
|
|
66
49
|
param: unknown,
|
|
67
50
|
previous?: boolean,
|
|
68
|
-
): Promise<unknown
|
|
51
|
+
): Promise<InfiniteData<unknown>> => {
|
|
69
52
|
if (cancelled) {
|
|
70
53
|
return Promise.reject()
|
|
71
54
|
}
|
|
72
55
|
|
|
73
|
-
if (typeof param === 'undefined' && pages.length) {
|
|
74
|
-
return Promise.resolve(
|
|
56
|
+
if (typeof param === 'undefined' && data.pages.length) {
|
|
57
|
+
return Promise.resolve(data)
|
|
75
58
|
}
|
|
76
59
|
|
|
77
60
|
const queryFnContext: Omit<
|
|
@@ -80,60 +63,58 @@ export function infiniteQueryBehavior<
|
|
|
80
63
|
> = {
|
|
81
64
|
queryKey: context.queryKey,
|
|
82
65
|
pageParam: param,
|
|
66
|
+
direction: previous ? 'backward' : 'forward',
|
|
83
67
|
meta: context.options.meta,
|
|
84
68
|
}
|
|
85
69
|
|
|
86
70
|
addSignalProperty(queryFnContext)
|
|
87
71
|
|
|
88
|
-
const
|
|
72
|
+
const page = await queryFn(
|
|
89
73
|
queryFnContext as QueryFunctionContext<QueryKey, unknown>,
|
|
90
74
|
)
|
|
91
75
|
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
)
|
|
76
|
+
const { maxPages } = context.options
|
|
77
|
+
const addTo = previous ? addToStart : addToEnd
|
|
95
78
|
|
|
96
|
-
return
|
|
79
|
+
return {
|
|
80
|
+
pages: addTo(data.pages, page, maxPages),
|
|
81
|
+
pageParams: addTo(data.pageParams, param, maxPages),
|
|
82
|
+
}
|
|
97
83
|
}
|
|
98
84
|
|
|
99
|
-
let
|
|
85
|
+
let result: InfiniteData<unknown>
|
|
100
86
|
|
|
101
87
|
// Fetch first page?
|
|
102
88
|
if (!oldPages.length) {
|
|
103
|
-
|
|
89
|
+
result = await fetchPage(empty, options.defaultPageParam)
|
|
104
90
|
}
|
|
105
91
|
|
|
106
92
|
// fetch next / previous page?
|
|
107
93
|
else if (direction) {
|
|
108
94
|
const previous = direction === 'backward'
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
:
|
|
112
|
-
|
|
95
|
+
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
|
|
96
|
+
const oldData = {
|
|
97
|
+
pages: oldPages,
|
|
98
|
+
pageParams: oldPageParams,
|
|
99
|
+
}
|
|
100
|
+
const param = pageParamFn(options, oldData)
|
|
101
|
+
|
|
102
|
+
result = await fetchPage(oldData, param, previous)
|
|
113
103
|
}
|
|
114
104
|
|
|
115
105
|
// Refetch pages
|
|
116
106
|
else {
|
|
117
|
-
newPageParams = []
|
|
118
|
-
|
|
119
107
|
// Fetch first page
|
|
120
|
-
|
|
108
|
+
result = await fetchPage(empty, oldPageParams[0])
|
|
121
109
|
|
|
122
110
|
// Fetch remaining pages
|
|
123
111
|
for (let i = 1; i < oldPages.length; i++) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return fetchPage(pages, param)
|
|
127
|
-
})
|
|
112
|
+
const param = getNextPageParam(options, result)
|
|
113
|
+
result = await fetchPage(result, param)
|
|
128
114
|
}
|
|
129
115
|
}
|
|
130
116
|
|
|
131
|
-
|
|
132
|
-
pages,
|
|
133
|
-
pageParams: newPageParams,
|
|
134
|
-
}))
|
|
135
|
-
|
|
136
|
-
return finalPromise
|
|
117
|
+
return result
|
|
137
118
|
}
|
|
138
119
|
},
|
|
139
120
|
}
|
|
@@ -141,36 +122,47 @@ export function infiniteQueryBehavior<
|
|
|
141
122
|
|
|
142
123
|
function getNextPageParam(
|
|
143
124
|
options: InfiniteQueryPageParamsOptions<any>,
|
|
144
|
-
pages: unknown
|
|
125
|
+
{ pages, pageParams }: InfiniteData<unknown>,
|
|
145
126
|
): unknown | undefined {
|
|
146
|
-
|
|
127
|
+
const lastIndex = pages.length - 1
|
|
128
|
+
return options.getNextPageParam(
|
|
129
|
+
pages[lastIndex],
|
|
130
|
+
pages,
|
|
131
|
+
pageParams[lastIndex],
|
|
132
|
+
pageParams,
|
|
133
|
+
)
|
|
147
134
|
}
|
|
148
135
|
|
|
149
136
|
function getPreviousPageParam(
|
|
150
137
|
options: InfiniteQueryPageParamsOptions<any>,
|
|
151
|
-
pages: unknown
|
|
138
|
+
{ pages, pageParams }: InfiniteData<unknown>,
|
|
152
139
|
): unknown | undefined {
|
|
153
|
-
return options.getPreviousPageParam?.(
|
|
140
|
+
return options.getPreviousPageParam?.(
|
|
141
|
+
pages[0],
|
|
142
|
+
pages,
|
|
143
|
+
pageParams[0],
|
|
144
|
+
pageParams,
|
|
145
|
+
)
|
|
154
146
|
}
|
|
155
147
|
|
|
156
148
|
/**
|
|
157
149
|
* Checks if there is a next page.
|
|
158
150
|
*/
|
|
159
151
|
export function hasNextPage(
|
|
160
|
-
options: InfiniteQueryPageParamsOptions<any>,
|
|
161
|
-
|
|
152
|
+
options: InfiniteQueryPageParamsOptions<any, any>,
|
|
153
|
+
data?: InfiniteData<unknown>,
|
|
162
154
|
): boolean {
|
|
163
|
-
if (!
|
|
164
|
-
return typeof getNextPageParam(options,
|
|
155
|
+
if (!data) return false
|
|
156
|
+
return typeof getNextPageParam(options, data) !== 'undefined'
|
|
165
157
|
}
|
|
166
158
|
|
|
167
159
|
/**
|
|
168
160
|
* Checks if there is a previous page.
|
|
169
161
|
*/
|
|
170
162
|
export function hasPreviousPage(
|
|
171
|
-
options: InfiniteQueryPageParamsOptions<any>,
|
|
172
|
-
|
|
163
|
+
options: InfiniteQueryPageParamsOptions<any, any>,
|
|
164
|
+
data?: InfiniteData<unknown>,
|
|
173
165
|
): boolean {
|
|
174
|
-
if (!
|
|
175
|
-
return typeof getPreviousPageParam(options,
|
|
166
|
+
if (!data || !options.getPreviousPageParam) return false
|
|
167
|
+
return typeof getPreviousPageParam(options, data) !== 'undefined'
|
|
176
168
|
}
|
|
@@ -28,6 +28,7 @@ export class InfiniteQueryObserver<
|
|
|
28
28
|
TData = InfiniteData<TQueryFnData>,
|
|
29
29
|
TQueryData = TQueryFnData,
|
|
30
30
|
TQueryKey extends QueryKey = QueryKey,
|
|
31
|
+
TPageParam = unknown,
|
|
31
32
|
> extends QueryObserver<
|
|
32
33
|
TQueryFnData,
|
|
33
34
|
TError,
|
|
@@ -56,7 +57,8 @@ export class InfiniteQueryObserver<
|
|
|
56
57
|
TError,
|
|
57
58
|
TData,
|
|
58
59
|
TQueryData,
|
|
59
|
-
TQueryKey
|
|
60
|
+
TQueryKey,
|
|
61
|
+
TPageParam
|
|
60
62
|
>,
|
|
61
63
|
) {
|
|
62
64
|
super(client, options)
|
|
@@ -74,7 +76,8 @@ export class InfiniteQueryObserver<
|
|
|
74
76
|
TError,
|
|
75
77
|
TData,
|
|
76
78
|
TQueryData,
|
|
77
|
-
TQueryKey
|
|
79
|
+
TQueryKey,
|
|
80
|
+
TPageParam
|
|
78
81
|
>,
|
|
79
82
|
notifyOptions?: NotifyOptions,
|
|
80
83
|
): void {
|
|
@@ -93,7 +96,8 @@ export class InfiniteQueryObserver<
|
|
|
93
96
|
TError,
|
|
94
97
|
TData,
|
|
95
98
|
TQueryData,
|
|
96
|
-
TQueryKey
|
|
99
|
+
TQueryKey,
|
|
100
|
+
TPageParam
|
|
97
101
|
>,
|
|
98
102
|
): InfiniteQueryObserverResult<TData, TError> {
|
|
99
103
|
options.behavior = infiniteQueryBehavior()
|
|
@@ -104,7 +108,7 @@ export class InfiniteQueryObserver<
|
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
fetchNextPage(
|
|
107
|
-
options
|
|
111
|
+
options?: FetchNextPageOptions,
|
|
108
112
|
): Promise<InfiniteQueryObserverResult<TData, TError>> {
|
|
109
113
|
return this.fetch({
|
|
110
114
|
...options,
|
|
@@ -114,9 +118,9 @@ export class InfiniteQueryObserver<
|
|
|
114
118
|
})
|
|
115
119
|
}
|
|
116
120
|
|
|
117
|
-
fetchPreviousPage(
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
fetchPreviousPage(
|
|
122
|
+
options?: FetchPreviousPageOptions,
|
|
123
|
+
): Promise<InfiniteQueryObserverResult<TData, TError>> {
|
|
120
124
|
return this.fetch({
|
|
121
125
|
...options,
|
|
122
126
|
meta: {
|
|
@@ -132,7 +136,8 @@ export class InfiniteQueryObserver<
|
|
|
132
136
|
TError,
|
|
133
137
|
TData,
|
|
134
138
|
TQueryData,
|
|
135
|
-
TQueryKey
|
|
139
|
+
TQueryKey,
|
|
140
|
+
TPageParam
|
|
136
141
|
>,
|
|
137
142
|
): InfiniteQueryObserverResult<TData, TError> {
|
|
138
143
|
const { state } = query
|
|
@@ -150,8 +155,8 @@ export class InfiniteQueryObserver<
|
|
|
150
155
|
...result,
|
|
151
156
|
fetchNextPage: this.fetchNextPage,
|
|
152
157
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
153
|
-
hasNextPage: hasNextPage(options, state.data
|
|
154
|
-
hasPreviousPage: hasPreviousPage(options, state.data
|
|
158
|
+
hasNextPage: hasNextPage(options, state.data),
|
|
159
|
+
hasPreviousPage: hasPreviousPage(options, state.data),
|
|
155
160
|
isFetchingNextPage,
|
|
156
161
|
isFetchingPreviousPage,
|
|
157
162
|
isRefetching:
|
package/src/mutationCache.ts
CHANGED
|
@@ -139,11 +139,11 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
139
139
|
>(
|
|
140
140
|
filters: MutationFilters,
|
|
141
141
|
): Mutation<TData, TError, TVariables, TContext> | undefined {
|
|
142
|
-
|
|
143
|
-
filters.exact = true
|
|
144
|
-
}
|
|
142
|
+
const defaultedFilters = { exact: true, ...filters }
|
|
145
143
|
|
|
146
|
-
return this.#mutations.find((mutation) =>
|
|
144
|
+
return this.#mutations.find((mutation) =>
|
|
145
|
+
matchMutation(defaultedFilters, mutation),
|
|
146
|
+
)
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
findAll(filters: MutationFilters = {}): Mutation[] {
|
package/src/mutationObserver.ts
CHANGED
package/src/queriesObserver.ts
CHANGED
|
@@ -41,7 +41,7 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
protected onSubscribe(): void {
|
|
44
|
-
if (this.listeners.
|
|
44
|
+
if (this.listeners.size === 1) {
|
|
45
45
|
this.#observers.forEach((observer) => {
|
|
46
46
|
observer.subscribe((result) => {
|
|
47
47
|
this.#onUpdate(observer, result)
|
|
@@ -51,13 +51,13 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
protected onUnsubscribe(): void {
|
|
54
|
-
if (!this.listeners.
|
|
54
|
+
if (!this.listeners.size) {
|
|
55
55
|
this.destroy()
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
destroy(): void {
|
|
60
|
-
this.listeners =
|
|
60
|
+
this.listeners = new Set()
|
|
61
61
|
this.#observers.forEach((observer) => {
|
|
62
62
|
observer.destroy()
|
|
63
63
|
})
|
|
@@ -134,28 +134,28 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
134
134
|
queries: QueryObserverOptions[],
|
|
135
135
|
): QueryObserverMatch[] {
|
|
136
136
|
const prevObservers = this.#observers
|
|
137
|
+
const prevObserversMap = new Map(
|
|
138
|
+
prevObservers.map((observer) => [observer.options.queryHash, observer]),
|
|
139
|
+
)
|
|
140
|
+
|
|
137
141
|
const defaultedQueryOptions = queries.map((options) =>
|
|
138
142
|
this.#client.defaultQueryOptions(options),
|
|
139
143
|
)
|
|
140
144
|
|
|
141
145
|
const matchingObservers: QueryObserverMatch[] =
|
|
142
146
|
defaultedQueryOptions.flatMap((defaultedOptions) => {
|
|
143
|
-
const match =
|
|
144
|
-
(observer) =>
|
|
145
|
-
observer.options.queryHash === defaultedOptions.queryHash,
|
|
146
|
-
)
|
|
147
|
+
const match = prevObserversMap.get(defaultedOptions.queryHash)
|
|
147
148
|
if (match != null) {
|
|
148
149
|
return [{ defaultedQueryOptions: defaultedOptions, observer: match }]
|
|
149
150
|
}
|
|
150
151
|
return []
|
|
151
152
|
})
|
|
152
153
|
|
|
153
|
-
const matchedQueryHashes =
|
|
154
|
-
(match) => match.defaultedQueryOptions.queryHash,
|
|
154
|
+
const matchedQueryHashes = new Set(
|
|
155
|
+
matchingObservers.map((match) => match.defaultedQueryOptions.queryHash),
|
|
155
156
|
)
|
|
156
157
|
const unmatchedQueries = defaultedQueryOptions.filter(
|
|
157
|
-
(defaultedOptions) =>
|
|
158
|
-
!matchedQueryHashes.includes(defaultedOptions.queryHash),
|
|
158
|
+
(defaultedOptions) => !matchedQueryHashes.has(defaultedOptions.queryHash),
|
|
159
159
|
)
|
|
160
160
|
|
|
161
161
|
const getObserver = (options: QueryObserverOptions): QueryObserver => {
|
package/src/query.ts
CHANGED
|
@@ -74,8 +74,10 @@ export interface QueryBehavior<
|
|
|
74
74
|
) => void
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
export type FetchDirection = 'forward' | 'backward'
|
|
78
|
+
|
|
77
79
|
export interface FetchMeta {
|
|
78
|
-
fetchMore?: { direction:
|
|
80
|
+
fetchMore?: { direction: FetchDirection }
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
export interface FetchOptions {
|
|
@@ -120,7 +122,7 @@ interface ContinueAction {
|
|
|
120
122
|
|
|
121
123
|
interface SetStateAction<TData, TError> {
|
|
122
124
|
type: 'setState'
|
|
123
|
-
state: QueryState<TData, TError
|
|
125
|
+
state: Partial<QueryState<TData, TError>>
|
|
124
126
|
setStateOptions?: SetStateOptions
|
|
125
127
|
}
|
|
126
128
|
|
|
@@ -211,7 +213,7 @@ export class Query<
|
|
|
211
213
|
}
|
|
212
214
|
|
|
213
215
|
setState(
|
|
214
|
-
state: QueryState<TData, TError
|
|
216
|
+
state: Partial<QueryState<TData, TError>>,
|
|
215
217
|
setStateOptions?: SetStateOptions,
|
|
216
218
|
): void {
|
|
217
219
|
this.#dispatch({ type: 'setState', state, setStateOptions })
|
|
@@ -453,7 +455,7 @@ export class Query<
|
|
|
453
455
|
|
|
454
456
|
// Try to fetch the data
|
|
455
457
|
this.#retryer = createRetryer({
|
|
456
|
-
fn: context.fetchFn as () => TData
|
|
458
|
+
fn: context.fetchFn as () => Promise<TData>,
|
|
457
459
|
abort: abortController.abort.bind(abortController),
|
|
458
460
|
onSuccess: (data) => {
|
|
459
461
|
if (typeof data === 'undefined') {
|
|
@@ -462,7 +464,7 @@ export class Query<
|
|
|
462
464
|
`Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`,
|
|
463
465
|
)
|
|
464
466
|
}
|
|
465
|
-
onError(new Error(
|
|
467
|
+
onError(new Error(`${this.queryHash} data is undefined`) as any)
|
|
466
468
|
return
|
|
467
469
|
}
|
|
468
470
|
|
|
@@ -586,7 +588,7 @@ export class Query<
|
|
|
586
588
|
|
|
587
589
|
notifyManager.batch(() => {
|
|
588
590
|
this.#observers.forEach((observer) => {
|
|
589
|
-
observer.onQueryUpdate(
|
|
591
|
+
observer.onQueryUpdate()
|
|
590
592
|
})
|
|
591
593
|
|
|
592
594
|
this.#cache.notify({ query: this, type: 'updated', action })
|
package/src/queryCache.ts
CHANGED
|
@@ -27,7 +27,6 @@ interface QueryCacheConfig {
|
|
|
27
27
|
error: DefaultError | null,
|
|
28
28
|
query: Query<unknown, unknown, unknown>,
|
|
29
29
|
) => void
|
|
30
|
-
createStore?: () => QueryStore
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
interface NotifyEventQueryAdded extends NotifyEvent {
|
|
@@ -95,7 +94,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
95
94
|
|
|
96
95
|
constructor(public config: QueryCacheConfig = {}) {
|
|
97
96
|
super()
|
|
98
|
-
this.#queries =
|
|
97
|
+
this.#queries = new Map<string, Query>()
|
|
99
98
|
}
|
|
100
99
|
|
|
101
100
|
build<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(
|
|
@@ -176,13 +175,11 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
176
175
|
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
|
|
177
176
|
filters: WithRequired<QueryFilters, 'queryKey'>,
|
|
178
177
|
): Query<TQueryFnData, TError, TData> | undefined {
|
|
179
|
-
|
|
180
|
-
filters.exact = true
|
|
181
|
-
}
|
|
178
|
+
const defaultedFilters = { exact: true, ...filters }
|
|
182
179
|
|
|
183
|
-
return this.getAll().find((query) =>
|
|
184
|
-
|
|
185
|
-
|
|
180
|
+
return this.getAll().find((query) =>
|
|
181
|
+
matchQuery(defaultedFilters, query),
|
|
182
|
+
) as Query<TQueryFnData, TError, TData> | undefined
|
|
186
183
|
}
|
|
187
184
|
|
|
188
185
|
findAll(filters: QueryFilters = {}): Query[] {
|
package/src/queryClient.ts
CHANGED
|
@@ -212,14 +212,12 @@ export class QueryClient {
|
|
|
212
212
|
filters: QueryFilters = {},
|
|
213
213
|
cancelOptions: CancelOptions = {},
|
|
214
214
|
): Promise<void> {
|
|
215
|
-
|
|
216
|
-
cancelOptions.revert = true
|
|
217
|
-
}
|
|
215
|
+
const defaultedCancelOptions = { revert: true, ...cancelOptions }
|
|
218
216
|
|
|
219
217
|
const promises = notifyManager.batch(() =>
|
|
220
218
|
this.#queryCache
|
|
221
219
|
.findAll(filters)
|
|
222
|
-
.map((query) => query.cancel(
|
|
220
|
+
.map((query) => query.cancel(defaultedCancelOptions)),
|
|
223
221
|
)
|
|
224
222
|
|
|
225
223
|
return Promise.all(promises).then(noop).catch(noop)
|
|
@@ -249,25 +247,26 @@ export class QueryClient {
|
|
|
249
247
|
filters: RefetchQueryFilters = {},
|
|
250
248
|
options?: RefetchOptions,
|
|
251
249
|
): Promise<void> {
|
|
250
|
+
const fetchOptions = {
|
|
251
|
+
...options,
|
|
252
|
+
cancelRefetch: options?.cancelRefetch ?? true,
|
|
253
|
+
}
|
|
252
254
|
const promises = notifyManager.batch(() =>
|
|
253
255
|
this.#queryCache
|
|
254
256
|
.findAll(filters)
|
|
255
257
|
.filter((query) => !query.isDisabled())
|
|
256
|
-
.map((query) =>
|
|
257
|
-
query.fetch(undefined,
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
258
|
+
.map((query) => {
|
|
259
|
+
let promise = query.fetch(undefined, fetchOptions)
|
|
260
|
+
if (!fetchOptions.throwOnError) {
|
|
261
|
+
promise = promise.catch(noop)
|
|
262
|
+
}
|
|
263
|
+
return query.state.fetchStatus === 'paused'
|
|
264
|
+
? Promise.resolve()
|
|
265
|
+
: promise
|
|
266
|
+
}),
|
|
262
267
|
)
|
|
263
268
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (!options?.throwOnError) {
|
|
267
|
-
promise = promise.catch(noop)
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return promise
|
|
269
|
+
return Promise.all(promises).then(noop)
|
|
271
270
|
}
|
|
272
271
|
|
|
273
272
|
fetchQuery<
|
|
@@ -480,8 +479,8 @@ export class QueryClient {
|
|
|
480
479
|
defaultedOptions.refetchOnReconnect =
|
|
481
480
|
defaultedOptions.networkMode !== 'always'
|
|
482
481
|
}
|
|
483
|
-
if (typeof defaultedOptions.
|
|
484
|
-
defaultedOptions.
|
|
482
|
+
if (typeof defaultedOptions.throwOnError === 'undefined') {
|
|
483
|
+
defaultedOptions.throwOnError = !!defaultedOptions.suspense
|
|
485
484
|
}
|
|
486
485
|
|
|
487
486
|
return defaultedOptions as DefaultedQueryObserverOptions<
|