Skip to content

feat(react-query): treat async iterables as arrays - #5741

Merged
KATT merged 21 commits into
nextfrom
05-26-iterable-query
May 28, 2024
Merged

KATT merged 21 commits into
nextfrom
05-26-iterable-query

Conversation

@KATT

@KATT KATT commented May 26, 2024 •

Copy link
Copy Markdown
Member

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

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@KATT KATT changed the title feat(react-query): treak async iterables as arrays feat(react-query): treat async iterables as arrays May 26, 2024
@vercel

vercel Bot commented May 26, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
next-prisma-starter βœ… Ready (Inspect) Visit Preview May 28, 2024 1:09pm
og-image βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback May 28, 2024 1:09pm
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback May 28, 2024 1:09pm

Comment on lines +246 to +278
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",
},
]
`);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the different states

Comment on lines +190 to +213
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;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @TkDodo does this look good to you?

@KATT
KATT marked this pull request as ready for review May 26, 2024 09:55
@KATT
KATT requested a review from a team as a code owner May 26, 2024 09:55
@github-actions

github-actions Bot commented May 26, 2024 •

Copy link
Copy Markdown
Contributor

Diagnostics Comparison

Details

Numbers

Metric PR next
Files 798 798 (βž– 0)
Lines of Library 40,640 40,640 (βž– 0)
Lines of Definitions 120,271 120,269 (πŸ”Ί 2)
Lines of TypeScript 4,967 4,967 (βž– 0)
Lines of JavaScript 0 0 (βž– 0)
Lines of JSON 0 0 (βž– 0)
Lines of Other 0 0 (βž– 0)
Identifiers 176,023 176,015 (πŸ”Ί 8)
Symbols 109,480 109,477 (πŸ”Ί 3)
Types 89 89 (βž– 0)
Instantiations 0 0 (βž– 0)
Memory used 173,443 171,556 (πŸ”Ί 1,887)
Assignability cache size 0 0 (βž– 0)
Identity cache size 0 0 (βž– 0)
Subtype cache size 0 0 (βž– 0)
Strict subtype cache size 0 0 (βž– 0)

Timings and averages

Metric PR next
max (s) 4.358 4.299 (πŸ”Ί 0.06)
min (s) 4.358 4.299 (πŸ”Ί 0.06)
avg (s) 4.358 4.299 (πŸ”Ί 0.06)
median (s) 4.358 4.299 (πŸ”Ί 0.06)
length 1 1 (βž– 0)
unstable timings

Unstable

Timings are not reliable in here

Metric PR next
I/O Read time 0.04 0.04 (βž– 0)
Parse time 0.71 0.7 (πŸ”Ί 0.01)
ResolveTypeReference time 0.03 0.03 (βž– 0)
ResolveModule time 0.11 0.1 (πŸ”Ί 0.01)
ResolveLibrary time 0.02 0.02 (βž– 0)
Program time 1.05 1.02 (πŸ”Ί 0.03)
Bind time 0.44 0.41 (πŸ”Ί 0.03)
Total time 1.49 1.43 (πŸ”Ί 0.06)

Comment on lines +194 to +197
const query = queryCache.build(queryClient, {
queryKey,
queryHash,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
const query = queryCache.build(queryClient, {
queryKey,
queryHash,
});
const query = queryCache.build(queryClient, {
queryHash,
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my phone but think they were both required

@Sheraff Sheraff May 26, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +199 to +202
query.setState({
data: [],
status: 'pending',
});

@Sheraff Sheraff May 26, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm you might have a point - I rarely think of fetchStatus

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @TkDodo wdyt is best here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

@github-actions

Copy link
Copy Markdown
Contributor

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.

This branch had an error being deployed

1 failed (outdated) and 5 active deployments
Preview – www β€” 85c07721 Deployed May 28, 2024 by vercel[bot]
Preview – next-prisma-starter β€” 85c07721 Deployed May 28, 2024 by vercel[bot]
Preview – trpc-next-app-dir β€” 85c07721 Deployed May 28, 2024 by vercel[bot]
Preview – todomvc β€” 85c07721 Deployed May 28, 2024 by vercel[bot]
Preview – og-image β€” 85c07721 Deployed May 28, 2024 by vercel[bot]
Preview – trpc-sse β€” 2dd35eb0 Deployed May 26, 2024 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants