reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.69k stars 1.16k forks source link

RTK Query Streaming updates: Waiting for the initial call and subscription to websocket stream race #3354

Closed mdeperas closed 1 year ago

mdeperas commented 1 year ago

Hi, thank you for your hard work with this fantastic library.

I have a question about streaming updates and how they work. My understanding from the docs is that:

So we want to fill in the cache data, then we are listening to the websocket events.

1) Is it possible that between the moment that initial call returns the data and the moment when we start to listen to websocket events, some data will be sent through websocket channel and it will be lost?

2) Is it possible to change the order of actions and first subscribe to websocket channel and then fire the query to API? Does it makes any sense to do that in case of the problem described in the point 1?

Thanks for any help.

markerikson commented 1 year ago

Yeah, we've got this described in https://redux-toolkit.js.org/rtk-query/usage/streaming-updates#using-the-oncacheentryadded-lifecycle :

Typically, you will await cacheDataLoaded to determine when the first data has been fetched, then use the updateCacheData utility to apply streaming updates as messages are received.

mdeperas commented 1 year ago

Thanks for the answer. I've read the docs and I understand the typical scenario.

My concern is that I may loose some data that would be sent between the moment the initial call will feed the cache and I will start listening to the websocket events. Is it even worth to consider such scenario or it is not likely to happen?

mdeperas commented 1 year ago

Is it possible to apply opposite scenario? First listen to streaming updates and then ask for the cache data to load?

mdeperas commented 1 year ago

Thanks @markerikson