add-watch and writing data into a core.async channel

277 views
Skip to first unread message

Raymond Huang

unread,
Jul 31, 2015, 2:21:18 AM7/31/15
to Clojure
I'd like to use `add-watch` on an atom which writes the data to a core.async channel. So far, I've come up with this, but it seems bad because I create a new go-routine everytime something happens.

(add-watch ref watch-id #(go (a/>! user-changes %&)))

This seems like a bad idea to me because I am creating a new go routine everytime. Furthermore, I'd like this to be portable between Clojure & Clojurescript.

I can't think of how I'd be able to solve this.

Leon Grapenthin

unread,
Jul 31, 2015, 4:22:38 AM7/31/15
to Clojure, 12ay....@gmail.com
Use put!

Stefan Kamphausen

unread,
Jul 31, 2015, 8:53:35 AM7/31/15
to Clojure, 12ay....@gmail.com

On Friday, July 31, 2015 at 8:21:18 AM UTC+2, Raymond Huang wrote:
I'd like to use `add-watch` on an atom which writes the data to a core.async channel. So far, I've come up with this, but it seems bad because I create a new go-routine everytime something happens.


Makes me think, one might want to have an add-channel function on ARef in general.


stefan
 

Timothy Baldridge

unread,
Jul 31, 2015, 9:02:14 AM7/31/15
to clo...@googlegroups.com
One "gotcha" is that atoms add-watches do not always guarantee order semantics. So if you have a watch put [old-val new-val] on a channel, and your atom operation is something like (swap! a inc), you may see values in your channel like this:

[0 1]
[2 3]
[1 2]
[3 4]
[6 7]
[4 5]

This is because the watches are dispatched in different threads. So just be careful when doing this. 

Timothy

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

Raymond Huang

unread,
Jul 31, 2015, 1:51:42 PM7/31/15
to clo...@googlegroups.com
Thanks everyone for the suggestions, I'll try it out later today

@leon Thanks. I wish this were documented in the API for how to write Clojure/Script compatible code. Just saw your answer on stackoverflow which was very insightful.

@timothy Interesting to know. I've seen that myself, but assumed it was solely due to a logic flaw in my program. I'll have to think through this problem more.
Reply all
Reply to author
Forward
0 new messages