Fork me on GitHub
#mount
<
2016-06-08
>
jiangts00:06:12

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

jiangts00:06:28

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

jiangts00:06:14

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.

tolitius01:06:54

@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)

jiangts01:06:23

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

jiangts01:06:20

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

jiangts01:06:50

the js library handles it by mutating a field

jiangts01:06:29

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

jiangts01:06:04

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

tolitius01:06:18

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

tolitius01:06:33

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

tolitius01:06:41

i.e. not a defstate

jiangts01:06:50

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

jiangts01:06:00

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

jiangts01:06:15

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

jiangts01:06:29

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

tolitius01:06:57

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?

tolitius01:06:46

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)
?

jiangts01:06:23

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

tolitius01:06:44

and instead you'd like it to be? 🙂

jiangts01:06:09

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

jiangts01:06:39

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

jiangts01:06:05

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

tolitius01:06:54

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?

jiangts01:06:14

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

jiangts01:06:31

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

jiangts01:06:59

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

tolitius01:06:16

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

jiangts01:06:42

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

tolitius01:06:14

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

tolitius01:06:49

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

tolitius01:06:26

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

jiangts01:06:18

gotcha. thanks for clarifying!

tolitius01:06:53

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