From 63c548cd98866a48bb030156d1d8a2151265f7d8 Mon Sep 17 00:00:00 2001 From: Alexander Johansson Date: Fri, 13 Dec 2024 12:10:06 +0100 Subject: [PATCH] wip --- packages/react-query/src/useBaseQuery.ts | 57 +++++++++++++++--------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/packages/react-query/src/useBaseQuery.ts b/packages/react-query/src/useBaseQuery.ts index bcbf700ef7..a2a52f3886 100644 --- a/packages/react-query/src/useBaseQuery.ts +++ b/packages/react-query/src/useBaseQuery.ts @@ -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)