This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-05-04
Channels
- # announcements (1)
- # asami (61)
- # babashka (71)
- # beginners (170)
- # biff (1)
- # calva (14)
- # clj-kondo (23)
- # cljsrn (28)
- # clojars (1)
- # clojure (152)
- # clojure-australia (2)
- # clojure-europe (65)
- # clojure-nl (2)
- # clojure-spec (8)
- # clojure-sweden (3)
- # clojure-uk (45)
- # clojurescript (1)
- # css (12)
- # cursive (16)
- # datomic (9)
- # devcards (2)
- # emacs (1)
- # events (1)
- # graalvm (31)
- # honeysql (10)
- # jackdaw (2)
- # jobs (5)
- # lambdaisland (9)
- # lsp (4)
- # malli (11)
- # meander (43)
- # off-topic (6)
- # pathom (7)
- # polylith (1)
- # portal (14)
- # re-frame (7)
- # releases (1)
- # remote-jobs (1)
- # rewrite-clj (6)
- # shadow-cljs (101)
- # specter (1)
- # tools-deps (26)
- # vim (9)
- # xtdb (2)
I was wondering why the :dispatch-later
map doesn’t support :dispatch-n
. My use case is that I need two events happening after certain ms. I have many ways to achieve the same behaviour but thought that a dispatch-n
within dispatch-later
was the cleanest way.
:dispatch-later
and :dispatch-n
are effects that accept events. That's why.
With that being said, :dispatch-later
accepts not only a map, but also a vector of maps - that's how you dispatch multiple events later.
In the https://day8.github.io/re-frame/Effects/#order-of-effects, there's a portion that references best practices changing and the new approach (i.e., :db and :fx) are "more about making it easier to compose event handlers." I totally agree & think this new best practice is great. I'm curious, however, is the composition of event handlers documented somewhere? I'd like to be able to link to this doc from our internal best practices doc.
There's not much to it.
You have two handler functions, that both return {:db ..., :fx ...}
, where both keys are optional. And then you just do something like:
(defn combine-fx-handlers [fns]
(fn [{db :db :as ctx} event-v]
(reduce (fn [acc f]
(let [fxs (f (assoc ctx :db (:db acc)) event-v)]
(cond-> acc
(contains? fxs :db) (assoc :db (:db fxs))
(contains? fxs :fx) (update :fx into (:fx fxs)))))
{:db db, :fx []} fns)))
I haven't tested it though. And the way you compose your handlers might differ.
Apart from that, you can have separate functions for db and fx updates - then combining them would be even easier.