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 1eebfc9 commit 87ea4ec
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 27 deletions.
13 changes: 12 additions & 1 deletion packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ export class QueryObserver<
}

updateResult(notifyOptions?: NotifyOptions): void {

const prevResult = this.#currentResult as
| QueryObserverResult<TData, TError>
| undefined
Expand Down Expand Up @@ -700,9 +701,18 @@ export class QueryObserver<

return Object.keys(this.#currentResult).some((key) => {
const typedKey = key as keyof QueryObserverResult
if (!includedProps.has(typedKey)) {
return false
}
const changed = this.#currentResult[typedKey] !== prevResult[typedKey]

return changed && includedProps.has(typedKey)
console.log('changed', {
key,
changed,
includedProps,
})

return changed;
})
}

Expand All @@ -716,6 +726,7 @@ export class QueryObserver<
#updateQuery(): void {
const query = this.#client.getQueryCache().build(this.#client, this.options)

console.log('updating query')
if (query === this.#currentQuery) {
return
}
Expand Down
21 changes: 16 additions & 5 deletions packages/react-query/src/__tests__/useQuery.promise.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
useQuery,
} from '..'
import { QueryCache } from '../index'
import { createQueryClient, queryKey, sleep } from './utils'
import { createDeferred, createQueryClient, queryKey, sleep } from './utils'



describe('useQuery().promise', () => {
const queryCache = new QueryCache()
Expand Down Expand Up @@ -75,11 +77,11 @@ describe('useQuery().promise', () => {
withinDOM().getByText('loading..')
expect(renderedComponents).toEqual([Page, Loading])
}

{
const { renderedComponents, withinDOM } = await renderStream.takeRender()
withinDOM().getByText('test')
expect(renderedComponents).toEqual([Page, MyComponent])
expect(renderedComponents).toEqual([MyComponent])
}
})

Expand Down Expand Up @@ -1035,10 +1037,11 @@ describe('useQuery().promise', () => {
expect(queryFn).toHaveBeenCalledTimes(0)
})

it('should show correct data when switching between cache entries without re-fetches', async () => {
it.only('should show correct data when switching between cache entries without re-fetches', async () => {
const key = queryKey()
const renderStream = createRenderStream({ snapshotDOM: true })


function MyComponent(props: { promise: Promise<string> }) {
useTrackRenders()
const data = React.use(props.promise)
Expand Down Expand Up @@ -1091,7 +1094,15 @@ describe('useQuery().promise', () => {
expect(renderedComponents).toEqual([MyComponent])
}

rendered.getByText('inc').click()
{
rendered.getByText('inc').click()

const { renderedComponents, withinDOM } = await renderStream.takeRender()
withinDOM().getByText('test0')
console.log({renderedComponents})
expect(renderedComponents).toEqual([Page, MyComponent])

}

{
const { renderedComponents, withinDOM } = await renderStream.takeRender()
Expand Down
13 changes: 13 additions & 0 deletions packages/react-query/src/__tests__/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ export function setIsServer(isServer: boolean) {
}

export const doNotExecute = (_func: () => void) => true


export function createDeferred<TValue>() {
let resolve: (value: TValue) => void;
let reject: (error: unknown) => void;
const promise = new Promise<TValue>((res, rej) => {
resolve = res;
reject = rej;
});

return { promise, resolve: resolve!, reject: reject! };
}
export type Deferred<TValue> = ReturnType<typeof createDeferred<TValue>>;
53 changes: 33 additions & 20 deletions packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,41 @@ 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(() => {
console.log('setting options', defaultedOptions)
// 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 })
Expand Down
1 change: 0 additions & 1 deletion packages/react-query/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default defineConfig({
test: {
name: packageJson.name,
dir: './src',
watch: false,
environment: 'jsdom',
setupFiles: ['test-setup.ts'],
coverage: { enabled: true, provider: 'istanbul', include: ['src/**/*'] },
Expand Down

0 comments on commit 87ea4ec

Please sign in to comment.