【问题标题】:RxJS Observables called in the reverse orderRxJS Observables 以相反的顺序调用
【发布时间】:2017-04-28 15:42:15
【问题描述】:

我有以下分派 3 个动作的代码:

  • deleteLineFailed
  • showConfirmationMessage
  • 等待 2s
  • hideConfirmationMessage

由于某些原因,我能够使其工作的唯一方法是按相反的顺序,我做错了什么?

const deleteLineEpic = (action$, store) =>
action$.ofType(types.DELETE_LINE_REQUEST)
    .flatMap((action) => {
        return Observable.of(hideConfirmationMessage(action.line.productID))
                .delay(2000)
                .merge(
                    Observable.of(deleteLineFailure(action.line.productID)),
                    Observable.of(showConfirmationMessage(action.line.productID))
                );
        }
    });

【问题讨论】:

    标签: redux rxjs5 redux-observable


    【解决方案1】:

    我认为会发生以下情况:

    deleteLine        -----X------------
    showConfirmation  -------Y----------
    hideConfirmation  --------- 2s -----Z
    
    merge             -----X-Y- 2s -----Z
    

    所有 3 个流被合并,具有延迟的流在两秒后发出,而另一个立即发出动作。

    所以这样做可能会更好:

    const deleteLineEpic = (action$, store) =>
    action$.ofType(types.DELETE_LINE_REQUEST)
        .flatMap((action) => {
            return Observable.merge(
                Observable.of(deleteLineFailure(action.line.productID),
                showConfirmationMessage(action.line.productID)),
                Observable.of(hideConfirmationMessage(action.line.productID))
                    .delay(2000)
            )}
    );
    

    在这里,第一个 Observable.of 中的动作会立即彼此发出,而隐藏动作会稍后发出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 2015-02-17
      相关资源
      最近更新 更多