This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-27
Channels
- # aleph (1)
- # announcements (39)
- # aws (11)
- # beginners (58)
- # calva (10)
- # cider (7)
- # clj-kondo (65)
- # cljs-dev (5)
- # clojure (90)
- # clojure-dev (48)
- # clojure-europe (23)
- # clojure-madison (1)
- # clojure-norway (1)
- # clojure-uk (40)
- # clojured (11)
- # clojurescript (20)
- # conjure (12)
- # core-async (4)
- # core-logic (4)
- # cursive (3)
- # datalevin (1)
- # emacs (7)
- # events (2)
- # fulcro (48)
- # introduce-yourself (2)
- # lsp (36)
- # malli (11)
- # missionary (1)
- # off-topic (1)
- # other-languages (72)
- # pathom (4)
- # polylith (13)
- # portal (94)
- # re-frame (14)
- # react (5)
- # releases (1)
- # sci (12)
- # shadow-cljs (29)
- # spacemacs (3)
- # vim (4)
- # xtdb (12)
Hello What is the idiomatic way to get the current date/time on a subscription? :thinking_face: On an event it would be a coeffect, but what is its counterpart on subscriptions?
There's no counterpart because subscriptions react to the changes in app-db, and current date/time has nothing to do with app-db. A common solution is to set up a timer with the desired granularity in some effect, fire off that effect in some initializing event, and make that timer write the current date/time under some path in app-db. Then you'll be able to use that value in subscriptions.
You could setup a timer in a new reg-sub-raw
subscription and wire it up in a form-3 subscription. Something like this (I didn't test it)
(rf/reg-sub-raw
::now
(fn [_ [_ frequency-ms]]
(let [now (r/atom (js/Date.))
interval (js/setInterval #(reset! now (js/Date.)) frequency-ms)]
(reagent.ratom/make-reaction
#(deref now)
:on-dispose #(js/clearInterval interval)))))
It will re-render your subscription, and whatever views subscribe to it every frequency-ms
, so be careful
Note however that such an approach has been explicitly described as not recommended in re-frame docs.
Yep, I read https://github.com/day8/re-frame/blob/master/docs/Subscribing-To-External-Data.md but I was left with doubts on how to solve it without leaving the re-frame recommendations. I don’t really need date/time to be reactive, so maybe I will ended up with:
(rf/reg-sub
::valid?
:<- [::deadline]
(fn [deadline _]
(< (time/now) deadline)))
But I was looking for some way to make that function pureThe simplest way to achieve that would be to pass now
from a view.
As far as I'm aware, the next simplest way that's still within the framework of re-frame recommendations is the approach I described above.
Going through the documentation -it’s not entirely clear what the difference between reg-fx
and reg-event-fx
Is it just that the handler within reg-fx
does not implicitly receive the cofx
map - but rather whatever values you hand to it?
While reg-event-fx
takes cofx
no matter what followed by the event?
I may have just rubber ducked myself but would appreciate confirmation lol
In reg-event-fx
handlers you are expected to return a map with keys of effects
. Each of those keys corresponds to a handler registered with reg-fx
I see.
:db new-db-value
That explains the explanation that :db is an effect registered by re-frame out of the box
Yep, reg-event-fx
declares a new event, that is supposed to return a map of effects (that are declared with reg-fx
)
Yep. Here is where the db effect is registered: https://github.com/day8/re-frame/blob/69cf39552715fa410e7007b7fcbc894097d8db1f/src/re_frame/fx.cljc#L181-L185, there are others in that namespace too.