On Monday, May 25, 2015 at 12:13:53 AM UTC+10, Colin Yates wrote: > I am finding more and more use-cases for doing something when data changes > that aren't necessarily to do with a UI component. > > Ideally I want to do something like (outside any GUI): > > (defn my-controller-for-concern-x > (let [source (subscribe [:....])] > (do-something-when-this-changes @source))) > > and that (do-something-when-this-changes) be called whenever @source changes. > > But that feels dangerous and a possible memory leak. Am I being overly > cautious/is there a better way?
Only event handlers can change app-db, so the most natural way to achieve this is via middleware. For example, here is middleware that watches multiple parts of app-db for changes and then does "something" (modifies another part of ap-db): https://github.com/Day8/re-frame/blob/b921fa497d169d7473c1b0124679f68f6c380208/src/re_frame/middleware.cljs#L182-L218 Your "something" will obliviously be different. If you use this method, then you do have to add this middleware to the right event handlers. In some cases, I can see how it would be convenient to just attach a single "watcher", in which case this safe (no memory leak): (let [source (subscribe [:....])] (reaction (something! @source))) Note: I took out the wrapping function definition you had -- I was a bit puzzled about the purpose of that -- perhaps I misunderstood. I assume you just want to setup a single watcher. And I'm guessing the something! must be side-effecting. -- Mike -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/clojurescript.
