You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a simple race condition in the library right now:
A query that provides a tag starts.
A mutation invalidates the same tag.
The query finishes.
The query is not rerun in this case, despite its tags having been invalidated. I actually noticed this in my application when the query took a bit too long while mutations were applied.
The problem stated here is similar to the problem stated in #2203, but the title/framing is different in this issue.
A simple fix could be to add the option to not invalidate any tags while any queries (or mutations) are running, and only invalidate them after all pending queries are settled. This would also solve #2203. It could, however, cause problems for people using extremely long-running queries (since no tags are invalidated while they run), so it might make sense to allow disabling this behavior.
Implementation-wise, we could add another field to the API slice named pendingTagInvalidations, which contains a list of tags that will be invalidated once all queries are settled. Each time that a mutation settles, it checks if there are any queries/mutations that are in the pending state. If that is the case, it adds the tags to pendingTagInvalidations, else it invalidates them directly. When a query settles, it does the same check and invalidates all tags in pendingTagInvalidations, then clears the list.
Pseudocode:
// api is our API slicefunctionhasPendingRequests(api: ApiSlice){consthasPendingQueries=Object.values(queries).some(query=>query?.status===QueryStatus.pending)consthasPendingMutations=Object.values(mutations).some(mutation=>mutation?.status===QueryStatus.pending);returnhasPendingQueries||hasPendingMutations;}// When a mutation settles:if(hasPendingQueries(api)){api.pendingTagInvalidations.push(...invalidatedTags);}else{invalidateTags(invalidatedTags);}// When a query settles:if(!hasPendingQueries(api)){invalidateTags(api.pendingTagInvalidations);api.pendingTagInvalidations=[];}
The overhead of this shouldn't be too high (in O(#endpoints) ), but if required, we could save the list of pending requests separately (making it O(1)).
I'd be happy to contribute!
The text was updated successfully, but these errors were encountered:
There is a simple race condition in the library right now:
The query is not rerun in this case, despite its tags having been invalidated. I actually noticed this in my application when the query took a bit too long while mutations were applied.
The problem stated here is similar to the problem stated in #2203, but the title/framing is different in this issue.
A simple fix could be to add the option to not invalidate any tags while any queries (or mutations) are running, and only invalidate them after all pending queries are settled. This would also solve #2203. It could, however, cause problems for people using extremely long-running queries (since no tags are invalidated while they run), so it might make sense to allow disabling this behavior.
Implementation-wise, we could add another field to the API slice named
pendingTagInvalidations
, which contains a list of tags that will be invalidated once all queries are settled. Each time that a mutation settles, it checks if there are any queries/mutations that are in thepending
state. If that is the case, it adds the tags topendingTagInvalidations
, else it invalidates them directly. When a query settles, it does the same check and invalidates all tags inpendingTagInvalidations
, then clears the list.Pseudocode:
The overhead of this shouldn't be too high (in
O(#endpoints)
), but if required, we could save the list of pending requests separately (making itO(1)
).I'd be happy to contribute!
The text was updated successfully, but these errors were encountered: