@tanstack/query-core 4.14.3 → 4.14.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/lib/infiniteQueryBehavior.esm.js +1 -1
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +1 -1
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +1 -1
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.esm.js +10 -3
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +10 -3
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +10 -3
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +1 -1
- package/build/lib/mutation.esm.js +4 -1
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +4 -1
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +4 -1
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.esm.js +1 -2
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +1 -2
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +1 -2
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/query.d.ts +1 -3
- package/build/lib/query.esm.js +5 -4
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +5 -4
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +5 -4
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.esm.js +1 -2
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +1 -2
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +1 -2
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/umd/index.development.js +22 -13
- 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 +1 -1
- package/src/infiniteQueryBehavior.ts +1 -1
- package/src/infiniteQueryObserver.ts +13 -6
- package/src/mutation.ts +4 -2
- package/src/mutationCache.ts +0 -1
- package/src/query.ts +4 -7
- package/src/queryCache.ts +0 -1
- package/src/tests/query.test.tsx +17 -0
- package/src/tests/queryCache.test.tsx +36 -1
package/package.json
CHANGED
|
@@ -139,18 +139,25 @@ export class InfiniteQueryObserver<
|
|
|
139
139
|
): InfiniteQueryObserverResult<TData, TError> {
|
|
140
140
|
const { state } = query
|
|
141
141
|
const result = super.createResult(query, options)
|
|
142
|
+
|
|
143
|
+
const { isFetching, isRefetching } = result
|
|
144
|
+
|
|
145
|
+
const isFetchingNextPage =
|
|
146
|
+
isFetching && state.fetchMeta?.fetchMore?.direction === 'forward'
|
|
147
|
+
|
|
148
|
+
const isFetchingPreviousPage =
|
|
149
|
+
isFetching && state.fetchMeta?.fetchMore?.direction === 'backward'
|
|
150
|
+
|
|
142
151
|
return {
|
|
143
152
|
...result,
|
|
144
153
|
fetchNextPage: this.fetchNextPage,
|
|
145
154
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
146
155
|
hasNextPage: hasNextPage(options, state.data?.pages),
|
|
147
156
|
hasPreviousPage: hasPreviousPage(options, state.data?.pages),
|
|
148
|
-
isFetchingNextPage
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
state.fetchStatus === 'fetching' &&
|
|
153
|
-
state.fetchMeta?.fetchMore?.direction === 'backward',
|
|
157
|
+
isFetchingNextPage,
|
|
158
|
+
isFetchingPreviousPage,
|
|
159
|
+
isRefetching:
|
|
160
|
+
isRefetching && !isFetchingNextPage && !isFetchingPreviousPage,
|
|
154
161
|
}
|
|
155
162
|
}
|
|
156
163
|
}
|
package/src/mutation.ts
CHANGED
|
@@ -91,7 +91,6 @@ export class Mutation<
|
|
|
91
91
|
state: MutationState<TData, TError, TVariables, TContext>
|
|
92
92
|
options: MutationOptions<TData, TError, TVariables, TContext>
|
|
93
93
|
mutationId: number
|
|
94
|
-
meta: MutationMeta | undefined
|
|
95
94
|
|
|
96
95
|
private observers: MutationObserver<TData, TError, TVariables, TContext>[]
|
|
97
96
|
private mutationCache: MutationCache
|
|
@@ -110,12 +109,15 @@ export class Mutation<
|
|
|
110
109
|
this.logger = config.logger || defaultLogger
|
|
111
110
|
this.observers = []
|
|
112
111
|
this.state = config.state || getDefaultState()
|
|
113
|
-
this.meta = config.meta
|
|
114
112
|
|
|
115
113
|
this.updateCacheTime(this.options.cacheTime)
|
|
116
114
|
this.scheduleGc()
|
|
117
115
|
}
|
|
118
116
|
|
|
117
|
+
get meta(): MutationMeta | undefined {
|
|
118
|
+
return this.options.meta
|
|
119
|
+
}
|
|
120
|
+
|
|
119
121
|
setState(state: MutationState<TData, TError, TVariables, TContext>): void {
|
|
120
122
|
this.dispatch({ type: 'setState', state })
|
|
121
123
|
}
|
package/src/mutationCache.ts
CHANGED
package/src/query.ts
CHANGED
|
@@ -34,7 +34,6 @@ interface QueryConfig<
|
|
|
34
34
|
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
35
35
|
defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
36
36
|
state?: QueryState<TData, TError>
|
|
37
|
-
meta: QueryMeta | undefined
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
export interface QueryState<TData = unknown, TError = unknown> {
|
|
@@ -64,7 +63,6 @@ export interface FetchContext<
|
|
|
64
63
|
options: QueryOptions<TQueryFnData, TError, TData, any>
|
|
65
64
|
queryKey: TQueryKey
|
|
66
65
|
state: QueryState<TData, TError>
|
|
67
|
-
meta: QueryMeta | undefined
|
|
68
66
|
}
|
|
69
67
|
|
|
70
68
|
export interface QueryBehavior<
|
|
@@ -152,7 +150,6 @@ export class Query<
|
|
|
152
150
|
initialState: QueryState<TData, TError>
|
|
153
151
|
revertState?: QueryState<TData, TError>
|
|
154
152
|
state: QueryState<TData, TError>
|
|
155
|
-
meta: QueryMeta | undefined
|
|
156
153
|
isFetchingOptimistic?: boolean
|
|
157
154
|
|
|
158
155
|
private cache: QueryCache
|
|
@@ -176,7 +173,10 @@ export class Query<
|
|
|
176
173
|
this.queryHash = config.queryHash
|
|
177
174
|
this.initialState = config.state || getDefaultState(this.options)
|
|
178
175
|
this.state = this.initialState
|
|
179
|
-
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
get meta(): QueryMeta | undefined {
|
|
179
|
+
return this.options.meta
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
private setOptions(
|
|
@@ -184,8 +184,6 @@ export class Query<
|
|
|
184
184
|
): void {
|
|
185
185
|
this.options = { ...this.defaultOptions, ...options }
|
|
186
186
|
|
|
187
|
-
this.meta = options?.meta
|
|
188
|
-
|
|
189
187
|
this.updateCacheTime(this.options.cacheTime)
|
|
190
188
|
}
|
|
191
189
|
|
|
@@ -406,7 +404,6 @@ export class Query<
|
|
|
406
404
|
queryKey: this.queryKey,
|
|
407
405
|
state: this.state,
|
|
408
406
|
fetchFn,
|
|
409
|
-
meta: this.meta,
|
|
410
407
|
}
|
|
411
408
|
|
|
412
409
|
addSignalProperty(context)
|
package/src/queryCache.ts
CHANGED
package/src/tests/query.test.tsx
CHANGED
|
@@ -596,6 +596,23 @@ describe('query', () => {
|
|
|
596
596
|
expect(query.options.meta).toBeUndefined()
|
|
597
597
|
})
|
|
598
598
|
|
|
599
|
+
test('can use default meta', async () => {
|
|
600
|
+
const meta = {
|
|
601
|
+
it: 'works',
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const key = queryKey()
|
|
605
|
+
const queryFn = () => 'data'
|
|
606
|
+
|
|
607
|
+
queryClient.setQueryDefaults(key, { meta })
|
|
608
|
+
|
|
609
|
+
await queryClient.prefetchQuery(key, queryFn)
|
|
610
|
+
|
|
611
|
+
const query = queryCache.find(key)!
|
|
612
|
+
|
|
613
|
+
expect(query.meta).toBe(meta)
|
|
614
|
+
})
|
|
615
|
+
|
|
599
616
|
test('provides meta object inside query function', async () => {
|
|
600
617
|
const meta = {
|
|
601
618
|
it: 'works',
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { sleep, queryKey, createQueryClient } from './utils'
|
|
2
2
|
import type { QueryClient } from '..'
|
|
3
|
-
import { QueryCache } from '..'
|
|
3
|
+
import { QueryCache, QueryObserver } from '..'
|
|
4
4
|
import type { Query } from '.././query'
|
|
5
|
+
import { waitFor } from '@testing-library/react'
|
|
5
6
|
|
|
6
7
|
describe('queryCache', () => {
|
|
7
8
|
let queryClient: QueryClient
|
|
@@ -37,6 +38,40 @@ describe('queryCache', () => {
|
|
|
37
38
|
expect(callback).toHaveBeenCalled()
|
|
38
39
|
})
|
|
39
40
|
|
|
41
|
+
test('should notify query cache when a query becomes stale', async () => {
|
|
42
|
+
const key = queryKey()
|
|
43
|
+
const events: Array<string> = []
|
|
44
|
+
const unsubscribe = queryCache.subscribe((event) => {
|
|
45
|
+
events.push(event.type)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const observer = new QueryObserver(queryClient, {
|
|
49
|
+
queryKey: key,
|
|
50
|
+
queryFn: () => 'data',
|
|
51
|
+
staleTime: 10,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const unsubScribeObserver = observer.subscribe(jest.fn)
|
|
55
|
+
|
|
56
|
+
await waitFor(() => {
|
|
57
|
+
expect(events.length).toBe(8)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
expect(events).toEqual([
|
|
61
|
+
'added', // 1. Query added -> loading
|
|
62
|
+
'observerResultsUpdated', // 2. Observer result updated -> loading
|
|
63
|
+
'observerAdded', // 3. Observer added
|
|
64
|
+
'observerResultsUpdated', // 4. Observer result updated -> fetching
|
|
65
|
+
'updated', // 5. Query updated -> fetching
|
|
66
|
+
'observerResultsUpdated', // 6. Observer result updated -> success
|
|
67
|
+
'updated', // 7. Query updated -> success
|
|
68
|
+
'observerResultsUpdated', // 8. Observer result updated -> stale
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
unsubscribe()
|
|
72
|
+
unsubScribeObserver()
|
|
73
|
+
})
|
|
74
|
+
|
|
40
75
|
test('should include the queryCache and query when notifying listeners', async () => {
|
|
41
76
|
const key = queryKey()
|
|
42
77
|
const callback = jest.fn()
|