invalidateQueries vs cancelQueries + refetchQueries for in-flight race conditions
I have an edge case I'm trying to work through on an app that combines server-side events + traditional fetching.
It's basically a chat application that receives
The race condition is this:
1) User clicks a chat - this triggers a fetch for the conversation from the DB
2) While that query is fetching, a
3) Original query (#1) completes, message from #2 does not appear in the chat
4) User has to wait ~20 seconds (backup polling mechanism) to see the message from #2
I have code that looks like this:
I'm just wondering if
Been trying to write some tests but this is pretty flaky and tricky logic to get exactly right in a test.
It's basically a chat application that receives
new_message events that update both the inbox and conversation window. The race condition is this:
1) User clicks a chat - this triggers a fetch for the conversation from the DB
2) While that query is fetching, a
new_message event comes in for that in-flight conversation3) Original query (#1) completes, message from #2 does not appear in the chat
4) User has to wait ~20 seconds (backup polling mechanism) to see the message from #2
I have code that looks like this:
I'm just wondering if
invalidateQueries is the correct API here or if cancelQueries + refetchQueries is more appropriate? My instinct is that invalidate would mark the query as stale which would immediately cause another refetch, but I was not 100% positive on the expected behavior of the API or how staleness propagates.Been trying to write some tests but this is pretty flaky and tricky logic to get exactly right in a test.