-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add getStatus to createAsyncThunk to return promise state #418
Add getStatus to createAsyncThunk to return promise state #418
Conversation
msutkowski
commented
Mar 10, 2020
- An example implementation of how a user could track the status of a long-running process per convo in Fix bug in docs for createAsyncThunk example #417
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit ddcee04:
|
I wouldn't really want to make the promise that observable - it will keep moving logic from the reducers/middleware to the component level. Also, I think this doesn't solve the problem the code example tried to solve. |
Which also brings us to the point that we currently do not have a way to cancel a running asyncThunk that we do not have an object handle on - which is all we can do with only the thunkMiddleware right now. But it means that another component cannot abort a running asyncThunk that was started elsewhere. If we wanted to have a way to cancel running asyncThunks, we could add a middleware that
What do you two think @markerikson @msutkowski ? |
That's actually where I was going to go with this idea. I see value doing this outside of redux, but like I said in that issue a user can create a simple wrapper and achieve the same thing. |
@phryneas Thought about a few things in regards to this.
Edit:
I think regarding that idea, I might disagree slightly and lean more towards what is seen here: https://redux.js.org/advanced/async-actions/#actionsjs-with-fetch with |
I don't really think we need to worry about this. All other actions would be duplicates as well. That's not an error you don't notice for a long time.
If the user wanted to access that information, they could always store it in the state. I wouldn't allow for too much non-redux logic here. The store should be the source of all truth.
Such a middleware would need to be part of
Maybe something like useEffect(() => {
dispatch(fetchLongRunningQuery.abortAll())
dispatch(fetchLongRunningQuery(params))
}, [params])
Doing this from the payloadcreator might lead to a circular reference unless we'd add those abort-action-creators to the thunkApi object. We might consider doing that to enable both usages, though.
Hmm. Of course, a payloadCreator can always do an early return/reject depending on the state, but at the time the payloadCreator is executed, the function wrapWithStateCondition(asyncThunk, conditionFn) {
return args =>(dispatch, getState) => {
if (conditionFn(getState()) {
dispatch(asyncThunk(args));
}
}
}
// usage:
const wrappedAsyncThunk = wrapWithStateCondition(myAsyncThunk, state => state.users.state === 'idle');
dispatch(wrappedAsyncThunk(args)); And while we can add that as an example, going so far as supporting it per default would probably be too much for RTK, which aims for creating an easy API for the 80% use case. |
I'm feeling like this train of thought is getting out of hand. Cancellation has always been a weak point for thunks. I really don't want to spend any more time trying to solve that kind of problem. We've provided the |
@markerikson Agreed. The good thing is I don't think this is a blocking issue at all, because you could go with @phryneas 's idea or potentially using what I presented and call it a day. Is it worth documenting the ideas we've outlined as a starting point or no? In general, I do like the concept of what we're discussing here. It'd be a good long term solution depending on |
Yeah, let's close this, but try to add some more usage guide docs on cancelation or tracking requests. |
I admit I was having a similar thought. Maybe we should wait for the adoption and if people are actually running into these problems. No real use solving problems nobody's gonna have in the end. |