简体   繁体   English

Redux如何保证没有竞争条件?

[英]How can Redux guarantee no race condition?

I've recently learned about Redux. 我最近了解了Redux。
I well understood the concept but I don't figure out this line in the official documentation : 我很好理解这个概念,但我在官方文档中没有弄清楚这一行:

Because all changes are centralized and happen one by one in a strict order, there are no subtle race conditions to watch out for. 因为所有的变化都是集中的,并且按照严格的顺序逐个发生,所以没有细微的竞争条件需要注意。

How can Redux guarantee no subtle race conditions? Redux如何保证不会出现微妙的竞争条件?

Indeed, I can imagine this scenario (in order): 实际上,我可以想象这种情况(按顺序):

  1. Some component dispatches ACTION_TYPE_1. 某些组件调度ACTION_TYPE_1。
  2. Some component dispatches ACTION_TYPE_2. 某些组件调度ACTION_TYPE_2。
  3. ACTION_CREATOR_1 makes an ajax call to grab some data. ACTION_CREATOR_1进行ajax调用以获取一些数据。
  4. ACTION_CREATOR_2 makes an ajax call to grab some data. ACTION_CREATOR_2进行ajax调用以获取一些数据。
  5. ACTION_CREATOR_2 treats grabbed data before ACTION_CREATOR_1 does. ACTION_CREATOR_2 ACTION_CREATOR_1 之前处理抓取的数据。
  6. Possible weird behavior due to this race condition? 由于这种竞争条件可能会出现奇怪的行为?

So actions creators dispatching would be applied in order, but implementations of those actions creators might not apply in order, possibly leading to non-deterministic code. 因此,动作创建者调度将按顺序应用,但这些动作创建者的实现可能不会按顺序应用,可能导致非确定性代码。

How to figure out the quote? 如何弄清楚报价?

I can understand that with Redux, there's a kind of trace about dispatched actions' history (helping to understand the program workflow at runtime), but what about the real implementations of those actions? 我可以理解,对于Redux,有一种关于调度操作历史的跟踪 (有助于在运行时理解程序工作流),但这些操作的实际实现又如何呢?

I've just came across this point , very meaningful, by @DanAbramov himself. @DanAbramov本人,我刚刚遇到了这一点 ,非常有意义。

I think it well answers to my OP. 我认为这很好地解答了我的OP。

The point is that as long as dispatching of actions remains synchronous (despite that actions creators internal process might not be synchronous at all), it's easy to figure out quickly about a race condition (or other issues regarding non-determinism) by establishing and logging an history of dispatched actions. 关键是只要调度操作保持同步 (尽管创建者内部进程可能根本不同步),通过建立和记录很容易快速找出竞争条件(或其他有关非确定性的问题)派遣行动的历史。
Especially it's easy to reproduce the "weird" state to then figure out how to avoid it (like Event-Sourcing ). 特别是很容易重现“怪异”状态,然后弄清楚如何避免它(如事件采购 )。

Without any synchronous and visible point in time , it would be very hard to master what happened; 没有任何同步和可见的时间点,很难掌握发生的事情; that's why Redux brings a benefit. 这就是Redux带来好处的原因。

Redux does not avoid race conditions, but does reduce drastically subtle (hard to analyse) race conditions, as documentation states. 正如文档所述,Redux并没有避免竞争条件,但确实减少了极其微妙 (难以分析)的竞争条件。

  1. Javascript is thread safe so 2 functions cannot run at the same time. Javascript是线程安全的,因此2个函数不能同时运行。

  2. Redux does not know about asynchronous operations, it only cares about the current state, events that are dispatched to it and how to transition current state to some other state given an event. Redux不了解异步操作,它只关心当前状态,调度给它的事件以及如何在给定事件的情况下将当前状态转换到某个其他状态。

  3. Thus ensuring action dispatching order is your responsibility, it's not a responsibility of Redux. 因此,确保行动调度顺序是您的责任,这不是Redux的责任。

  4. If there are two events, X and Y, and you want to prevent handling Y before X is handled, then you can trace it in your Redux state using some state properties. 如果有两个事件,X和Y,并且您希望在处理X之前阻止处理Y,那么您可以使用某些状态属性在Redux状态中跟踪它。 Thus you can easiliy prevent handling Y before X is handled. 因此,您可以轻松地防止在处理X之前处理Y.

  5. There are too many different requirements for asynchronous operations and error handling people may wish to apply. 对于异步操作和人们可能希望应用的错误处理有太多不同的要求。 Thus it is difficult to provide a general solution for "race conditions" you are talking about. 因此很难为你所谈论的“竞争条件”提供一般解决方案。

  6. Design your system, find all edge cases, and create a new event/event name/event handling for each edge case you can have. 设计您的系统,查找所有边缘案例,并为您可以拥有的每个边缘案例创建新的事件/事件名称/事件处理。 Then handle them in your redux reducer, make proper state transitions. 然后在redux减速器中处理它们,进行适当的状态转换。 In Web applications, nothing is a deadly error (it's not a good UI/UX to ask users to refresh their browsers) so you must recover from every possible edge case, thus throwing errors don't make much sense. 在Web应用程序中,没有什么是致命错误(要求用户刷新浏览器不是一个好的UI / UX),因此您必须从每个可能的边缘情况中恢复,因此抛出错误没有多大意义。

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

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