I use a mixin for this purpose all the time!

(defmixin go-loop-aware
  (init-aware [owner]
              {:chans {:mounted (async/chan)}})
  (will-unmount [owner]
                (async/close! (om/get-state owner [:chans :mounted])))
  (go-loop-aware [owner read-chan callback]
            (when (exists? js/window)
              (let [mounted (om/get-state owner [:chans :mounted])]
                (go-loop []
                  (when-some [v (first (async/alts! [read-chan mounted]))]
                             (callback v)
                             (recur)))))))

I like the kill channel solution better personally since like Daniel point out, 
it doesn't require the read-chan to be owned or controlled by the component. 

When [:chans :mounted] is closed, the go-loop exits. I always use when-some 
over when-let here since when-let would close the go block if false was ever 
submitted to your read channel, whereas when-some only evaluates (not= nil)

-- 
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 clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to