Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (queryInstance.clearInterval) is not called on unmount #717

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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