3 ms·
SOmething like this: (rf/reg-event-fx :stop-timer (fn [{:keys [db]} _] (let [handle (db :ticker-handle)] {:db (a
by LandR 4y ago
SOmething like this:
(rf/reg-event-fx
:stop-timer
(fn [{:keys [db]} _]
(let [handle (db :ticker-handle)]
{:db (assoc db :ticker-handle nil)
:stop-ticker [handle]})))
The side effect here is to a stop a running ticker on the page, but the `reg-event-fx` function just returns a map (data) where the first key is :db (which is the new app state, which is like a db, can even have a sorta schema over it) and the second key is the actual even :stop-ticker, which takes an argument handle (the handle of the ticker to stop).
The event handler is just describing the side effect that is to occur by returning the map.
- dmn322 4y agook so would it be correct to say this is sort of like handling an event that gets emitted? Sorry, I know no clojurescript, so I'm having a hard time parsing it even googling the syntax
- LandR 4y agoYes, exactly. There is a piece of code which emits the event :stop-timer, that stops a js timer in the window. :on-click (fn [_](rf/dispatch-sync [:stop-timer])) In clojure a map has the syntax {:key :value} So this event handler is returning a map which describes the event. There is then a further function which does the actual event (rf/reg-fx :stop-ticker (fn [[handle]] (when (not (nil? handle)) (js/clearInterval handle)))) BUt as far as testing goes, you can test the event handler and test that it returns the expected map. The framework is responsible for actually executing the event that is described the event handler and it does it via the name :stop-ticker. Your event handler itself can remain a pure function.