简体   繁体   English

离线时排队并暂停 Redux 次操作

[英]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.我想在我离线时暂停所有派遣的 thunk,并在我重新在线时恢复。 Is createListenerMiddleware a good option for this? createListenerMiddleware 是一个不错的选择吗?

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.我可以创建一个单独的 redux 切片来保存已分派的操作并重新分派它们,但这将导致现有的 thunk 返回一个被拒绝的 Promise 并在调用的地方抛出错误。 I want the consumers to just not resolve the promise until inte.net is back again.我希望消费者在 inte.net 再次回来之前不要解析 promise。

Would you use createListenerMiddleware for this or how can I pause redux thunk actions until online event triggers.您会为此使用 createListenerMiddleware 还是我如何暂停 redux thunk 操作直到在线事件触发。 I guess I cannot intercept thunks in normal middleware, right?我想我无法拦截普通中间件中的 thunk,对吧?

I tried also different libraries for that but I could not get any of them working because not maintained / broken types / etc.我也为此尝试了不同的库,但我无法让它们中的任何一个工作,因为没有维护/损坏类型等。

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.您可能需要查看https://redux-offline.github.io/redux-offline/

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 .还有许多 Redux 中间件,用于各种 forms 的暂停、节流和类似行为,列在https://github.com/markerikson/redux-ecosystem-links/blob/master/middleware-async.md#timeouts-and -延迟 However, most of those were collected prior to 2018, and most likely few of them are written in TS.然而,其中大部分是在 2018 年之前收集的,而且很可能很少有是用 TS 编写的。 Still, some of those may give you ideas or code you can paste into your project for writing your own custom middleware.尽管如此,其中一些可能会为您提供想法或代码,您可以将这些想法或代码粘贴到您的项目中以编写您自己的自定义中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM