Skip to content

Commit f0ac65c

Browse files
committed
fix: cleanup
1 parent 75199ba commit f0ac65c

File tree

8 files changed

+79
-78
lines changed

8 files changed

+79
-78
lines changed

docs/src/pages/guides/migrating-to-react-query-3.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function Overview() {
356356
}
357357
```
358358
359-
## Retry/offline mutations
359+
#### Retry/offline mutations
360360
361361
By default React Query will not retry a mutation on error, but it is possible with the `retry` option:
362362
@@ -381,6 +381,24 @@ const unsubscribe = observer.subscribe(result => {
381381
})
382382
```
383383
384+
#### InfiniteQueryObserver
385+
386+
A `InfiniteQueryObserver` can be used to create and/or watch an infinite query:
387+
388+
```js
389+
const observer = new InfiniteQueryObserver(queryClient, {
390+
queryKey: 'posts',
391+
queryFn: fetchPosts,
392+
getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
393+
getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
394+
})
395+
396+
const unsubscribe = observer.subscribe(result => {
397+
console.log(result)
398+
unsubscribe()
399+
})
400+
```
401+
384402
#### QueriesObserver
385403
386404
A `QueriesObserver` can be used to create and/or watch multiple queries:
@@ -397,7 +415,7 @@ const unsubscribe = observer.subscribe(result => {
397415
})
398416
```
399417
400-
## `queryClient.setQueryDefaults`
418+
#### Set default options for specific queries
401419
402420
The `queryClient.setQueryDefaults()` method can be used to set default options for specific queries:
403421
@@ -409,7 +427,7 @@ function Component() {
409427
}
410428
```
411429
412-
## `queryClient.setMutationDefaults`
430+
#### Set default options for specific mutations
413431
414432
The `queryClient.setMutationDefaults()` method can be used to set default options for specific mutations:
415433

src/core/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ export { setLogger } from './logger'
99
export { notifyManager } from './notifyManager'
1010
export { focusManager } from './focusManager'
1111
export { onlineManager } from './onlineManager'
12-
export { hashQueryKey, isCancelledError, isError } from './utils'
12+
export { hashQueryKey, isError } from './utils'
13+
export { isCancelledError } from './retryer'
1314

1415
// Types
1516
export * from './types'
16-
export type { CancelledError } from './utils'
17+
export type { CancelledError } from './retryer'
1718
export type { Query } from './query'
1819
export type { Logger } from './logger'

src/core/query.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import {
2-
CancelOptions,
32
Updater,
43
ensureArray,
54
functionalUpdate,
6-
isCancelledError,
75
isValidTimeout,
86
noop,
97
replaceEqualDeep,
@@ -20,7 +18,7 @@ import type { QueryCache } from './queryCache'
2018
import type { QueryObserver } from './queryObserver'
2119
import { notifyManager } from './notifyManager'
2220
import { getLogger } from './logger'
23-
import { Retryer } from './retryer'
21+
import { Retryer, CancelOptions, isCancelledError } from './retryer'
2422

2523
// TYPES
2624

@@ -162,34 +160,16 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
162160
this.defaultOptions = options
163161
}
164162

165-
private dispatch(action: Action<TData, TError>): void {
166-
this.state = this.reducer(this.state, action)
167-
168-
notifyManager.batch(() => {
169-
this.observers.forEach(observer => {
170-
observer.onQueryUpdate(action)
171-
})
172-
173-
this.cache.notify(this)
174-
})
175-
}
176-
177163
private scheduleGc(): void {
178164
this.clearGcTimeout()
179165

180166
if (!this.observers.length && isValidTimeout(this.cacheTime)) {
181167
this.gcTimeout = setTimeout(() => {
182-
this.remove()
168+
this.cache.remove(this)
183169
}, this.cacheTime)
184170
}
185171
}
186172

187-
cancel(options?: CancelOptions): Promise<void> {
188-
const promise = this.promise
189-
this.retryer?.cancel(options)
190-
return promise ? promise.then(noop).catch(noop) : Promise.resolve()
191-
}
192-
193173
private clearGcTimeout() {
194174
clearTimeout(this.gcTimeout)
195175
this.gcTimeout = undefined
@@ -228,8 +208,10 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
228208
this.dispatch({ type: 'setState', state })
229209
}
230210

