On Saturday, August 30, 2014 8:24:19 PM UTC-5, Daniel Kersten wrote:
> Something like this:
> 
> 
> (defn component [data owner]
>   (reify
>     om/IInitState
> 
> 
>     (init-state [_]
>       (let [chan (chan)]
> 
> 
>         {:chan chan
>          :kill (chan)
>          :pub  (pub chan first)}))
> 
> 
> 
> 
>     om/IWillMount
>     (will-mount [_]
>       (let [pub (om/get-state owner :pub)
> 
> 
>             chan (chan)
>             kill (om/get-state owner :kill)]
>         ; If using pub/sub, need to subscribe
> 
> 
> 
>         (sub pub :foo chan) 
>         (go-loop []
> 
> 
>           (let [[v c] (alts! [chan kill])]
>             (when-not (= c kill)
> 
> 
>               (do-something v)
>               (recur))
>             ; If using pub/sub, need to unsubscribe:
> 
> 
>             (unsub pub :foo chan)))))
> 
> 
>     om/IWillUnmount
> 
> 
>     (will-unmount [_]
>       (put! (om/get-state owner :kill) :quit))
> 
> 
> 
> 
>     om/IRenderState
>     (render-state [_ {:keys [chan]}]
> 
> 
>       (dom/div {:onClick #(put! chan [:foo "Hello"])}
>         "Click me to send [:foo \"Hello\"]"))))
> 
> 
>             
> 
> 
> 
> On 30 August 2014 16:08, Dhruv Bhatia <dh...@dhruvbhatia.com> wrote:
> 
> 
> 
> On Sunday, 13 July 2014 01:46:41 UTC+10, Daniel Kersten  wrote:
> 
> > Channels are cheap, but unless they get garbage collected (and I assume not 
> > closing them will prevent this), they will still take up some resources.
> 
> >
> 
> >
> 
> > Related and important is that you shut down any go blocks that you create 
> > in IWillMount, especially if they take from a channel which may still be 
> > receiving data (eg: through mux or pub/sub). If you don't shut these down, 
> > they will continue to run even after the component has unmounted and if the 
> > component is later mounted again, you will have multiple go blocks doing 
> > the same thing! Besides using resources, this can actually be dangerous if 
> > you are accessing owner inside the go block (eg to set local state) as you 
> > may end up accessing an unmounted owner. The simplest solution is having a 
> > kill channel that you listen on using async/alt that gets closed in the 
> > IWillUnmount and that terminates the go block.
> 
> 
> 
> 
> 
> Thanks for the explanation. Would you be able to provide a code example that 
> demonstrates how to properly clean up a pub/sub watcher within a component's 
> IWillUnmount lifecycle method?
> 
> 
> 
> 
> 
> --
> 
> 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 clojurescrip...@googlegroups.com.
> 
> To post to this group, send email to clojur...@googlegroups.com.
> 
> Visit this group at http://groups.google.com/group/clojurescript.

This was so extremely helpful, thanks!

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