This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-09
Channels
- # aleph (16)
- # bangalore-clj (1)
- # beginners (57)
- # cider (4)
- # clara (1)
- # cljs-dev (25)
- # cljsrn (12)
- # clojure (76)
- # clojure-dusseldorf (2)
- # clojure-italy (41)
- # clojure-russia (4)
- # clojure-spec (3)
- # clojure-uk (122)
- # clojurescript (101)
- # cursive (8)
- # data-science (30)
- # datomic (2)
- # emacs (2)
- # figwheel (10)
- # fulcro (53)
- # garden (5)
- # gorilla (6)
- # hoplon (1)
- # jobs (1)
- # juxt (14)
- # leiningen (12)
- # om (1)
- # om-next (1)
- # onyx (21)
- # pedestal (40)
- # perun (5)
- # portkey (2)
- # re-frame (16)
- # reagent (1)
- # ring-swagger (3)
- # rum (6)
- # shadow-cljs (239)
- # spacemacs (10)
- # specter (9)
- # uncomplicate (2)
- # unrepl (1)
- # vim (13)
- # yada (16)
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.
I probably misunderstood the problem but can you not dispatch the event with the set-clock effect after datas has been loaded from localstorage ?
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 ?
You should be able to do something using the effect map and the :dispatch key to dispatch a new event
I ended up writing doing an inject-cofx
on :initialize
to inject the db state in local storage into the coeffects map.
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))}))
@bravilogy is there a specific problem you would like us to help you with?
I just wanted to know if it’s a good approach in re-frame to have those nested if / if-not logics in there
@bravilogy seems reasonable to me.
@deg re-com has a widget like that