Replies: 1 comment 1 reply
-
I think it's interesting and I'm open for suggestions. I wouldn't want to add a complicated api for this, but if we can find a straight forward way it could see it happen. So basically, what we would is consecutive calls to |
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
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
First of all want to thank you for this amazing library, it helps a lot to solve many problems in pretty neat way! Anyway, got bumped into one issue which I solved but the solution I did at first glance does not look really good. Or actually it is a new feature which could be integratable in the query library.
Problem:
user clicks add to cart and after each cart addition we need to initialize background synch with the back-end to re-fetch new possible prices for the product. When user first time clicks add to cart and there is no cart, in the back-end we create the cart and return an id, when user clicks add to cart second time, we sent a PATCH request for a given cart id and product to update the cart in the back-end.
The problem I found is that if user does this fast, or has a slower internet connection, when the second useMutation is called it ofcourse does not wait for first one to finish thus creating the cart again which leads to a bug with incorrect cart being synched with the back-end. I solved it by adding some local state to some components - code below ⬇️
the code which initialises add to cart
here the code displays only the mutation trigerring , it happens in debounced function. Also , I use mutation defaults because I need to persist the request even if user changes the page. The code below now has an actual way how the problem was solved ⬇️
note how
requestStack
local state is used here, inmutationFn
if id does not exist I call other mutation to create the cart, now it sets the state for that mutation loading and while it's happening all other requests are being put to local staterequestStack
.Once loading is finished I have a cartId that now I need to attach to the other requests. However now with the below useEffect I need to repeat all the API requests that tried to be sent when the cart was in the creation process.
So this code actually works but I really feel tanstack query library could benefit of this scenario and to have some kind of request stacking functionality implemented under the hood. It could use some kind of an lock mechanism to lock the mutation until the lock is released under certain logic etc...
In any ways, if you find the issue and possible ideas interesting , let's discuss, will be willing to help and contribute myself to the library if you see there can be more people benefiting from this use case. Cheers!
Beta Was this translation helpful? Give feedback.
All reactions