231-
remove(): void {
232-
this.cache.remove(this)
211+
cancel(options?: CancelOptions): Promise<void> {
212+
const promise = this.promise
213+
this.retryer?.cancel(options)
214+
return promise ? promise.then(noop).catch(noop) : Promise.resolve()
233215
}
234216

235217
destroy(): void {
@@ -433,6 +415,18 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
433415
return this.promise
434416
}
435417

418+
private dispatch(action: Action<TData, TError>): void {
419+
this.state = this.reducer(this.state, action)
420+
421+
notifyManager.batch(() => {
422+
this.observers.forEach(observer => {
423+
observer.onQueryUpdate(action)
424+
})
425+
426+
this.cache.notify(this)
427+
})
428+
}
429+
436430
protected getDefaultState(
437431
options: QueryOptions<TData, TError, TQueryFnData>
438432
): QueryState<TData, TError> {

src/core/queryClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
CancelOptions,
32
QueryFilters,
43
Updater,
54
noop,
@@ -26,6 +25,7 @@ import { MutationCache } from './mutationCache'
2625
import { focusManager } from './focusManager'
2726
import { onlineManager } from './onlineManager'
2827
import { notifyManager } from './notifyManager'
28+
import { CancelOptions } from './retryer'
2929

3030
// TYPES
3131

src/core/queryObserver.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,6 @@ export class QueryObserver<
165165
return this.currentResult
166166
}
167167

168-
getCurrentOrNextResult(
169-
options?: ResultOptions
170-
): Promise<QueryObserverResult<TData, TError>> {
171-
if (this.currentQuery.isFetching()) {
172-
return this.getNextResult(options)
173-
} else if (this.currentResult.isError && options?.throwOnError) {
174-
return Promise.reject(this.currentResult.error)
175-
}
176-
return Promise.resolve(this.currentResult)
177-
}
178-
179168
getNextResult(
180169
options?: ResultOptions
181170
): Promise<QueryObserverResult<TData, TError>> {
@@ -198,7 +187,7 @@ export class QueryObserver<
198187
}
199188

200189
remove(): void {
201-
this.currentQuery.remove()
190+
this.client.getQueryCache().remove(this.currentQuery)
202191
}
203192

204193
refetch(
@@ -418,7 +407,7 @@ export class QueryObserver<
418407

419408
this.updateResult(willFetch)
420409

421-
if (!this.listeners.length) {
410+
if (!this.hasListeners()) {
422411
return
423412
}
424413

src/core/retryer.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { focusManager } from './focusManager'
22
import { onlineManager } from './onlineManager'
3-
import {
4-
CancelOptions,
5-
CancelledError,
6-
functionalUpdate,
7-
isCancelable,
8-
sleep,
9-
} from './utils'
3+
import { functionalUpdate, sleep } from './utils'
104

115
// TYPES
126

@@ -34,6 +28,32 @@ function defaultRetryDelay(failureCount: number) {
3428
return Math.min(1000 * 2 ** failureCount, 30000)
3529
}
3630

31+
interface Cancelable {
32+
cancel(): void
33+
}
34+
35+
function isCancelable(value: any): value is Cancelable {
36+
return typeof value?.cancel === 'function'
37+
}
38+
39+
export interface CancelOptions {
40+
revert?: boolean
41+
silent?: boolean
42+
}
43+
44+
export class CancelledError {
45+
revert?: boolean
46+
silent?: boolean
47+
constructor(options?: CancelOptions) {
48+
this.revert = options?.revert
49+
this.silent = options?.silent
50+
}
51+
}
52+
53+
export function isCancelledError(value: any): value is CancelledError {
54+
return value instanceof CancelledError
55+
}
56+
3757
// CLASS
3858

3959
export class Retryer<TData = unknown, TError = unknown> {

src/core/tests/queryCache.test.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import {
66
mockNavigatorOnLine,
77
expectType,
88
} from '../../react/tests/utils'
9-
import { QueryCache, QueryClient, QueryObserver } from '../..'
10-
import { isCancelledError, isError } from '../utils'
9+
import {
10+
QueryCache,
11+
QueryClient,
12+
QueryObserver,
13+
isCancelledError,
14+
isError,
15+
} from '../..'
1116
import { QueryObserverResult } from '../types'
1217
import { QueriesObserver } from '../queriesObserver'
1318

src/core/utils.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ export type Updater<TInput, TOutput> =
4949
| TOutput
5050
| DataUpdateFunction<TInput, TOutput>
5151

52-
interface Cancelable {
53-
cancel(): void
54-
}
55-
56-
export interface CancelOptions {
57-
revert?: boolean
58-
silent?: boolean
59-
}
60-
61-
export class CancelledError {
62-
revert?: boolean
63-
silent?: boolean
64-
constructor(options?: CancelOptions) {
65-
this.revert = options?.revert
66-
this.silent = options?.silent
67-
}
68-
}
69-
7052
// UTILS
7153

7254
export const isServer = typeof window === 'undefined'
@@ -353,18 +335,10 @@ export function isQueryKey(value: any): value is QueryKey {
353335
return typeof value === 'string' || Array.isArray(value)
354336
}
355337

356-
export function isCancelable(value: any): value is Cancelable {
357-
return typeof value?.cancel === 'function'
358-
}
359-
360338
export function isError(value: any): value is Error {
361339
return value instanceof Error
362340
}
363341

364-
export function isCancelledError(value: any): value is CancelledError {
365-
return value instanceof CancelledError
366-
}
367-
368342
export function sleep(timeout: number): Promise<void> {
369343
return new Promise(resolve => {
370344
setTimeout(resolve, timeout)

0 commit comments

Comments
 (0)