Problem with Om and core.async pub/sub channels

267 views
Skip to first unread message

Michiel Borkent

unread,
Sep 18, 2014, 2:39:27 PM9/18/14
to clojur...@googlegroups.com
Code: https://www.refheap.com/90401

Problem: the go loop in the child component (line 23) that reads messages from a channel doesn't continue after I press that button that changes state in the parent component.
If I uncomment that state change in the button event handler it all works ok.
What could be wrong?

Gijs S.

unread,
Sep 18, 2014, 4:48:59 PM9/18/14
to clojur...@googlegroups.com
With line 41 uncommented the following seems to happen:

1. Parent component's state changed
2. om/react "walks" the component tree in parent's render to see what should update
3. on line 45 with om/build for the child component finds that the child component already exists, so there is no new component created nor mounted.
4. However, "running"/calling om/build on line 45 created a new subscription to the event-pub through :subscriber/:create-subscriber in {:init-state ...}
5. There will not be a new component created that would create a go-loop to consume from this new subscriber channel (there's no call to om/will-mount for a new component from line 22)
6. Now event-pub has two subscribers but only one go-loop that consumes from a channel. The pub on :event-ch will block [1] [2]
7. Weirdness on the page

Seems you shouldn't have side-effects in the {:init-state ...} passed to om/build. Instead pass the event-pub to the child component via :init-state and create the sub chan together with the go-loop to consume from it.

-Gijs

[1] http://clojure.github.io/core.async/#clojure.core.async/pub
"Each item is distributed to all subs in parallel and synchronously,
i.e. each sub must accept before the next item is distributed. Use
buffering/windowing to prevent slow subs from holding up the pub."
[2] Play around with buffering in the chan on line 57 to see this behavior change for a couple of clicks

Michiel Borkent

unread,
Sep 18, 2014, 5:38:25 PM9/18/14
to clojur...@googlegroups.com
Thanks Gijs,

This really explains a lot.
> --
> Note that posts from new members are moderated - please be patient with your first post.
> ---
> You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/5rCTfnulNXI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
> To post to this group, send email to clojur...@googlegroups.com.
> Visit this group at http://groups.google.com/group/clojurescript.



--
http://www.michielborkent.nl
http://www.eetvoorjeleven.nu
Reply all
Reply to author
Forward
0 new messages