Skip to content

Commit c9d4786

Browse files
committed
refactor: cacheTime-zero
since every fetch triggers a new gc cycle, we don't need to do this in a loop anymore also, reset isFetchingOptimistic after every fetch
1 parent 307bc32 commit c9d4786

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/core/query.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,8 @@ export class Query<
195195
}
196196

197197
protected optionalRemove() {
198-
if (!this.observers.length) {
199-
if (this.state.fetchStatus === 'idle') {
200-
this.cache.remove(this)
201-
} else {
202-
this.scheduleGc()
203-
}
198+
if (!this.observers.length && this.state.fetchStatus === 'idle') {
199+
this.cache.remove(this)
204200
}
205201
}
206202

@@ -449,9 +445,10 @@ export class Query<
449445
this.cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
450446

451447
if (!this.isFetchingOptimistic) {
452-
// Remove query after fetching
448+
// Schedule query gc after fetching
453449
this.scheduleGc()
454450
}
451+
this.isFetchingOptimistic = false
455452
},
456453
onError: (error: TError | { silent?: boolean }) => {
457454
// Optimistically update state if needed
@@ -471,9 +468,10 @@ export class Query<
471468
}
472469

473470
if (!this.isFetchingOptimistic) {
474-
// Remove query after fetching
471+
// Schedule query gc after fetching
475472
this.scheduleGc()
476473
}
474+
this.isFetchingOptimistic = false
477475
},
478476
onFail: () => {
479477
this.dispatch({ type: 'failed' })

src/core/queryObserver.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,7 @@ export class QueryObserver<
312312
.build(this.client, defaultedOptions)
313313
query.isFetchingOptimistic = true
314314

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

321318
protected fetch(

0 commit comments

Comments
 (0)