weavejester / reagi

An FRP library for Clojure and ClojureScript
232 stars 13 forks source link

publish #9

Closed kul closed 10 years ago

kul commented 10 years ago

I am not sure if i am missing the obvious but how can i treat a core.async channel as a event stream? Would it make sense to have (publish stream channel) where channel is publishing to stream, instead of subscribing like in (subscribe stream channel) and is completed? when channel closes.

weavejester commented 10 years ago

This is covered in the README. Take a look at the port and subscribe functions.

kul commented 10 years ago

@weavejester i am being thick here it seems, so given an core.async channel ch which is being >!'d from somewhere else, how do i get a stream out of it?

kul commented 10 years ago

So i had a crack at it, obviously it is not implemented as protocol methods which are there in reagi.core, but here is what i am trying to do

(defn publish
  [stream channel]
  (go (loop [m (<! channel)]
        (if m (r/deliver stream m) (r/completed nil))
        (when m (recur (<! channel))))))
weavejester commented 10 years ago

You can use the port function from Reagi, and the pipe function from core.async:

(a/pipe channel (r/port stream))
kul commented 10 years ago

Thanks!