This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-15
Channels
- # beginners (34)
- # boot (45)
- # cider (16)
- # cljs-dev (20)
- # cljsjs (1)
- # cljsrn (8)
- # clojure (207)
- # clojure-berlin (3)
- # clojure-dev (18)
- # clojure-greece (1)
- # clojure-ireland (1)
- # clojure-italy (9)
- # clojure-russia (20)
- # clojure-spec (27)
- # clojure-uk (19)
- # clojurescript (110)
- # code-reviews (2)
- # cursive (7)
- # data-science (2)
- # datomic (7)
- # devcards (1)
- # emacs (4)
- # graphql (1)
- # hoplon (2)
- # immutant (15)
- # jobs (5)
- # jobs-rus (1)
- # juxt (1)
- # luminus (7)
- # lumo (26)
- # microservices (3)
- # off-topic (27)
- # om (13)
- # onyx (11)
- # pedestal (7)
- # proton (4)
- # re-frame (24)
- # remote-jobs (1)
- # spacemacs (2)
- # specter (2)
- # unrepl (31)
- # untangled (7)
- # vim (14)
I've got some subscriptions that take a lot of CPU time. Is there an easy way to memoize them within re-frame?
@ksmithbaylor can you explain a bit more what you are trying to do? The subscription won't signal a change if the data in the app-db that it is pulling from is not updated by something. When it does change, you would need to re-run the function anyway and would lose the benefits of memoize.
I've got an app-db with (among other things), something like this:
{:page :home
:raw-data { ... }} ; deeply-nested map deserialized from a large JSON file on disk
I have a :navigate
event that is fired when the user switches which page they are viewing (`:home`, :preferences
, etc)
Some of the raw data is "inactive" and should not be considered in my app, so I have a function called active-data
that takes in the raw data and returns just the relevant set of data I care about.
Maybe make a sub for :active-data
and register all your subs with (reg-sub :foo :<- [:active-data] (fn [active-data] ....)
I set up the following subscriptions:
(rf/reg-sub :raw-data
#(:raw-data %))
(rf/ref-sub :active-data
:<- [:raw-data]
(fn [data _]
(active-data data)))
And whenever I :navigate
between pages, it is re-calling my active-data
function even though only the :page
key has changed in app-db.
Does that make sense?
Yeah, I have other subs that depend on :<- [:active-data]
.
It does seems strange that it's calling active-data
when only :page
changes
Yeah. My solution right now is to pre-compute :active-data
and insert it in the app-db along with :raw-data
, since that only happens once when the app boots. But it would certainly be cleaner to not need to store :active-data
in the app-db and instead make it just a view of :raw-data
.
I sort of see subscriptions as views on top of db tables, and what I'm doing is sort of like a materialized view.
(minus the tables, of course :))
that makes sense
what if you just compute active-data
and discard raw-data
?
I agree that it seems like it would be good to have subscriptions as "views" of the "raw-data" and not store "active-data"
I could probably do that, my hoarding tendencies tell me I might need it at some point, but I think most of my app will be built upon :active-data
.
That's probably the best solution.
Does it sound like I have something hooked up wrong, though? I thought subscriptions would only be re-calculated if their input data changed.
that's my understanding as well
I haven't dug into it much to really understand the inner workings (yet) though
Alright. If I get a chance to circle back and investigate this, I'll report here. But for now I'm going to do the calculation up-front and call it a day.