mount

jiangts 2016-06-08T00:42:12.000045Z

@tolitius: ah, just got back to this. Does your solution only update the states when mount/stop and mount/start are called?

jiangts 2016-06-08T00:43:28.000046Z

I'm using re-frame for my front end and I want to change some mutable fields on the webrtc javascript object when my app-db is updated

jiangts 2016-06-08T00:45:14.000047Z

so setting fields on the mutable webrtc object isn't something that just happens on mount start, it happens throughout the lifecycle of the component.

tolitius 2016-06-08T01:13:54.000050Z

@jiangts: if the map is {:socketURL (config :some-url) :onstream (config :on-stream-fn)}, then after you change the config and do

(mount/stop #'app/config)
(mount/start #'app/config)
the map will have updated values since it calls into config to get them every time. unless I am missing something in your use case (I have not used reframe)

jiangts 2016-06-08T01:18:23.000051Z

right, I gathered the above code would update the fields. However, I thought it'd be cleaner if I didn't have to explicitly call mount/stop and mount/start every time I wanted to update the fields

jiangts 2016-06-08T01:20:20.000055Z

for example, for the webrtc library i'm using, the user can toggle whether or not they want to stream audio, video, or both

jiangts 2016-06-08T01:20:50.000056Z

the js library handles it by mutating a field

jiangts 2016-06-08T01:21:29.000057Z

I want the user to be able to click on buttons on the user interface to alter what the field contains to get the right behavior from webrtc

jiangts 2016-06-08T01:22:04.000058Z

personally it feels a bit heavy handed to have to call mount/stop and mount/start each time a user clicks such a button

tolitius 2016-06-08T01:23:18.000059Z

of course, I am with you.. however I can't really connect it to mount

tolitius 2016-06-08T01:23:33.000060Z

why can't that map be just an atom with watchers?

tolitius 2016-06-08T01:23:41.000061Z

i.e. not a defstate

jiangts 2016-06-08T01:23:50.000062Z

yeah that's what i'm doing now lol. 😛

jiangts 2016-06-08T01:24:00.000063Z

or actually, I'm putting an atom into the defstate

jiangts 2016-06-08T01:24:15.000064Z

the reason i'm doing that is because adding a watch feels stateful to me, so i want that to be managed by mount

jiangts 2016-06-08T01:24:29.000065Z

so on :start it can add the watch and on :stop it can remove it

tolitius 2016-06-08T01:25:57.000066Z

I see.. so the reason you want this map to be a mount's state is to add a lifecycle to this atom to add/remove watcher, right?

jiangts 2016-06-08T01:26:04.000068Z

right

tolitius 2016-06-08T01:27:46.000069Z

ok, and right now you do have it working by having a

(defstate my-map :start (create-atom-with-watcher) :stop (remove-watcher-and-reset-atom my-map)
?

jiangts 2016-06-08T01:28:23.000070Z

yes. the create-atom-with-watcher function returns an atom

tolitius 2016-06-08T01:28:44.000071Z

and instead you'd like it to be? 🙂

jiangts 2016-06-08T01:29:09.000072Z

I guess it works at this point, I just hadn't figured it out when I first asked hte question 🙂 haha

jiangts 2016-06-08T01:29:39.000073Z

I haven't used mount much, so the double deref @@ feels a bit ugly to me, but otherwise it works fine!

jiangts 2016-06-08T01:30:05.000074Z

I saw the double deref in the datalog code example for cljs so I'm guessing it's acceptable enough

tolitius 2016-06-08T01:30:54.000076Z

hold on, but

(defstate my-map :start (create-atom-with-watcher) :stop (remove-watcher-and-reset-atom my-map)
does not use @@ right, it is just an atom with a watcher?

jiangts 2016-06-08T01:32:14.000078Z

ah, not that code. I guess I never explicitly @@, but it's just something I need to keep in mind. For example, now I need to make sure I swap! on a @my-map rather than just on my-map

jiangts 2016-06-08T01:32:31.000079Z

But I think it's fine, I just had to figure out this pattern 🙂

jiangts 2016-06-08T01:32:59.000080Z

thanks!! btw the clj(s) community is incredible, I almost never get to talk to library maintainers!!

tolitius 2016-06-08T01:33:16.000081Z

right (swap! @my-map fn) since it is cljs: i.e. the state is derefable

jiangts 2016-06-08T01:33:42.000082Z

ah, right! I forgot in the clj version you do not need to deref the state.

tolitius 2016-06-08T01:34:14.000083Z

right, so @ in cljc-mode is consistent accross cljs and clj

tolitius 2016-06-08T01:34:49.000084Z

but if you would have used mount for strictly clj side of things, you would just do (swap! my-map fn)

tolitius 2016-06-08T01:35:14.000085Z

there are a bit more details here: https://github.com/tolitius/mount/blob/master/doc/clojurescript.md#managing-state-in-clojurescript

tolitius 2016-06-08T01:36:26.000087Z

I think your approach with

(defstate my-map :start (create-atom-with-watcher) :stop (remove-watcher-and-reset-atom my-map)
where (create-atom-with-watcher) creates a regular atom with a watcher is a fine solution

jiangts 2016-06-08T01:37:18.000088Z

gotcha. thanks for clarifying!

tolitius 2016-06-08T01:38:53.000089Z

sure, very welcome 🙂 let me know if you'd have more questions / thoughts