This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-12-27
Channels
- # adventofcode (7)
- # announcements (31)
- # babashka (15)
- # beginners (14)
- # calva (45)
- # circleci (6)
- # clojure (27)
- # clojure-europe (19)
- # clojure-france (2)
- # clojure-gamedev (4)
- # clojure-uk (2)
- # clojurescript (26)
- # conjure (14)
- # data-science (6)
- # deps-new (7)
- # depstar (4)
- # emacs (13)
- # events (1)
- # fulcro (20)
- # graalvm (2)
- # hoplon (30)
- # joker (11)
- # london-clojurians (1)
- # malli (26)
- # pathom (2)
- # re-frame (13)
- # reagent (8)
- # reclojure (3)
- # reveal (8)
- # robots (4)
- # shadow-cljs (29)
- # sql (5)
- # tools-deps (28)
- # vim (4)
Does anybody else find themselves wanting a :dispatch-many
sort of fx? I often want to dispatch multiple effects in response to a single event, and I don't want to couple them all together in a chain.
Something like this:
(defn dispatch-many
[events]
(doseq [event events]
(rf/dispatch event)))
(rf/reg-fx ::dispatch-many dispatch-many)
(rf/reg-event-fx
::dispatch-many
[rf/debug]
(fn [_ [_ & dispatches]]
{::dispatch-many dispatches}))
The fx already exists, and it's called :dispatch-n
.
But combining multiple events into one by wrapping them is definitely a code smell. Move such combinations into a separate event with a specific name.
Cool that it already exists! I'll have to compare implementations. Could you elaborate more on the smell? I often have multiple side effects that I want to trigger, all independent of each other. It seems to me I either dispatch them all in response to the trigger event, or I chain them all together where one calls the next. I'm wary of coupling them in that sort of artificial order.
Here's an example of how I'm using it now: once a user has authenticated, I need to fetch some more stuff and take care of some state management stuff.
(rf/dispatch [::util/dispatch-many
(auth/set-auth (:auth-success state))
(app.evt/get-capabilities)
(app.evt/sync)
(form.evt/destroy-form app.const/authentication-form-id)])
> Could you elaborate more on the smell? Re-frame documentation does it much better than I ever could.
> I'm wary of coupling them in that sort of artificial order. Combining them in a new event handler does not couple them in any way.
An intent should be expressed with a single dispatch with a relevant ID and handler. "Dispatch many" is not the intent in your example above. The intent is to finalize the process of user authentication, or something like that.
Ah, I see what you're saying. The event dispatched should be semantically interesting and describable, and you can dispatch as much as you want from inside the event handler.
Also, I went to the docs and found this:
From v1.1.0 onwards, this effect is deprecated in favour of using :fx with multiple :dispatch tuples.
There's a whole bunch of goodies there that are new in 1.1.0, so I'm glad you pointed me there
The only time I write a dispatch many effect is when I use the https://github.com/day8/re-frame-async-flow-fx library and I want to start multiple initial events at the beginning of the flow