在最后一个调度链完成后调度
Dispatch after last dispatch chain finishes
发货单应该是什么样的:
;; Initial Data
(dispatch [:http/get-bar])
;; We click something to update foo
(dispatch [:http/update-foo])
;; :success handler gets run:
(dispatch [:http-success/update-foo])
;; Foo data influences bar data, we want to get-bar again after foo updates
(dispatch [:http/get-bar])
如果我们有这样的事情:
{:on-click
(fn []
(dispatch [:http/update-foo])
(dispatch [:http/get-bar]))}
订单实际上看起来像:
[[:http/get-bar]
[:http/update-foo]
[:http/get-bar]
[:http-success/update-foo]]
我们无法保证在再次获取bar之前更新成功。
可以将 :http/get-bar
作为 :http-success/update-foo
的一部分进行分派,
但是硬编码使事情变得不那么灵活。在我的特定用例中,我有一个
在两个不同页面上使用的模态组件。单击保存时,两者
将发送到 [:http/update-foo]
但一页将跟进
[:http/get-bar]
另一个将跟进 [:http/get-baz]
,
两者都需要 foo
先完成更新。
这对我来说听起来像是可以用 re-frame-async-flow-fx 解决的问题。
您的代码可能如下所示:
(defn modal-flow [dispatch-after]
{:first-dispatch [:http/update-foo]
:rules [
{:when :seen? :events :http/update-foo-success :dispatch [dispatch-after]}
]})
(re-frame/reg-event-fx
:modal-1
(fn [_ _]
{:async (modal-flow :get/update-foo)}))
(re-frame/reg-event-fx
:modal-2
(fn [_ _]
{:async (modal-flow :get/update-baz)}))
异步流程对于构建这些类型的依赖关系非常强大,让您可以让您的个人处理程序摆脱硬编码(或笨拙的参数化)dispatch-after 值。
发货单应该是什么样的:
;; Initial Data
(dispatch [:http/get-bar])
;; We click something to update foo
(dispatch [:http/update-foo])
;; :success handler gets run:
(dispatch [:http-success/update-foo])
;; Foo data influences bar data, we want to get-bar again after foo updates
(dispatch [:http/get-bar])
如果我们有这样的事情:
{:on-click
(fn []
(dispatch [:http/update-foo])
(dispatch [:http/get-bar]))}
订单实际上看起来像:
[[:http/get-bar]
[:http/update-foo]
[:http/get-bar]
[:http-success/update-foo]]
我们无法保证在再次获取bar之前更新成功。
可以将 :http/get-bar
作为 :http-success/update-foo
的一部分进行分派,
但是硬编码使事情变得不那么灵活。在我的特定用例中,我有一个
在两个不同页面上使用的模态组件。单击保存时,两者
将发送到 [:http/update-foo]
但一页将跟进
[:http/get-bar]
另一个将跟进 [:http/get-baz]
,
两者都需要 foo
先完成更新。
这对我来说听起来像是可以用 re-frame-async-flow-fx 解决的问题。
您的代码可能如下所示:
(defn modal-flow [dispatch-after]
{:first-dispatch [:http/update-foo]
:rules [
{:when :seen? :events :http/update-foo-success :dispatch [dispatch-after]}
]})
(re-frame/reg-event-fx
:modal-1
(fn [_ _]
{:async (modal-flow :get/update-foo)}))
(re-frame/reg-event-fx
:modal-2
(fn [_ _]
{:async (modal-flow :get/update-baz)}))
异步流程对于构建这些类型的依赖关系非常强大,让您可以让您的个人处理程序摆脱硬编码(或笨拙的参数化)dispatch-after 值。