RFC: create subscriptions for query #8524
Replies: 1 comment 2 replies
-
Thanks for proposing this. What you are basically proposing is an observer that only watches a cache entry, but can never actually fill it. That requires this observer to be created further down the component tree than another one that has a valid We’ve thought about a Usually, components with Your Perf wise, it yields another problem though: One an update comes in, the component that has Another problem is that you might not want a All in all, I think there are too many “ifs” here for us to provide this functionality out of the box right now. What you can do is implement a As a similar example, have a look at the It subscribes to the mutation cache and uses For queries, we always said “just If this works well for you, and we find a good way to create Also note that without observers, a query is not considered “active”, as a direct cache subscription is not a subscription to a query per se. So you have to be aware of potential garbage collection, too. |
Beta Was this translation helpful? Give feedback.
-
Hello everyone,
I would like to express my gratitude for this exceptional library. It is undoubtedly a crucial component of contemporary architecture.
The Problem
We are currently developing an IDE that must be capable of displaying thousands of elements simultaneously and efficiently updating them. To achieve this, we have integrated react-query, suspense, and selectors. However, we have encountered performance limitations that require our attention. Notably, approximately 80% of the mounting time is allocated to react-query subscriptions. Additionally, creating a subscription necessitates the transmission of superfluous data (from the perspective of nested components) to recreate queryFn and queryKey. This process introduces complexity into testing, necessitating the mocking of unnecessary components.
The performance issues associated with subscriptions have been discussed in detail in this thread: https://x.com/MrFlashAccount/status/1857881760110952926.
Several attempts have been made by @TkDodo to address these performance concerns, but the proposed solutions potentially involve breaking changes.
For reference, please see the following links:
Proposed Solution
To address the performance concerns, we propose introducing a new field named
subscription
to thequery
object. This lightweight wrapper will eliminate stale time checks, garbage collection, and other query-related operations, leaving only subscription-related options (such asselect
).The query-related options are not needed, because subscription exist only when
useQuery
exist.We will also introduce a new hook called
useSubscription
that accepts asubscription
property and returns the relevant data.For demonstration purposes, we have provided an example using
zustand
and@tanstack/react-query
: https://codesandbox.io/p/devbox/react-query-subscription-q75dpg. Please note that this example serves as a demonstration and should be adapted to address various edge cases.Usage of the API: https://codesandbox.io/p/devbox/react-query-subscription-q75dpg?file=/src/App.tsx:51,2
Potential API could look like:
Things to address:
use
API in React (should it extend theUsable
interface? Should it be just a promise?)Implementation details
No idea ATM as I'm not deeply familiar with query internals.
Beta Was this translation helpful? Give feedback.
All reactions