Skip to content

Commit

Permalink
fix(solid-query): Defer observer unsubscription untill resource resol…
Browse files Browse the repository at this point in the history
…ves (#6982)
  • Loading branch information
ardeora authored Mar 2, 2024
1 parent bcfe218 commit a680dba
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/solid-query/src/createBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export function createBaseQuery<

const client = createMemo(() => useQueryClient(queryClient?.()))
const isRestoring = useIsRestoring()
// There are times when we run a query on the server but the resource is never read
// This could lead to times when the queryObserver is unsubscribed before the resource has loaded
// Causing a time out error. To prevent this we will queue the unsubscribe if the cleanup is called
// before the resource has loaded
let unsubscribeQueued = false

const defaultedOptions = createMemo(() => {
const defaultOptions = client().defaultQueryOptions(options())
Expand Down Expand Up @@ -155,13 +160,22 @@ export function createBaseQuery<

if (unwrappedResult.isError) {
reject(unwrappedResult.error)
unsubscribeIfQueued()
} else {
resolve(unwrappedResult)
unsubscribeIfQueued()
}
})()
})
}

const unsubscribeIfQueued = () => {
if (unsubscribeQueued) {
unsubscribe?.()
unsubscribeQueued = false
}
}

const createClientSubscriber = () => {
const obs = observer()
return obs.subscribe((result) => {
Expand Down Expand Up @@ -295,6 +309,10 @@ export function createBaseQuery<
})

onCleanup(() => {
if (isServer && queryResource.loading) {
unsubscribeQueued = true
return
}
if (unsubscribe) {
unsubscribe()
unsubscribe = null
Expand Down

0 comments on commit a680dba

Please sign in to comment.