Replies: 2 comments
-
I would probably just have the mutation invalidate a query that then starts polling with |
Beta Was this translation helpful? Give feedback.
-
I really appreciate your response. Thank you for suggesting that approach! The I wanted to be mindful of the server load, since implementing automatic refetching could potentially generate a significant number of extra requests. If I did refetch on an interval that is more like a minute or two. Maybe it would've help with server load but than it would mean making my user wait up to almost the interval time -in the worst case- which is not ideal. |
Beta Was this translation helpful? Give feedback.
-
I have a situation in my application where the frontend sends a request to update a resource, which triggers an asynchronous background task. When this task completes, it updates the resource in the database, and the UI needs to react to that change.
Here's an example: A user clicks a button to change the text shown in a lesson's video. The server responds with 200 and starts an asynchronous video generation task for the lesson. When finished, it sends a notification to the UI. However, useMutation won't invalidate the queries related to that mutation when video generation finishes, since it has already done so right after receiving the 200 response from the initial mutation request.
I've implemented a system for async invalidation where my custom hook watches for notification changes and triggers the related invalidations when a change occurs. I'll share some pseudo code below that explains my approach.
My question is: Am I overengineering this mechanism? Is there a simpler way to achieve this functionality? While my current system works, it feels overly complex and has edge cases. I'm curious if others have faced similar issues and how they've solved them.
This is how I run my custom mutation hook
This is how it handles the async invalidation decisions:
Then, where I receive notifications, I run the async invalidations set in the app state:
Beta Was this translation helpful? Give feedback.
All reactions