react-nativereduxredux-toolkitredux-thunkoffline-caching

Queue and pause Redux actions while offline


I want to pause all dispatched thunks when I am offline and resume when I'm back online. Is createListenerMiddleware a good option for this?

I could create like a seperate redux slice to save the dispatched actions and just redispatch them, but this will lead to existing thunks returning a rejected Promise and throwing errors where called. I want the consumers to just not resolve the promise until internet is back again.

Would you use createListenerMiddleware for this or how can I pause redux thunk actions until online event triggers. I guess I cannot intercept thunks in normal middleware, right?

I tried also different libraries for that but I could not get any of them working because not maintained / broken types / etc.


Solution

  • No, the listener middleware is intentionally for "listening". It doesn't have the ability to stop or pause actions, and it runs after an action has already reached the reducers.

    You may want to look at https://redux-offline.github.io/redux-offline/ instead.

    There are also a number of Redux middleware for various forms of pausing, throttling, and similar behavior listed at https://github.com/markerikson/redux-ecosystem-links/blob/master/middleware-async.md#timeouts-and-delays . However, most of those were collected prior to 2018, and most likely few of them are written in TS. Still, some of those may give you ideas or code you can paste into your project for writing your own custom middleware.