6
6
noop ,
7
7
} from './utils'
8
8
import { notifyManager } from './notifyManager'
9
- import type { QueryResult , ResolvedQueryConfig } from './types'
9
+ import type { QueryConfig , QueryResult , ResolvedQueryConfig } from './types'
10
10
import type { Query , Action , FetchMoreOptions , RefetchOptions } from './query'
11
+ import { DEFAULT_CONFIG , isResolvedQueryConfig } from './config'
11
12
12
13
export type UpdateListener < TResult , TError > = (
13
14
result : QueryResult < TResult , TError >
@@ -41,13 +42,14 @@ export class QueryObserver<TResult, TError> {
41
42
this . remove = this . remove . bind ( this )
42
43
this . refetch = this . refetch . bind ( this )
43
44
this . fetchMore = this . fetchMore . bind ( this )
45
+ this . unsubscribe = this . unsubscribe . bind ( this )
44
46
45
47
// Subscribe to the query
46
48
this . updateQuery ( )
47
49
}
48
50
49
51
subscribe ( listener ?: UpdateListener < TResult , TError > ) : ( ) => void {
50
- this . listener = listener
52
+ this . listener = listener || noop
51
53
this . currentQuery . subscribeObserver ( this )
52
54
53
55
if (
@@ -60,7 +62,8 @@ export class QueryObserver<TResult, TError> {
60
62
}
61
63
62
64
this . updateTimers ( )
63
- return this . unsubscribe . bind ( this )
65
+
66
+ return this . unsubscribe
64
67
}
65
68
66
69
unsubscribe ( ) : void {
@@ -69,11 +72,19 @@ export class QueryObserver<TResult, TError> {
69
72
this . currentQuery . unsubscribeObserver ( this )
70
73
}
71
74
72
- updateConfig ( config : ResolvedQueryConfig < TResult , TError > ) : void {
75
+ updateConfig (
76
+ config : QueryConfig < TResult , TError > | ResolvedQueryConfig < TResult , TError >
77
+ ) : void {
73
78
const prevConfig = this . config
74
79
const prevQuery = this . currentQuery
75
80
76
- this . config = config
81
+ this . config = isResolvedQueryConfig ( config )
82
+ ? config
83
+ : this . config . queryCache . getResolvedQueryConfig (
84
+ this . config . queryKey ,
85
+ config
86
+ )
87
+
77
88
this . updateQuery ( )
78
89
79
90
// Take no further actions if there is no subscriber
@@ -143,6 +154,11 @@ export class QueryObserver<TResult, TError> {
143
154
}
144
155
145
156
fetch ( ) : Promise < TResult | undefined > {
157
+ // Never try to fetch if no query function has been set
158
+ if ( this . config . queryFn === DEFAULT_CONFIG . queries ?. queryFn ) {
159
+ return Promise . resolve ( this . currentResult . data )
160
+ }
161
+
146
162
return this . currentQuery . fetch ( undefined , this . config ) . catch ( noop )
147
163
}
148
164
@@ -157,50 +173,6 @@ export class QueryObserver<TResult, TError> {
157
173
}
158
174
}
159
175
160
- private notify ( options : NotifyOptions ) : void {
161
- const { config, currentResult, currentQuery, listener } = this
162
- const { onSuccess, onSettled, onError } = config
163
-
164
- notifyManager . batch ( ( ) => {
165
- // First trigger the configuration callbacks
166
- if ( options . onSuccess ) {
167
- if ( onSuccess ) {
168
- notifyManager . schedule ( ( ) => {
169
- onSuccess ( currentResult . data ! )
170
- } )
171
- }
172
- if ( onSettled ) {
173
- notifyManager . schedule ( ( ) => {
174
- onSettled ( currentResult . data ! , null )
175
- } )
176
- }
177
- } else if ( options . onError ) {
178
- if ( onError ) {
179
- notifyManager . schedule ( ( ) => {
180
- onError ( currentResult . error ! )
181
- } )
182
- }
183
- if ( onSettled ) {
184
- notifyManager . schedule ( ( ) => {
185
- onSettled ( undefined , currentResult . error ! )
186
- } )
187
- }
188
- }
189
-
190
- // Then trigger the listener
191
- if ( options . listener && listener ) {
192
- notifyManager . schedule ( ( ) => {
193
- listener ( currentResult )
194
- } )
195
- }
196
-
197
- // Then the global listeners
198
- if ( options . globalListeners ) {
199
- config . queryCache . notifyGlobalListeners ( currentQuery )
200
- }
201
- } )
202
- }
203
-
204
176
private updateStaleTimeout ( ) : void {
205
177
if ( isServer ) {
206
178
return
@@ -393,4 +365,48 @@ export class QueryObserver<TResult, TError> {
393
365
394
366
this . notify ( notifyOptions )
395
367
}
368
+
369
+ private notify ( options : NotifyOptions ) : void {
370
+ const { config, currentResult, currentQuery, listener } = this
371
+ const { onSuccess, onSettled, onError } = config
372
+
373
+ notifyManager . batch ( ( ) => {
374
+ // First trigger the configuration callbacks
375
+ if ( options . onSuccess ) {
376
+ if ( onSuccess ) {
377
+ notifyManager . schedule ( ( ) => {
378
+ onSuccess ( currentResult . data ! )
379
+ } )
380
+ }
381
+ if ( onSettled ) {
382
+ notifyManager . schedule ( ( ) => {
383
+ onSettled ( currentResult . data ! , null )
384
+ } )
385
+ }
386
+ } else if ( options . onError ) {
387
+ if ( onError ) {
388
+ notifyManager . schedule ( ( ) => {
389
+ onError ( currentResult . error ! )
390
+ } )
391
+ }
392
+ if ( onSettled ) {
393
+ notifyManager . schedule ( ( ) => {
394
+ onSettled ( undefined , currentResult . error ! )
395
+ } )
396
+ }
397
+ }
398
+
399
+ // Then trigger the listener
400
+ if ( options . listener && listener ) {
401
+ notifyManager . schedule ( ( ) => {
402
+ listener ( currentResult )
403
+ } )
404
+ }
405
+
406
+ // Then the global listeners
407
+ if ( options . globalListeners ) {
408
+ config . queryCache . notifyGlobalListeners ( currentQuery )
409
+ }
410
+ } )
411
+ }
396
412
}
0 commit comments