Replies: 1 comment 1 reply
-
I like where this is going. I do see how this makes sense looking forward and tighten things up together in a cohesive sets of primitives. On thought occurred to me while integrating with the Today, a getter is wrapped into a function fetchArticle(slug) {
return fetch(`https://api.com/article/${slug}`).then(r => r.json());
}
const [state, createState] = createState({
get article() {
return fetchArticle(props.slug);
}
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
(Updated Feb 2nd 2020) - remove
createResourceMemo
In this RFC I'm looking for feedback on some proposed changes to the Resource APIs. This is inspired mostly from wanting to keep the interfaces simpler to make learning Solid easier, and to remove complicated edge cases from SSR.
The most important changes is I intend to remove
createResourceState
and change the behavior ofcreateResource
to be more dynamic.Background
Resources are an essential part to handling asynchronicity in Solid. Async is not always easy and the library having knowledge let us it do a lot for you in terms of making it easier to control flow. While one can opt into Async updates at anytime simply by plugging a signals into promises:
we can use resources as a way to coordinate placeholders with
Suspense
, do some navigations withuseTransition
, and to know both when we are done rendering on the server, and serve as a channel to stream data.Proposal
Solid will have 1 resource primitive
createResource
.createResource
The new version will carry this signature
The
source
will be a literal or a function that tracks. This is where we generate the data we send to thefetcher
. If thesource
is falsey we do not execute thefetcher
.mutate
does a synchronous update to the data andrefetch
triggers the whole thing again even if it hasn't changed.This example doesn't load until you set the signal as it starts with an
undefined
. In so we keep loading semantics. But we can also just have it autoload both single time or every time props update:There are many patterns we can use here. You define many types of fetcher. Your fetcher could be generic shared across all your resources and you pass the query as the source. Maybe you use GraphQL. The fetcher returns a promise, but it can be a promise for anything. It can also return data synchronously, so building a query cache is possible. By default Solid does nothing special with the source but a library could build on this and get SWR or React Query functionality.
Using Resources with State
We still have a gap though when dealing state. The solution is leveraging the fact that Solid state objects accept other reactive primitives. Furthermore we can use the transform on load to reconcile state differences. In this way we could refresh our data and only notify the specific properties that update.
Impact
This change removes
createResourceState
so it is important that we consider all use cases. If you have been usingcreateResourceState
I'm very interested if these scenarios cover your usage. The biggest challenge withcreateResourceState
was it dynamically created resources on the fly based on property access which are challenging for SSR/Hydration where we need to align them to automatically serialize data across. But this does mean that lists are generally going to be explicit or share a single resource which can mean triggering Suspense more often. With transitions this is smooth, but I might be not considering cases where it would trigger falling back to skeleton loading states unintentionally.This also starts a pattern of leveraging nested signals in
state
. This already existed but this is the first time it has become the defacto solution. I worry about the verbosity of the API. However, since we need to get thesource
function out of the create it makes sense to create the resource separately. I'm also open to feedback about this as well.Beta Was this translation helpful? Give feedback.
All reactions