Skip to content

Commit

Permalink
fix(suspense): Suspense instances now unmount properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed May 11, 2020
1 parent 03614d1 commit 83ced96
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ yarn-error.log*
.history
size-plugin.json
stats.html
.vscode/settings.json
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"workbench.colorCustomizations": {
"titleBar.activeBackground": "#ff4154", // change this color!
"titleBar.inactiveBackground": "#ff4154", // change this color!
"titleBar.activeBackground": "#00da63", // change this color!
"titleBar.inactiveBackground": "#00da63", // change this color!
"titleBar.activeForeground": "#ffffff", // change this color!
"titleBar.inactiveForeground": "#ffffff" // change this color!
}
Expand Down
7 changes: 6 additions & 1 deletion src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ export function makeQueryCache() {
}

query.updateInstance = instance => {
// Filter out any placeholder instances created for suspense
query.instances = query.instances.filter(d => !d.isPlaceholder)

let found = query.instances.find(d => d.id === instance.id)

if (found) {
Expand All @@ -291,7 +294,9 @@ export function makeQueryCache() {

// Return the unsubscribe function
return () => {
query.instances = query.instances.filter(d => d.id !== instanceId)
query.instances = query.instances.filter(
d => !d.isPlaceholder && d.id !== instanceId
)

if (!query.instances.length) {
// Cancel any side-effects
Expand Down
29 changes: 20 additions & 9 deletions src/useBaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,30 @@ export function useBaseQuery(queryKey, queryVariables, queryFn, config = {}) {
[query]
)

// Create or update this instance of the query
query.updateInstance({
id: instanceId,
onStateUpdate: () => rerender({}),
onSuccess: data => getLatestConfig().onSuccess(data),
onError: err => getLatestConfig().onError(err),
onSettled: (data, err) => getLatestConfig().onSettled(data, err),
})
const updateInstance = React.useCallback(
isPlaceholder => {
query.updateInstance({
id: instanceId,
isPlaceholder,
onStateUpdate: () => rerender({}),
onSuccess: data => getLatestConfig().onSuccess(data),
onError: err => getLatestConfig().onError(err),
onSettled: (data, err) => getLatestConfig().onSettled(data, err),
})
},
[getLatestConfig, instanceId, query, rerender]
)

// Create the placeholder instance of this query (suspense needs this to
// to fire things like onSuccess/onError/onSettled)
updateInstance(true)

// After mount, subscribe to the query
React.useEffect(() => {
// Update the instance to the query again, but not as a placeholder
updateInstance()
return query.subscribe(instanceId)
}, [getLatestConfig, instanceId, query, rerender])
}, [getLatestConfig, instanceId, query, rerender, updateInstance])

React.useEffect(() => {
// Perform the initial fetch for this query if necessary
Expand Down

0 comments on commit 83ced96

Please sign in to comment.