Skip to content

Commit a840e7c

Browse files
committed
fix(useQuery): cleanup queries even if they have been fetching
do not re-schedule garbage collection if a query is fetching and we never had any observers subscribed; this is necessary to make suspense work, because with suspense, we always throw before we subscribe, so the garbage collection would prematurely remove the query, resulting in an infinite loop
1 parent aea7fd2 commit a840e7c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/core/query.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ export class Query<
163163
private observers: QueryObserver<any, any, any, any, any>[]
164164
private defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
165165
private abortSignalConsumed: boolean
166+
private hadObservers: boolean
166167

167168
constructor(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>) {
168169
this.abortSignalConsumed = false
170+
this.hadObservers = false
169171
this.defaultOptions = config.defaultOptions
170172
this.setOptions(config.options)
171173
this.observers = []
@@ -216,7 +218,9 @@ export class Query<
216218
private optionalRemove() {
217219
if (!this.observers.length) {
218220
if (this.state.isFetching) {
219-
this.scheduleGc()
221+
if (this.hadObservers) {
222+
this.scheduleGc()
223+
}
220224
} else {
221225
this.cache.remove(this)
222226
}
@@ -321,6 +325,7 @@ export class Query<
321325

322326
addObserver(observer: QueryObserver<any, any, any, any, any>): void {
323327
if (this.observers.indexOf(observer) === -1) {
328+
this.hadObservers = true
324329
this.observers.push(observer)
325330

326331
// Stop the query from being garbage collected

0 commit comments

Comments
 (0)