How do I listen for when a Query is being released after the staleTime has elapsed? #1992
-
Is there a way to capture when a query is cleaned up due to the query being out of scope for a long period of time? I ask because I am wrapping a real-time connection with query for GETs and I have the query updated with the connection and honestly it works wonderfully. This pattern also works nicely for local data, should you want to do that. Anyway, I want to be able to pass in a 'OnCleanup' or similar function to the Options you can pass into a query function. I want to do this so that the queryFunction basically becomes a 'Keep this socket alive' observer, and when the query has been busted from the cache, it will be capable of cleaning up that connection. Does anyone know if you can currently do this? And if not, how difficult would this be to do? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
i don’t think there is. The callbacks work on a per-observer level, and inactive queries have no observer per definition. So when the query is garbage collected, there is no callback on useQuery to call, because there is no mounted useQuery instance.
Such a callback would need to be on the cache itself (see above reasoning)
how are you doing that please? do you not need to spawn up a useEffect? If so, tying this to the component lifecycle with the effect cleanup function seems like the way to go. |
Beta Was this translation helpful? Give feedback.
-
I have a hook that wraps react query, and currently it only unsubscribes the websocket when react-query attempts to open a new one, I am still determining when to unsubscribe.
Another option is to remove the websocket from react-query logic entirely and simply have a query point be a local state that is updated in response to pushed messages via queryClient.setQueryData. But then I have to have a seperate set of things to manage the websocket connections. I was hoping to kill both in one fell swoop. Like, having react-query be what determines when my connections are active, but still keep that data cached in the meantime. As for what I'm doing, I'm using firebase. Check below (I just finished writing this code, its likely a mess and has bugs still)
|
Beta Was this translation helpful? Give feedback.
I have a hook that wraps react query, and currently it only unsubscribes the websocket when react-query attempts to open a new one, I am still determining when to unsubscribe.