Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Replace solution with state marker check and streamline test
  • Loading branch information
kamranayub committed Apr 8, 2020
commit 5835d22e8359843ebac556bb5125ad446e381176
21 changes: 16 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{
"workbench.colorCustomizations": {
"titleBar.activeBackground": "#ff4154", // change this color!
"titleBar.inactiveBackground": "#ff4154", // change this color!
"titleBar.activeForeground": "#ffffff", // change this color!
"titleBar.inactiveForeground": "#ffffff" // change this color!
}
"activityBar.background": "#ff7482",
"activityBar.activeBackground": "#ff7482",
"activityBar.activeBorder": "#70ff60",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#70ff60",
"activityBarBadge.foreground": "#15202b",
"titleBar.activeBackground": "#ff4154",
"titleBar.inactiveBackground": "#ff415499",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveForeground": "#e7e7e799",
"statusBar.background": "#ff4154",
"statusBarItem.hoverBackground": "#ff7482",
"statusBar.foreground": "#e7e7e7"
},
"peacock.color": "#ff4154"
}
12 changes: 10 additions & 2 deletions src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const queryCache = makeQueryCache()
const actionInit = {}
const actionFailed = {}
const actionMarkStale = {}
const actionMarkGC = {}
const actionFetch = {}
const actionSuccess = {}
const actionError = {}
Expand Down Expand Up @@ -238,11 +239,11 @@ export function makeQueryCache() {
}

query.scheduleGarbageCollection = () => {
const scheduledAt = Date.now()
dispatch({ type: actionMarkGC })
query.cacheTimeout = setTimeout(
() => {
cache.removeQueries(
d => d.state.updatedAt < scheduledAt && d.queryHash === query.queryHash
d => d.state.markedForGarbageCollection && d.queryHash === query.queryHash
)
},
typeof query.state.data === 'undefined' &&
Expand Down Expand Up @@ -450,6 +451,7 @@ export function defaultQueryReducer(state, action) {
canFetchMore: false,
failureCount: 0,
isStale: action.isStale,
markedForGarbageCollection: false,
data: action.initialData,
updatedAt: action.hasInitialData ? Date.now() : 0,
}
Expand All @@ -463,6 +465,12 @@ export function defaultQueryReducer(state, action) {
...state,
isStale: true,
}
case actionMarkGC: {
return {
...state,
markedForGarbageCollection: true,
}
}
case actionFetch:
return {
...state,
Expand Down
72 changes: 0 additions & 72 deletions src/tests/cleanup.js

This file was deleted.

39 changes: 0 additions & 39 deletions src/tests/cleanup.test.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/tests/queryCache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,34 @@ describe('queryCache', () => {
await sleep(50)
expect(query.state.isStale).toBe(false)
})

test('query is garbage collected when unsubscribed to', async () => {
const queryKey = 'key'
const fetchData = () => Promise.resolve('data')
await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0 })
const query = queryCache.getQuery(queryKey)
expect(query.state.markedForGarbageCollection).toBe(false)
const unsubscribe = query.subscribe(1)
unsubscribe()
expect(query.state.markedForGarbageCollection).toBe(true)
await sleep(1)
expect(queryCache.getQuery(queryKey)).toBeUndefined()
})

test('query is not garbage collected unless markedForGarbageCollection is true', async () => {
const queryKey = 'key'
const fetchData = () => Promise.resolve(undefined)
await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0 })
const query = queryCache.getQuery(queryKey)
expect(query.state.markedForGarbageCollection).toBe(false)
const unsubscribe = query.subscribe(1)
unsubscribe()
expect(query.state.markedForGarbageCollection).toBe(true)
queryCache.clear()
queryCache.setQueryData(queryKey, 'data')
await sleep(1)
const newQuery = queryCache.getQuery(queryKey)
expect(newQuery.state.markedForGarbageCollection).toBe(false)
expect(newQuery.state.data).toBe('data')
})
})