通量存储或操作(或两者)应该接触外部服务吗? [英] Should flux stores, or actions (or both) touch external services?

查看:50
本文介绍了通量存储或操作(或两者)应该接触外部服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

商店是否应该维护自己的状态并有能力在这样做时调用网络和数据存储服务...在这种情况下,操作只是愚蠢的消息传递者,

Should the stores maintain their own state and have the ability to call network and data storage services in doing so ...in which case the actions are just dumb message passers,

-或-

...商店应该是来自动作的不可变数据的愚蠢接收者(并且动作是在外部源之间获取/发送数据的动作?在这种情况下,商店将充当视图模型并且能够聚合/在根据操作提供给他们的不可变数据设置自己的状态之前过滤他们的数据.

...should the stores be dumb recipients of immutable data from the actions (and the actions be the ones that fetch/send data between external sources? Store in this instance would act as view-models and would be able to aggregate / filter their data prior to setting their own state base on the immutable data they were fed by the action.

在我看来,它应该是一个或另一个(而不是两者的混合).如果是这样,为什么首选/推荐一个而不是另一个?

It seems to me that it should be one or the other (rather than a mix of both). If so, why is one preferred / recommended over the other?

推荐答案

我已经看到通量模式以两种方式实现,并且在我自己完成后(最初采用前一种方法),我相信存储应该是来自动作的数据的愚蠢接收者,并且写入的异步处理应该住在行动创造者.(异步读取的处理方式不同.) 根据我的经验,这有一些好处,按重要性排序:

I've seen the flux pattern implemented both ways, and after having done both myself (initially going with the former approach), I believe that stores should be dumb recipients of data from the actions, and that asynchronous processing of writes should live in the action creators. (Async reads can be handled differently.) In my experience, this has a few benefits, in order of importance:

  1. 您的商店变得完全同步.这使您的商店逻辑更易于遵循且非常易于测试 — 只需实例化具有某些给定状态的商店,向其发送操作并检查查看状态是否按预期更改.此外,flux 的核心概念之一是防止级联调度和防止一次多次调度;当您的商店进行异步处理时,这很难做到.

  1. Your stores become completely synchronous. This makes your store logic much easier to follow and very easy to test—just instantiate a store with some given state, send it an action, and check to see if the state changed as expected. Furthermore, one of the core concepts in flux is to prevent cascading dispatches and to prevent multiple dispatches at once; this is very difficult to do when your stores do asynchronous processing.

所有动作分派都发生在动作创建者身上.如果您在商店中处理异步操作并且希望保持商店的动作处理程序同步(并且您应该为了获得通量单次调度保证),您的商店将需要触发额外的 SUCCESS 和 FAIL 操作以响应异步处理.将这些调度放在动作创建者中有助于分离动作创建者和商店的工作;此外,您不必深入挖掘您的商店逻辑来找出操作是从哪里分派的.在这种情况下,典型的异步操作可能如下所示(根据您使用的通量风格更改 dispatch 调用的语法):

All action dispatches happen from the action creators. If you handle asynchronous operations in your stores and you want to keep your stores' action handlers synchronous (and you should in order to get the flux single-dispatch guarantees), your stores will need to fire additional SUCCESS and FAIL actions in response to asynchronous processing. Putting these dispatches in the action creators instead helps separate the jobs of the action creators and the stores; furthermore, you don't have to go digging through your store logic to figure out where actions are being dispatched from. A typical asynchronous action in this case might look something like this (change the syntax of the dispatch calls based on the flavor of flux you're using):

someActionCreator: function(userId) {
  // Dispatch an action now so that stores that want
  // to optimistically update their state can do so.
  dispatch("SOME_ACTION", {userId: userId});

  // This example uses promises, but you can use Node-style
  // callbacks or whatever you want for error handling.
  SomeDataAccessLayer.doSomething(userId)
  .then(function(newData) {
    // Stores that optimistically updated may not do anything
    // with a "SUCCESS" action, but you might e.g. stop showing
    // a loading indicator, etc.
    dispatch("SOME_ACTION_SUCCESS", {userId: userId, newData: newData});
  }, function(error) {
    // Stores can roll back by watching for the error case.
    dispatch("SOME_ACTION_FAIL", {userId: userId, error: error});
  });
}

否则可能会在各种操作中重复的逻辑应提取到单独的模块中;在本例中,该模块将是 SomeDataAccessLayer,它处理执行实际的 Ajax 请求.

Logic that may otherwise be duplicated across various actions should be extracted into a separate module; in this example, that module would be SomeDataAccessLayer, which handles doing the actual Ajax request.

您需要更少的动作创建者.这没什么大不了的,但很高兴拥有.正如#2 中提到的,如果你的商店有同步动作分派处理(他们应该),你需要触发额外的动作来处理异步操作的结果.在动作创建器中进行调度意味着单个动作创建器可以通过处理异步数据访问本身的结果来调度所有三种动作类型.

You need less action creators. This is less of a big deal, but nice to have. As mentioned in #2, if your stores have synchronous action dispatch handling (and they should), you'll need to fire extra actions to handle the results of asynchronous operations. Doing the dispatches in the action creators means that a single action creator can dispatch all three action types by handling the result of the asynchronous data access itself.

这篇关于通量存储或操作(或两者)应该接触外部服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