Skip to content

Commit 87ea4ec

Browse files
committed
wip
1 parent 1eebfc9 commit 87ea4ec

File tree

5 files changed

+74
-27
lines changed

5 files changed

+74
-27
lines changed

packages/query-core/src/queryObserver.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ export class QueryObserver<
649649
}
650650

651651
updateResult(notifyOptions?: NotifyOptions): void {
652+
652653
const prevResult = this.#currentResult as
653654
| QueryObserverResult<TData, TError>
654655
| undefined
@@ -700,9 +701,18 @@ export class QueryObserver<
700701

701702
return Object.keys(this.#currentResult).some((key) => {
702703
const typedKey = key as keyof QueryObserverResult
704+
if (!includedProps.has(typedKey)) {
705+
return false
706+
}
703707
const changed = this.#currentResult[typedKey] !== prevResult[typedKey]
704708

705-
return changed && includedProps.has(typedKey)
709+
console.log('changed', {
710+
key,
711+
changed,
712+
includedProps,
713+
})
714+
715+
return changed;
706716
})
707717
}
708718

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

729+
console.log('updating query')
719730
if (query === this.#currentQuery) {
720731
return
721732
}

packages/react-query/src/__tests__/useQuery.promise.test.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
useQuery,
1313
} from '..'
1414
import { QueryCache } from '../index'
15-
import { createQueryClient, queryKey, sleep } from './utils'
15+
import { createDeferred, createQueryClient, queryKey, sleep } from './utils'
16+
17+
1618

1719
describe('useQuery().promise', () => {
1820
const queryCache = new QueryCache()
@@ -75,11 +77,11 @@ describe('useQuery().promise', () => {
7577
withinDOM().getByText('loading..')
7678
expect(renderedComponents).toEqual([Page, Loading])
7779
}
78-
80+
7981
{
8082
const { renderedComponents, withinDOM } = await renderStream.takeRender()
8183
withinDOM().getByText('test')
82-
expect(renderedComponents).toEqual([Page, MyComponent])
84+
expect(renderedComponents).toEqual([MyComponent])
8385
}
8486
})
8587

@@ -1035,10 +1037,11 @@ describe('useQuery().promise', () => {
10351037
expect(queryFn).toHaveBeenCalledTimes(0)
10361038
})
10371039

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

1044+
10421045
function MyComponent(props: { promise: Promise<string> }) {
10431046
useTrackRenders()
10441047
const data = React.use(props.promise)
@@ -1091,7 +1094,15 @@ describe('useQuery().promise', () => {
10911094
expect(renderedComponents).toEqual([MyComponent])
10921095
}
10931096

1094-
rendered.getByText('inc').click()
1097+
{
1098+
rendered.getByText('inc').click()
1099+
1100+
const { renderedComponents, withinDOM } = await renderStream.takeRender()
1101+
withinDOM().getByText('test0')
1102+
console.log({renderedComponents})
1103+
expect(renderedComponents).toEqual([Page, MyComponent])
1104+
1105+
}
10951106

10961107
{
10971108
const { renderedComponents, withinDOM } = await renderStream.takeRender()

packages/react-query/src/__tests__/utils.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,16 @@ export function setIsServer(isServer: boolean) {
9494
}
9595

9696
export const doNotExecute = (_func: () => void) => true
97+
98+
99+
export function createDeferred<TValue>() {
100+
let resolve: (value: TValue) => void;
101+
let reject: (error: unknown) => void;
102+
const promise = new Promise<TValue>((res, rej) => {
103+
resolve = res;
104+
reject = rej;
105+
});
106+
107+
return { promise, resolve: resolve!, reject: reject! };
108+
}
109+
export type Deferred<TValue> = ReturnType<typeof createDeferred<TValue>>;

packages/react-query/src/useBaseQuery.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,41 @@ export function useBaseQuery<
8282
),
8383
)
8484

85-
const result = observer.getOptimisticResult(defaultedOptions)
86-
87-
React.useSyncExternalStore(
88-
React.useCallback(
89-
(onStoreChange) => {
90-
const unsubscribe = isRestoring
91-
? noop
92-
: observer.subscribe(notifyManager.batchCalls(onStoreChange))
93-
94-
// Update result to make sure we did not miss any query updates
95-
// between creating the observer and subscribing to it.
96-
observer.updateResult()
97-
98-
return unsubscribe
99-
},
100-
[observer, isRestoring],
101-
),
102-
() => observer.getCurrentResult(),
103-
() => observer.getCurrentResult(),
104-
)
85+
const [result, setResult] = React.useState(() => observer.getOptimisticResult(defaultedOptions))
86+
87+
// console.log('result', result)
88+
React.useEffect(() => {
89+
if (isRestoring) {
90+
return
91+
}
92+
console.log('subscribing to observer')
93+
94+
95+
const unsubscribe = observer.subscribe(
96+
notifyManager.batchCalls((newResult) => {
97+
98+
setResult((prev) => {
99+
console.log('got new result', {
100+
prev,
101+
newResult,
102+
})
103+
return newResult
104+
105+
106+
})
107+
})
108+
)
109+
110+
// Update result to make sure we did not miss any query updates
111+
// between creating the observer and subscribing to it.
112+
observer.updateResult()
113+
114+
return unsubscribe
115+
}, [observer, isRestoring])
116+
105117

106118
React.useEffect(() => {
119+
console.log('setting options', defaultedOptions)
107120
// Do not notify on updates because of changes in the options because
108121
// these changes should already be reflected in the optimistic result.
109122
observer.setOptions(defaultedOptions, { listeners: false })

packages/react-query/vite.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default defineConfig({
77
test: {
88
name: packageJson.name,
99
dir: './src',
10-
watch: false,
1110
environment: 'jsdom',
1211
setupFiles: ['test-setup.ts'],
1312
coverage: { enabled: true, provider: 'istanbul', include: ['src/**/*'] },

0 commit comments

Comments
 (0)