Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Dec 13, 2024
1 parent bc36411 commit 63c548c
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,50 @@ export function useBaseQuery<
),
)

const result = observer.getOptimisticResult(defaultedOptions)

React.useSyncExternalStore(
React.useCallback(
(onStoreChange) => {
const unsubscribe = isRestoring
? noop
: observer.subscribe(notifyManager.batchCalls(onStoreChange))

// Update result to make sure we did not miss any query updates
// between creating the observer and subscribing to it.
observer.updateResult()

return unsubscribe
},
[observer, isRestoring],
),
() => observer.getCurrentResult(),
() => observer.getCurrentResult(),
)
const [result, setResult] = React.useState(() => observer.getOptimisticResult(defaultedOptions))

// console.log('result', result)
React.useEffect(() => {
if (isRestoring) {
return
}
console.log('subscribing to observer')


const unsubscribe = observer.subscribe(
notifyManager.batchCalls((newResult) => {

setResult((prev) => {
console.log('got new result', {
prev,
newResult,
})
return newResult


})
})
)

// Update result to make sure we did not miss any query updates
// between creating the observer and subscribing to it.
observer.updateResult()

return unsubscribe
}, [observer, isRestoring])


React.useEffect(() => {

// Do not notify on updates because of changes in the options because
// these changes should already be reflected in the optimistic result.
observer.setOptions(defaultedOptions, { listeners: false })
}, [defaultedOptions, observer])


React.useEffect(() => {
console.log('new query key', defaultedOptions.queryHash)
}, [defaultedOptions.queryHash])
// Handle suspense
if (shouldSuspend(defaultedOptions, result)) {
throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
Expand Down

0 comments on commit 63c548c

Please sign in to comment.