Flux 调度程序保证同步操作意味着什么?

What does it mean for a Flux dispatcher to guarantee synchronous actions?

在使用(我认为)基于我构建的框架的 Flux 架构构建了一些应用程序之后,我想知道这意味着什么:

Mozilla 说:

"The dispatcher dispatches actions to interested stores. Only one action can be processed at any one time."

或 Facebook 说:

"We need the dispatcher to be able to invoke the callback for Store B, and finish that callback, before moving forward with Store A."

我之所以感到困惑是因为Javascript的并发模型只允许一次发生一件事,并确保当前操作的调用堆栈在继续任务队列之前耗尽。

所以我们可以免费获得 Facebook 和 Mozilla 所说的他们的调度员给我们的东西。我们无法避免。为什么我们谈论它就像它有什么特别之处?

好吧...另一个考虑因素是,如果您的 "action" 通过回调执行一些异步操作,例如:

  1. 更新状态,
  2. 启动 XHR,
  3. 用结果更新状态。

在这里你的动作可以分成两部分,在 2 和 3 之间可能会发生一些事情,因此违反了 "one action at a time" 原则。

这个问题只用术语就可以解决:

  1. 将动作 A 定义为更新状态并发送 XHR 的东西。
  2. XHR 的结果触发更新状态的操作 B。

毕竟脸书都说了"Actions may also come from other places, such as the server."

因此,只要稍微改变一下术语,我们就不需要一个调度程序来执行除将事件调度到感兴趣的商店之外的任何事情。

那我错过了什么?为什么 Flux 中的调度程序必须做的不仅仅是调度?

我的回答是:编写这些文档的人不清楚他们在说什么。

即使是顶级的 Whosebug React 回答者和 React 贡献者也说:

In my understanding, asynchronous actions that rely on Ajax, etc. shouldn't block the action from being dispatched to all subscribers.

You'll have a separate action for the user action, like TODO_UPDATE_TEXT in the TodoMVC example and one that gets called when the server returns, something like TODO_UPDATE_TEXT_COMPLETED (or maybe just something more generic like TODO_UPDATE_COMPLETED that contains a new copy of the latest attributes).

参见: