什么是在史诗中发布行动的正确方法?

时间:2018-02-22 15:52:17

标签: javascript redux redux-observable

在redux-observable史诗中发送operationReset()的正确方法是什么?

我应该导入实际商店并使用吗?

It used to be like this, but following store is deprecated, and will be removed

// show operation failed message
(action$, store) => action$.ofType(OPERATION_FAILURE).map(() => (error({
    title: 'Operation Failed',
    message: 'Opps! It didn\'t go through.',
    action: {
        label: 'Try Again',
        autoDismiss: 0,
        callback: () => store.dispatch(operationReset())
    }
}))),

1 个答案:

答案 0 :(得分:1)

这可能会引发一个更大的问题,即如何通过回调进行通知,因为这意味着您正在发送非JSON可序列化函数作为操作的一部分。

我假设你想要仍然匹配反应通知系统。有一种方法可以使用Observable.create

来完成此操作
(action$, store) =>
  action$.pipe(
    ofType(OPERATION_FAILURE),
    mergeMap(() =>
      Observable.create(observer => {
        observer.next(
          error({
            title: "Operation Failed",
            message: "Oops! It didn't go through.",
            action: {
              label: "Try Again",
              autoDismiss: 0,
              callback: () => {
                // Send off a reset action
                observer.next(operationReset());
                // Close off this observable
                observer.complete();
              },
              // If the notification is dismissed separately (can they click an x?)
              onRemove: () => observer.complete()
            }
          })
        );
      })
    )
  );

注意:我仍然不希望将回调作为操作的一部分发送。有趣的是,我的一个项目也使用了该通知系统组件 - 我们有史诗,它将添加通知并根据操作清除它们。所有操作都保持纯粹,通知系统是受控制的副作用。

相关问题