jamesmacaulay / zelkova

Elm-style FRP for Clojure and ClojureScript
MIT License
403 stars 9 forks source link

"Unmounting" zelkova #9

Open bensu opened 9 years ago

bensu commented 9 years ago

If I have an om component that starts a live graph when mounted, I would like to take the graph down when unmounting it. Is this possible? I'm currently doing something like this:

(defn pipe-to-atom
  "Adds live-graph to the return value of the original z/pipe-to-atom"
  [x]
  (let [live-graph (z/spawn x)]
    [(z/pipe-to-atom live-graph
       (atom (zimpl/init live-graph)
         :meta {::source live-graph}))
     live-graph]))

(defn zortable [data owner opts]
    (reify
      om/IWillMount
      (will-mount [_]
        (let [signal (state-signal opts)
              [state-ref live-graph] (pipe-to-atom signal)]
          (add-watch state-ref ::sortable
            (fn [_ _ _ nv]
              (om/update-state! owner #(merge % nv))))
          (om/set-state! owner :live-graph live-graph)
          (om/set-state! owner :state-ref state-ref)))
      om/IWillUnmount
      (will-unmount [_]
        (async/close! (om/get-state owner :live-graph)))
      ...)

The only thing I figured out was to async/close! the live-graph since it implements the protocol but it is not enough. After the component is unmounted, zelkova's event listeners still put! values in channels which raises the following exception on each mouse movement:

Uncaught Error: Assert failed: No more than 1024 pending puts are allowed on a single channel. Consider using a windowed buffer. (< (.-length puts) impl/MAX-QUEUE-SIZE

jamesmacaulay commented 9 years ago

Thanks for the report, I'm not sure why this is happening. The intention is definitely that you should be able to close! a graph when you're done with it. I'll have to do some investigation to see where channels are not being closed when I expect them to be.