Skip to content

Commit

Permalink
fix: (queryInstance.clearInterval) is not called on unmount (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
otaciliolacerda authored Jul 8, 2020
1 parent ce92bc2 commit 2cfd164
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/queryInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function makeQueryInstance(query, onStateUpdate) {
query.instances = query.instances.filter(d => d.id !== instance.id)

if (!query.instances.length) {
query.clearIntervals()
instance.clearInterval()
query.cancel()

if (!isServer) {
Expand Down
16 changes: 15 additions & 1 deletion src/core/tests/queryCache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('queryCache', () => {
expect(callback).toHaveBeenCalled()
})

test('should notify subsribers when new query with initialData is added', async () => {
test('should notify subscribers when new query with initialData is added', async () => {
const callback = jest.fn()

queryCache.subscribe(callback)
Expand Down Expand Up @@ -156,6 +156,20 @@ describe('queryCache', () => {
expect(query.state.isStale).toBe(false)
})

test('query interval is cleared when unsubscribed to a refetchInterval query', async () => {
const queryKey = 'key'
const fetchData = () => Promise.resolve('data')
await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0, refetchInterval: 1 })
const query = queryCache.getQuery(queryKey)
const instance = query.subscribe()
instance.updateConfig(query.config)
expect(instance.refetchIntervalId).not.toBeUndefined()
instance.unsubscribe()
expect(instance.refetchIntervalId).toBeUndefined()
await sleep(10)
expect(queryCache.getQuery(queryKey)).toBeUndefined()
})

test('query is garbage collected when unsubscribed to', async () => {
const queryKey = 'key'
const fetchData = () => Promise.resolve('data')
Expand Down

1 comment on commit 2cfd164

@vercel
Copy link

@vercel vercel bot commented on 2cfd164 Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.