-
-
Notifications
You must be signed in to change notification settings - Fork 109
Add async computed #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add async computed #686
Conversation
🦋 Changeset detectedLatest commit: 4c7eacc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for preact-signals-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Size Change: 0 B Total Size: 84.5 kB ℹ️ View Unchanged
|
c6d0fc7
to
7a62c6d
Compare
8802208
to
6c7b789
Compare
Would it be helpful for future library users to point out pre-emptively in the documentation that async computeds will be reactive until the first await? For example: asyncComputed(async () => {
const client = await authClient();
const user = await client.getUser(userId.value);
return user;
}); In this made-up example the async computed won't update when the asyncComputed(async () => {
const id = userId.value;
const client = await authClient();
const user = await client.getUser(id);
return user;
}); |
Yes, makes sense to explicitly document this. I thought about it but apparently did not write it 😂 EDIT: ah it's because I kind of did in the blog post I wrote today |
Hi! const userDataSignal = asyncComputed(async ({ abortSignal }) => {
const response = await fetch('/api/user', { signal: abortSignal });
return response.json();
}); This approach is used, for example, in react-query. |
The difference between this primitive and react-query is that it can be used for more than just |
True, it’s broader than |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hype hype hype, probably one of the most requested features? Would be awesome to see!
the issue being that this is based on
useId()
which isn't readily available in React 18 according to the types
useId
came out w/ React 18.0, types are probably incorrect there.
Question: When the input variables change for
computed
would you expect us to erasedata
anderror
or should we retain it and fetch in the background? Alternatively we can add afetching
signal to have a background indicator.EDIT: Added
running
as a way to differentiate between things
That sounds like a great solution to me.
This PR introduces two new utility functions for handling asynchronous computed values in Preact applications:
asyncComputed
anduseAsyncComputed
. These helpers make it easier to work with async data in a reactive way while maintaining proper TypeScript support and Suspense compatibility.asyncComputed<T>
A utility function that creates a signal that computes its value asynchronously. It handles both synchronous and asynchronous computations while providing proper error handling and state management.
Key features:
Implementation Details
Question: When the input variables change for
computed
would you expect us to erasedata
anderror
or should we retain it and fetch in the background? Alternatively we can add afetching
signal to have a background indicator.EDIT: Added
running
as a way to differentiate between thingsReact
In theory this should support React Suspense as well as we keep a shallow cache for the computeds, the issue being that this is based on
useId()
which isn't readily available in React 18 according to the types. When the minimum peer-dependency becomes React v19 we could add this for React as well, or we come up with a different way to track our key in the computed-cache.