This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-02-19
Channels
- # announcements (13)
- # asami (43)
- # babashka (35)
- # beginners (175)
- # calva (23)
- # cider (5)
- # clj-kondo (68)
- # cljsrn (4)
- # clojure (61)
- # clojure-australia (7)
- # clojure-europe (20)
- # clojure-gamedev (59)
- # clojure-israel (11)
- # clojure-italy (4)
- # clojure-nl (2)
- # clojure-norway (21)
- # clojure-spec (12)
- # clojure-uk (43)
- # clojurescript (9)
- # cursive (56)
- # data-oriented-programming (5)
- # datascript (1)
- # events (1)
- # fulcro (16)
- # honeysql (46)
- # leiningen (1)
- # malli (4)
- # off-topic (12)
- # pathom (46)
- # re-frame (24)
- # reagent (14)
- # reitit (1)
- # reveal (8)
- # rewrite-clj (16)
- # ring (13)
- # sci (9)
- # spacemacs (14)
- # specter (2)
- # sql (2)
- # tools-deps (1)
- # vim (2)
Is it bad practice to subscribe from inside a computation function in reg-sub
? I’m trying to figure out how to materialize relationships but I don’t have the nested data until the computation function.
@aramz maybe signal functions? https://github.com/day8/re-frame/blob/2965ffeda9b8f3b687e2c3e0ba9a62e7fe64c0bb/src/re_frame/core.cljc#L201
new blog post on re-frame and firebase: https://widdindustries.com/clojurescript-firebase-simple/ which talks about signal functions and limited use of re-frame app-db. feedback welcome 🙂
hi, in some cases i can see in traces that subscription run in each tick, even if there are no changes in app-db, why it might happen ? so it seems like subscription is broken and it just run on each tick
I don't know what your code is exactly so I cannot answer that. Perhaps that event handler is called on each tick, perhaps something else.
Alternatively, you might be right and there might be some bug. But again, I can't say anything without having an MRE.
I also often need a snack before debugging a problem 😀
Hey Guys! Happy Fried-day!
I was facing an conflict issue in my project and I'd like to have your input on the way I resolved it.
# The problem
In my project I had the tim-v
interceptor registered globally:
(re-frame/reg-global-interceptor trim-v)
This is handy because it makes the event handlers registration less verbose. For instance:
(rf/reg-event-fx
(fn [_ [prop]]
{:dispatch {::some-event prop}}))
This was working perfectly until I had to use the cool re-pressed
library.
This library registers event handlers the way one would if trim-v
wouldn't intercept the event. For instance:
(rf/reg-event-fx
(fn [_ [_ event-type]]
{::keyboard-event {:event-type event-type}}))
That's where the conflict happens, because of trim-v
the event-type
is actually passed as the first element in the second parameter of that handler and is consequently ignored.
# My solution to that problem
I wrote a custom reg-global-interceptor
that checks if the namespace of the event is within a whitelist, in which case it will apply the interceptor, otherwise it won't.
(defonce ns-whitelist #{"foo" "bar"})
(defn- event-is-whitelisted
"Returns true if the event is in the namespace white list"
[whitelist context]
(contains? whitelist (-> context
(get-coeffect :original-event)
first
namespace
(split #"[\./]")
first)))
(defn- get-whitelisted-int
"Executes the interceptor only if the scope falls in the whitelist"
[whitelist interceptor]
(let [{:keys [id before after]} interceptor
whitelisted (partial event-is-whitelisted whitelist)]
(->interceptor
:id id
:before #(if (and (some? before) (whitelisted %)) (before %) %)
:after #(if (and (some? after) (whitelisted %)) (after %) %))))
(defn- reg-global-interceptor
"Delimits the scope of the interceptor"
[interceptor]
(re-frame/reg-global-interceptor (get-whitelisted-int ns-whitelist interceptor)))
# Some thoughts
This adds a good amount of additional computing and I am not sure how to solve that problem in a more efficient way.
Thanks in advance for your help
please answer in a threadIn this code block:
(rf/reg-event-fx
(fn [_ [_ event-type]]
{::keyboard-event {:event-type event-type}}))
why not replace [_ event-type]
with just [event-type]
?Because this code is from re-pressed
which is an external project/dependency:
https://github.com/gadfly361/re-pressed
I see.
In general, I don't think that it's a good idea to use event-modifying interceptors globally.
In my projects, I have a custom versions of reg-event-*
functions that inject that interceptor automatically. kee-frame
does it as well, for example.
Good point! This means that my problem is due to a bad pattern/design. Maybe it'd be good to mention exactly what you just wrote in re-frame
's documentation about interceptors? :)
But I would steal those lovely words from you 😄 I'll try, thank you @U2FRKM4TW!!