I'd like to know if this approach is possible for a starting event in a card game app ( asking about the dispatchs in :fx ) :
(re-frame/reg-event-fx
::start-game
(fn [{:keys [db]} [_ match]]
{:db (assoc db :started? true)
:fx [[:dispatch [::prepare-cards]]
[:dispatch [::spread-cards]]
[:dispatch [::get-trump]]
[:dispatch [::set-player-turn match]]]}))What result do you imagine getting, or not getting?
Just call these four other events .
Honestly, I want to know if it is a good approach (or pattern) .
It might be a matter of taste. I like composing events this way, sometimes. But if another dev tried to read your project, it might feel more frictional. There's no guarantee that a race won't happen among the effects of subsequent events.
The :db effect won't cause a race. But if if that's the only effect, meaning your 4 events are just updating the app-db, then there might be no justification for making 4 separate event handlers. you could just compose 4 helper functions within a single event.
If you do have complicated effects that need to be synchronized, consider using something like re-frame-async-flow-fx
If a function can be used instead of an event, it should probably be used there. :) Otherwise, it's easy to code yourself into a corner and have fun times debugging race conditions.
Got it !
{:db (-> db start prepare-cards spread-cards get-trump (set-player-turn match))}
Or, as a commonly discussed pattern here, pass around not db but the full effects map.
The only problem with such a solution is that if an event requires a coeffect, you can't really make it into a plain function without making its users know about its needs, somehow.
A more involved alternative might be to create a function that retrieves both event handlers and registered interceptors based on event ID, and then combines that all to create a new event handler.
But that's not trivial, can't be done without relying on impl details of re-frame, and isn't needed anyway in the vast majority of cases.
Thanks the for suggestions ! I could decrease the number of event handles from 16 to 5 .
> Or, as a commonly discussed pattern here, pass around not db but the full effects map.
I have a small library for helping with that.
https://github.com/green-coder/mate/blob/made-in-taiwan/test/mate/re_frame_test.cljc#L70-L90