Simplify :dispatch-later and only support single :dispatch

This commit is contained in:
hipitihop 2016-07-27 16:03:29 +10:00
parent 12f6eeee3c
commit e884dcadb6
2 changed files with 8 additions and 13 deletions

View File

@ -47,24 +47,18 @@
;; Dispatch event(s) after some time.
;; Example:
;; {:dispatch-later [{:ms 200 :dispatch [:event-id "param"]} ;; in 200ms do this: (dispatch [:event-id "param"])
;; {:ms 100 :dispatch [:also :this :in :100ms]}
;; {:ms 250 :dispatch-n (list [:do ] [:all ] [:three ])}]}
;; {:ms 100 :dispatch [:also :this :in :100ms]}]}
;;
(register
:dispatch-later
(fn [effects-v]
;TODO: use Spec to verify vector and elements when clj 1.9.0 is rel.
(doseq [effect effects-v]
(let [{:keys [ms dispatch dispatch-n]} effect
tasks (cond dispatch (list dispatch)
dispatch-n dispatch-n
:else (console :error "re-frame: dispatch-later fx must have one of :dispatch or :dispatch-n"))]
(when (and (some? dispatch) (some? dispatch-n))
(console :warn "re-frame: dispatch-later fx must have one of :dispatch or :dispatch-n Got: " effect " Ignoring: :dispatch-n"))
(if (or (empty? tasks) (-> ms number? not))
(console :warn "re-frame: dispatch-later fx :ms not number or missing :dispatch/:dispatch-n Got:" effect " Ignored.")
(set-timeout! #(doseq [event tasks]
(router/dispatch event)) ms))))))
(let [ms (:ms effect)
event (:dispatch effect)]
(if (or (empty? event) (-> ms number? not))
(console :warn "re-frame: dispatch-later fx :ms not number or missing :dispatch Got:" effect " Ignored.")
(set-timeout! #(router/dispatch event) ms))))))
;; Supply a vector. For example:

View File

@ -29,7 +29,8 @@
(swap! seen-events #(conj % token))
db))
{:dispatch-later [{:ms 100 :dispatch [::watcher :event1]}
{:ms 200 :dispatch-n '([::watcher :event2] [::watcher :event3])}]}))
{:ms 200 :dispatch [::watcher :event2]}
{:ms 200 :dispatch [::watcher :event3]}]}))
(async done
(set-timeout!