-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
RFC: Controller.fetchIfStale() #2053
Comments
Council notes
Anansi router example: resolveData: async (controller: Controller, match: { id: string }) => {
if (match) {
// don't block on comments but start fetching
controller.fetchIfStale(CommentResource.getList, { postId: match.id });
const post = await controller.fetchIfStale(PostResource.get, {
id: match.id,
});
await Promise.allSettled([
controller.fetchIfStale(
UserResource.get,
post.userId ? { id: post.userId } : (null as any),
),
controller.fetchIfStale(getImage, {
src: UserResource.fromJS({ id: post.userId }).profileImage,
}),
]);
} |
This would be really helpful for us! Name suggestion: |
Thanks for the input @gregor-mueller ! What do you expect/want for the return value to be? The resolution of the promise, or the processed data from the cache? |
Thanks for the quick response! I'd expect the processed data from the cache. |
@gregor-mueller In case you just want the state, you can use https://resthooks.io/docs/api/Controller#getState The rest of the functionality will be soon added |
Closing in favor of discussion: #2402 |
Motivation
Current world
Currently final ‘stale’ logic exists in hooks
This isn’t too bad; but it does mean some logic is repeated in any implementation.
The non-lifecycle based logic can be extracted as such:
Proposal
💡 Need feedback on name. I don’t like how long fetchIfStale is!Add a new controller dispatch that will only fetch if the result is stale. If it ends up not fetching the promise will immediately resolve.
Open questions
Dispatch resolution control
dispatch adds to action a callback. if this callback is called it returns the dispatch early.
Instead of
And you can still continue processing the request by doing the rest of it async
Doing this would also enable not having to send resolve/reject in fetch meta; but just take dispatch return value. This would be actual promise used by the NetworkManager so referential equality checks could be performed against it.
This means dispatch() now has a variable return type based on what the middlewares do. This could have weird implications for user-defined managers; so perhaps we should add type inference based on middlewares? This can be delayed tho.
The text was updated successfully, but these errors were encountered: