Skip to content

Commit 2471a6f

Browse files
committed
refactor: cacheTime-zero
apply a different suspense "workaround": do not garbage collect when fetching optimistically (done only by suspense) - gc will kick in once an observer subscribes; this will make sure we can still gc other fetches that don't have an observer consistently, like prefetching when the fetch takes longer than the gc time (which was leaking with the old workaround)
1 parent 16b2af5 commit 2471a6f

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/core/query.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface QueryConfig<
3535
cache: QueryCache
3636
queryKey: TQueryKey
3737
queryHash: string
38+
optimistic?: boolean
3839
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
3940
defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
4041
state?: QueryState<TData, TError>
@@ -154,20 +155,19 @@ export class Query<
154155
revertState?: QueryState<TData, TError>
155156
state: QueryState<TData, TError>
156157
meta: QueryMeta | undefined
158+
isFetchingOptimistic?: boolean
157159

158160
private cache: QueryCache
159161
private promise?: Promise<TData>
160162
private retryer?: Retryer<TData, TError>
161163
private observers: QueryObserver<any, any, any, any, any>[]
162164
private defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>
163165
private abortSignalConsumed: boolean
164-
private hadObservers: boolean
165166

166167
constructor(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>) {
167168
super()
168169

169170
this.abortSignalConsumed = false
170-
this.hadObservers = false
171171
this.defaultOptions = config.defaultOptions
172172
this.setOptions(config.options)
173173
this.observers = []
@@ -177,7 +177,6 @@ export class Query<
177177
this.initialState = config.state || this.getDefaultState(this.options)
178178
this.state = this.initialState
179179
this.meta = config.meta
180-
this.scheduleGc()
181180
}
182181

183182
private setOptions(
@@ -200,7 +199,7 @@ export class Query<
200199
if (!this.observers.length) {
201200
if (this.state.fetchStatus === 'idle') {
202201
this.cache.remove(this)
203-
} else if (this.hadObservers) {
202+
} else {
204203
this.scheduleGc()
205204
}
206205
}
@@ -307,7 +306,6 @@ export class Query<
307306
addObserver(observer: QueryObserver<any, any, any, any, any>): void {
308307
if (this.observers.indexOf(observer) === -1) {
309308
this.observers.push(observer)
310-
this.hadObservers = true
311309

312310
// Stop the query from being garbage collected
313311
this.clearGcTimeout()
@@ -451,8 +449,10 @@ export class Query<
451449
// Notify cache callback
452450
this.cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
453451

454-
// Remove query after fetching
455-
this.scheduleGc()
452+
if (!this.isFetchingOptimistic) {
453+
// Remove query after fetching
454+
this.scheduleGc()
455+
}
456456
},
457457
onError: (error: TError | { silent?: boolean }) => {
458458
// Optimistically update state if needed
@@ -471,8 +471,10 @@ export class Query<
471471
getLogger().error(error)
472472
}
473473

474-
// Remove query after fetching
475-
this.scheduleGc()
474+
if (!this.isFetchingOptimistic) {
475+
// Remove query after fetching
476+
this.scheduleGc()
477+
}
476478
},
477479
onFail: () => {
478480
this.dispatch({ type: 'failed' })

src/core/queryObserver.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,13 @@ export class QueryObserver<
309309

310310
const query = this.client
311311
.getQueryCache()
312-
.build(
313-
this.client,
314-
defaultedOptions as QueryOptions<
315-
TQueryFnData,
316-
TError,
317-
TQueryData,
318-
TQueryKey
319-
>
320-
)
312+
.build(this.client, defaultedOptions)
313+
query.isFetchingOptimistic = true
321314

322-
return query.fetch().then(() => this.createResult(query, defaultedOptions))
315+
return query.fetch().then(() => {
316+
query.isFetchingOptimistic = false
317+
return this.createResult(query, defaultedOptions)
318+
})
323319
}
324320

325321
protected fetch(
@@ -655,17 +651,7 @@ export class QueryObserver<
655651
}
656652

657653
private updateQuery(): void {
658-
const query = this.client
659-
.getQueryCache()
660-
.build(
661-
this.client,
662-
this.options as QueryOptions<
663-
TQueryFnData,
664-
TError,
665-
TQueryData,
666-
TQueryKey
667-
>
668-
)
654+
const query = this.client.getQueryCache().build(this.client, this.options)
669655

670656
if (query === this.currentQuery) {
671657
return

0 commit comments

Comments
 (0)