Repository navigation
feat(react-query): treat async iterables as arrays - #5741
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
| expect(states).toMatchInlineSnapshot(` | ||
| Array [ | ||
| Object { | ||
| "data": undefined, | ||
| "status": "pending", | ||
| }, | ||
| Object { | ||
| "data": Array [], | ||
| "status": "pending", | ||
| }, | ||
| Object { | ||
| "data": Array [ | ||
| 1, | ||
| ], | ||
| "status": "pending", | ||
| }, | ||
| Object { | ||
| "data": Array [ | ||
| 1, | ||
| 2, | ||
| ], | ||
| "status": "pending", | ||
| }, | ||
| Object { | ||
| "data": Array [ | ||
| 1, | ||
| 2, | ||
| 3, | ||
| ], | ||
| "status": "success", | ||
| }, | ||
| ] | ||
| `); |
| const queryHash = hashKey(queryKey); | ||
|
|
||
| const queryCache = queryClient.getQueryCache(); | ||
|
|
||
| const query = queryCache.build(queryClient, { | ||
| queryKey, | ||
| queryHash, | ||
| }); | ||
|
|
||
| query.setState({ | ||
| data: [], | ||
| status: 'pending', | ||
| }); | ||
|
|
||
| const aggregate: unknown[] = []; | ||
| for await (const value of result) { | ||
| aggregate.push(value); | ||
|
|
||
| query.setState({ | ||
| data: [...aggregate], | ||
| }); | ||
| } | ||
| return aggregate; | ||
| } |
Diagnostics ComparisonDetailsNumbers
Timings and averages
unstable timingsUnstable
|
| const query = queryCache.build(queryClient, { | ||
| queryKey, | ||
| queryHash, | ||
| }); |
There was a problem hiding this comment.
i've never used the .build() method, but for all the methods I've used, I think I never needed both the queryHash and the queryKey.
Does this still work with only the queryHash?
| const query = queryCache.build(queryClient, { | |
| queryKey, | |
| queryHash, | |
| }); | |
| const query = queryCache.build(queryClient, { | |
| queryHash, | |
| }); |
There was a problem hiding this comment.
On my phone but think they were both required
There was a problem hiding this comment.
You're right, because .build can "create" a query, even if it does not yet exist in the cache.
Are there methods in tRPC that could kick off a tanstack/query query without going through the cache manager? Like if we use useQuery, it immediately creates the query.
- if there are such methods, then
.build()seems indeed appropriate - if there aren't, maybe
const query = queryCache.get(queryHash)could suffice
Also, maybe it doesn't matter and TkDodo can tell us more!
Here, we're inside useQuery, so my understanding is that the query has to exist in the cache manager at this point
| query.setState({ | ||
| data: [], | ||
| status: 'pending', | ||
| }); |
There was a problem hiding this comment.
Would love TkDodo's opinion on this. When I was trying to think of this DX I couldn't decide between the possible "is streaming" states
status: 'pending', fetchStatus: 'fetching', data: <defined>status: 'success', fetchStatus: 'fetching', data: <defined>status: 'success', fetchStatus: 'idle', data: <defined>
There was a problem hiding this comment.
Hmm you might have a point - I rarely think of fetchStatus
There was a problem hiding this comment.
in pending state, data should be undefined. That's how we have it typed, pending means "no data": https://github.com/TanStack/query/blob/8f55e36a7c94242176875bbb18c63ab6991c6af0/packages/query-core/src/types.ts#L651-L658
if data is defined, this is probably the most correct representation:
status: 'success', fetchStatus: 'fetching', data: <defined>
|
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |
Closes #
π― Changes
When using streaming and react-query, it doesn't make sense to deal with async iterables.
Instead, here, we aggregate and update the query cache as results are propagated
Thread on discord that prompted this: https://discord.com/channels/867764511159091230/1243613771981652070
Docs: https://www-git-05-26-iterable-query-trpc.vercel.app/docs/client/react/useQuery#streaming
β Checklist