If I had my time over again, I would make reg-event-fx return a seq of effects, rather than a map of effects. The resulting effects DSL would be more expressive, at the cost of some nesting.
I have previously teased out the issues a bit here:
https://gist.github.com/mike-thompson-day8/1388e68391f4c6c1373c9555c08c7114#using-data-litterals
Advantages:
- It gives a way to order effects (not that useful, but an interesting property)
- no need for
dispatch-n , just use :dispatch multiple times (this same -n issues comes up a lot with other effects like http GETs etc)
Disadvantages:
- slightly noisier, visually, with the additional structural nesting
- is a breaking change
It might be possible to work around the breaking change problem by introducing a new reg-event-fx2 which expects the alternatively structured effects return. Allowing us to leave the old reg-event-fx alone. But it isn't all sunshine and roses - we'd end up with interceptors which worked with one kind of effects, but not the other. So then there would be v1 interceptors and v2 interceptors. One worked with one kind of effect but not the other.
@olivergeorge has made an interesting suggestion which fixes the "breaking change" side of things, at the expense of a tiny bit of boilerplate. He suggests introducing a new :fx effect for use with reg-event-fx which can take seq of other effects.
(reg-event-fx
:token
(fn [{:keys [db]} [_ id]]
{:db (assoc db :something id)
:fx [{:dispatch [:blah]} ;; <-- :fx takes a seq of effects
{:dispatch [:other]}
{:http {:url "http://abc.com/endpont :method "GET"}}]}))
Notice the new :fx effect for which a seq of other effects is provided.
Question: if done this way, what is the ordering between :db and those in :fx? :db first? Probably.
Question: I regard :db to be a normal effect (it is just a side effect on app-db), so it could be put into :fx too. (What happens if it is not, what is the ordering?)
(reg-event-fx
:token
(fn [{:keys [db]} [_ id]]
{:fx [{:db (assoc db :something id)}
{:dispatch [:blah]}
{:dispatch [:other]}
{:http {:url "http://abc.com/endpont :method "GET"}}]}))
Question: maybe to reduce noise, just pairs like this
(reg-event-fx
:token
(fn [{:keys [db]} [_ id]]
{:fx [:db (assoc db :something id) ;; <-- not a map, just a pair
:dispatch [:blah]
:dispatch [:other]
:http {:url "http://abc.com/endpont :method "GET"}]}))
At the moment, my inclination is this arrangement (which is slightly hybrid):
(reg-event-fx
:token
(fn [{:keys [db]} [_ id]]
{:db (assoc db :something id) ;; <-- keep :db seperate
:fx [:dispatch [:blah] ;; <-- use pairs
:dispatch [:other]
:http {:url "http://abc.com/endpont :method "GET"}]}))
Conditional Exclusion
(reg-event-fx
:token
(fn [{:keys [db]} [_ id]]
{:db (assoc db :something id)
:fx [{:dispatch [:blah]}
(when cond? {:dispatch [:other]}) ;; <-- conditionally exclude this effect. nils ignored?
{:http {:url "http://abc.com/endpont :method "GET"}}]}))
Or if pairs are used ....
(reg-event-fx
:token
(fn [{:keys [db]} [_ id]]
{:db (assoc db :something id)
:fx [:dispatch [:blah]
:dispatch (when cond? [:other]) ;; <-- conditionally exclude this effect. nils ignored?
:http {:url "http://abc.com/endpont :method "GET"}]}))
As you can see, If paris are used, then It isn't neat to conditionally omit both. Only the value part of the pair could be made nil. This could be a bit clumsy because certain effects actually have a value nil value. Eg; :go-fullscreen
If I had my time over again, I would make
reg-event-fxreturn a seq of effects, rather than a map of effects. The resulting effects DSL would be more expressive, at the cost of some nesting.I have previously teased out the issues a bit here:
https://gist.github.com/mike-thompson-day8/1388e68391f4c6c1373c9555c08c7114#using-data-litterals
Advantages:
dispatch-n, just use:dispatchmultiple times (this same-nissues comes up a lot with other effects like http GETs etc)Disadvantages:
It might be possible to work around the breaking change problem by introducing a new
reg-event-fx2which expects the alternatively structured effects return. Allowing us to leave the oldreg-event-fxalone. But it isn't all sunshine and roses - we'd end up with interceptors which worked with one kind of effects, but not the other. So then there would be v1 interceptors and v2 interceptors. One worked with one kind of effect but not the other.@olivergeorge has made an interesting suggestion which fixes the "breaking change" side of things, at the expense of a tiny bit of boilerplate. He suggests introducing a new
:fxeffect for use withreg-event-fxwhich can take seq of other effects.Notice the new
:fxeffect for which aseqof other effects is provided.Question: if done this way, what is the ordering between
:dband those in:fx?:dbfirst? Probably.Question: I regard
:dbto be a normal effect (it is just a side effect onapp-db), so it could be put into:fxtoo. (What happens if it is not, what is the ordering?)Question: maybe to reduce noise, just pairs like this
At the moment, my inclination is this arrangement (which is slightly hybrid):
Conditional Exclusion
Or if pairs are used ....
As you can see, If paris are used, then It isn't neat to conditionally omit both. Only the
valuepart of the pair could be madenil. This could be a bit clumsy because certain effects actually have a valuenilvalue. Eg;:go-fullscreen