How to dispatch an event from an event in re-frame
Asked Answered
B

1

5

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?

Bedazzle answered 9/10, 2017 at 16:13 Comment(0)
L
6

: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

Lilla answered 9/10, 2017 at 16:42 Comment(1)
You're missing db as the first arg to assoc. It won't let me edit it cause the change is too small...Diagnostician

© 2022 - 2024 — McMap. All rights reserved.