Skip to content

Commit 52607f6

Browse files
authored
fix: make sure queries with only inactive observers are also invalidated (#949)
1 parent e12d002 commit 52607f6

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/core/query.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ export class Query<TResult, TError> {
133133

134134
private dispatch(action: Action<TResult, TError>): void {
135135
this.state = queryReducer(this.state, action)
136-
this.observers.forEach(d => d.onQueryUpdate(this.state, action))
136+
137+
this.observers.forEach(observer => {
138+
observer.onQueryUpdate(this.state, action)
139+
})
140+
137141
this.notifyGlobalListeners(this)
138142
}
139143

src/core/queryCache.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export class QueryCache {
9292
0
9393
)
9494

95-
this.globalListeners.forEach(d => d(this, query))
95+
this.globalListeners.forEach(listener => {
96+
listener(this, query)
97+
})
9698
}
9799

98100
getDefaultConfig() {
@@ -193,14 +195,10 @@ export class QueryCache {
193195
try {
194196
await Promise.all(
195197
this.getQueries(predicate, options).map(query => {
196-
if (query.observers.length) {
197-
if (refetchActive && query.isEnabled()) {
198-
return query.fetch()
199-
}
200-
} else {
201-
if (refetchInactive) {
202-
return query.fetch()
203-
}
198+
const enabled = query.isEnabled()
199+
200+
if ((enabled && refetchActive) || (!enabled && refetchInactive)) {
201+
return query.fetch()
204202
}
205203

206204
return undefined

src/react/tests/useQuery.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,56 @@ describe('useQuery', () => {
356356
return null
357357
})
358358

359+
it.only('should update disabled query when updated with invalidateQueries', async () => {
360+
const key = queryKey()
361+
const states: QueryResult<number>[] = []
362+
let count = 0
363+
364+
function Page() {
365+
const state = useQuery(
366+
key,
367+
async () => {
368+
await sleep(10)
369+
count++
370+
return count
371+
},
372+
{ enabled: false }
373+
)
374+
375+
states.push(state)
376+
377+
React.useEffect(() => {
378+
setTimeout(() => {
379+
queryCache.invalidateQueries(key, { refetchInactive: true })
380+
}, 20)
381+
}, [])
382+
383+
return null
384+
}
385+
386+
render(<Page />)
387+
388+
await waitFor(() => expect(states.length).toBe(3))
389+
390+
expect(states).toMatchObject([
391+
{
392+
data: undefined,
393+
isFetching: false,
394+
isSuccess: false,
395+
},
396+
{
397+
data: undefined,
398+
isFetching: true,
399+
isSuccess: false,
400+
},
401+
{
402+
data: 1,
403+
isFetching: false,
404+
isSuccess: true,
405+
},
406+
])
407+
})
408+
359409
it('should keep the previous data when keepPreviousData is set', async () => {
360410
const key = queryKey()
361411
const states: QueryResult<number>[] = []

0 commit comments

Comments
 (0)