Search code examples
clojureclojurescriptreagentre-frame

How to dispatch an event from an event in re-frame


I followed this example: https://github.com/Day8/re-frame/blob/master/docs/FAQs/PollADatabaseEvery60.md

And here is my interval handler

(defonce interval-handler
  (fn [{:keys [action id frequency event]}]
    (let [live-intervals (atom {})]
      (condp = action
        :start (swap! live-intervals assoc id (js/setInterval #(re-frame/dispatch event) frequency))
        :end (do (js/clearInterval (get live-intervals id))
                 (swap! live-intervals dissoc id))))))

(re-frame/reg-fx
  :interval
  interval-handler)

I'm trying to dispatch this interval event from another event right here:

(re-frame/reg-event-db
  :start-playing
  (fn [db _]
    (re-frame/dispatch [:interval {:action :start
                       :id :some-awesome-id
                       :frequency 1000
                       :event [:tick]}])
    (assoc db :is-playing? true
           :fake (random-active-color db)
           :real (random-active-color db))))

but it says re-frame: no :event handler registered for: :interval

Is this not possible to do?


Solution

  • :interval is an effect, not an event. To invoke an effect, you need to include it as a key in the effects map returned by your event handler - not emit another event with the effect's key:

    (re-frame/reg-event-fx
      :start-playing
      (fn [{:keys [db]} _]
        {:interval {:action :start
                    :id :some-awesome-id
                    :event [:tick]}]
         :db (assoc db :is-playing? true
                       :fake (random-active-color db)
                       :real (random-active-color db))}))
    

    Above event handler will return a map describing two effects:

    • :db - updating the app db to a new value (provided as the :db value)

    • :interval - re-frame will call your effect handler (interval-handler) with the value of :interval entry in the effects map