bhauman / devcards

Devcards aims to provide a visual REPL experience for ClojureScript
1.53k stars 116 forks source link

What would be an idiomatic way to display core.async channel results? #78

Closed mikavilpas closed 8 years ago

mikavilpas commented 8 years ago

Hi, I'm using devcards to create a build monitor app and lovin' it!

I have core.async channels in many places, and like to create proof of concept devcards to see what they return. Here's a minimal example of the code I use for this:

(def render-reagent-atom deref) ; for readability

(defcard-rg read-config-from-url-result
  render-reagent-atom
  (let [state (reagent/atom nil)]
    (go (let [conf (<! (config/read-config-from-url config-file-url))]
          (reset! state conf)))
    state))

I need the deref as the render function so a re-render will take place once the async go block completes (I think this is why at least). Do you know of a more idiomatic way to achieve this?

bhauman commented 8 years ago

Something like this?

;; helper
(defn channel->atom [ch atom]
   (go (reset! state (<! ch))) 

(defonce view-atom (atom :empty))

(defcard view-channel view-atom)

(channel->atom (config/read-config-from-url config-file-url) view-atom)
mikavilpas commented 8 years ago

Yes, that works nicely. This is what I came up with, for reference:

(defn channel->atom
  "Allows showing the first channel result in a devcard:

  (defcard foo
    (channel->atom (go :hello)))"
  [channel]
  (let [atom (reagent/atom :empty)]
    (go (reset! atom (<! channel)))
    atom))