Fork me on GitHub
#re-frame
<
2017-10-09
>
kirang07:10:21

I have a query about dispatching events on app boot: I create a new timer which fires an effect :set-clock that keep increasing the duration of the timer and stores the new value to reframe-db. This in turn renders the timer component to display the timer ticking in the UI. If I were to load the stored duration data directly from local-storage on page refresh, how should I start ticking the timers automatically? Each timer has a :state which tells us if the timer is running or paused.

jvuillermet21:10:49

I probably misunderstood the problem but can you not dispatch the event with the set-clock effect after datas has been loaded from localstorage ?

kirang06:10:52

Since I load the db data from the localstore by injecting it into coeffects at :initialize-db, how would I trigger a set-clock event after the data has been loaded ?

jvuillermet08:10:55

You should be able to do something using the effect map and the :dispatch key to dispatch a new event

kirang06:10:58

I ended up writing doing an inject-cofx on :initialize to inject the db state in local storage into the coeffects map.

kirang06:10:44

Thank you for responding! 🙂

deg14:10:04

What libraries exist for input text completion in re-frame?

Bravi23:10:57

hey everyone, can I get some recommendations about what I might improve / write a better approach in here please?

(ns colors-game.events
  (:require [re-frame.core :as re-frame]
            [colors-game.db :as db]))

(re-frame/reg-event-db
 :initialize-db
 (fn  [_ _]
   db/default-db))

(defn random-active-color [db]
  (->>
    (:colors db)
    (take (:active db))
    (rand-nth)))

(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)

(re-frame/reg-event-db
  :end-game
  (fn [db]
    (assoc db :has-ended? true)))

(re-frame/reg-event-db
  :tick
  (fn [db]
    (if (> (:time-left db) 1)
      (update db :time-left dec)
      (assoc db :has-ended? true))))

(re-frame/reg-event-db
  :select-color
  (fn [db [_ color]]
    (if-not (:has-ended? db)
      (if (= (:real db) color)
        (-> db
            (update :score inc)
            (assoc :time-left 3
                   :fake (random-active-color db)
                   :real (random-active-color db)))
        (assoc db :has-ended? true
               :time-left 0))
      db)))

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

mikethompson23:10:30

@bravilogy is there a specific problem you would like us to help you with?

Bravi23:10:38

I just wanted to know if it’s a good approach in re-frame to have those nested if / if-not logics in there

Bravi23:10:50

or perhaps there’s a different way to do it

Bravi23:10:59

something that I don’t know yet and I would look into

Bravi23:10:04

just trying to learn really

mikethompson23:10:57

@bravilogy seems reasonable to me.

mikethompson23:10:33

@deg re-com has a widget like that