This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-28
Channels
- # announcements (1)
- # beginners (205)
- # calva (30)
- # cider (5)
- # cljdoc (25)
- # cljs-dev (2)
- # clojure (119)
- # clojure-brasil (5)
- # clojure-conj (7)
- # clojure-europe (2)
- # clojure-hamburg (7)
- # clojure-italy (14)
- # clojure-nl (2)
- # clojure-russia (13)
- # clojure-spec (79)
- # clojure-uk (58)
- # clojurescript (54)
- # core-logic (2)
- # core-matrix (2)
- # cursive (40)
- # datascript (2)
- # datomic (18)
- # duct (2)
- # emacs (14)
- # figwheel (3)
- # figwheel-main (7)
- # fulcro (30)
- # funcool (1)
- # graphql (10)
- # jobs (1)
- # juxt (13)
- # lumo (1)
- # mount (1)
- # off-topic (56)
- # other-languages (2)
- # pedestal (17)
- # powderkeg (2)
- # protorepl (2)
- # re-frame (10)
- # reagent (1)
- # reitit (7)
- # ring-swagger (10)
- # schema (2)
- # shadow-cljs (70)
- # spacemacs (13)
- # specter (4)
- # sql (9)
- # tools-deps (26)
How ok is in nowadays to create and deref a subscription for a one-time use in an event handler?
consider putting the sub's calculation in a helper function and running that over the database? or is it at the end of a complex chain of subs?
@mbertheau I think that generally it should be avoided. What is the use case? I think that some alternatives for this would be to just grab whatever value the subscription returns from the db itself - or it might be better to pass the subscription value to the event handler from the view.
(defn view
[]
(let [sub-value @(subscribe :some-sub)]
(dispatch [:some-event sub-value]))
or what Braden said:
(defn computation [db] (do-stuff db))
(reg-sub :some-sub (fn [db _] (computation db))
(reg-event :some-event (fn [db _] (let [computed-value (computation db)] ...))
I've done both of these things in the past and would actually be interested in learning which one is better đ or if there is some other even better solutionYeah well I know that subscriptions aren't (weren't?) intended to be used like that. I just wonder what the current implications of such an approach might be - memory and cpu-wise. I assume because subscriptions are cached the additional cpu load would be negligible, but maybe some memory won't be freed.
@mbertheau you could use the inject-sub
cofx from re-frame utils
https://github.com/vimsical/re-frame-utils/blob/master/src/vimsical/re_frame/cofx/inject.cljc
@danieleneal Interesting. Thanks for the pointer!
@mbertheau i find myself running into this all the time, particularly with much larger apps. often i have a very nicely defined set of subs, and i like the idea of just using a sub as a read-only view in a handler. i donât like extracting the sub into a fn and reusing it elsewhere because it introduces namespace dependencies (and is less svelte imo). i wish there was some sort of first class mechanism for this, as it just feels so powerful to say âgive me this thingâ without having to say how or where.