re-frame

https://github.com/Day8/re-frame/blob/master/docs/README.md https://github.com/Day8/re-frame/blob/master/docs/External-Resources.md
2020-12-27T05:48:29.029Z

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.

2020-12-27T05:48:37.029300Z

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}))

2020-12-27T05:48:50.029600Z

Is this a bad smell, or does everyone do this?

tobias 2020-12-29T07:41:16.033Z

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

p-himik 2020-12-27T05:51:59.029700Z

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.

2020-12-27T05:55:08.029900Z

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.

2020-12-27T05:55:55.030100Z

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)])

p-himik 2020-12-27T06:00:13.030400Z

> Could you elaborate more on the smell? Re-frame documentation does it much better than I ever could.

p-himik 2020-12-27T06:00:53.030600Z

> 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.

p-himik 2020-12-27T06:02:09.030800Z

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.

2020-12-27T06:06:53.031Z

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.

2020-12-27T06:07:07.031200Z

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.

2020-12-27T06:07:32.031400Z

There's a whole bunch of goodies there that are new in 1.1.0, so I'm glad you pointed me there

👍 1