Seamlessly convert between pull-stream sources and ClojureScript core.async channels, preserving backpressure
Add [maackle/pull2chan "0.1.0"]
to your project.clj :dependencies
Imports at top of file:
(ns example
(:require [cljs.core.async :refer (chan <! >! close!)]
[pull2chan.core :refer (pull->chan chan->pull)])
(:require-macros [cljs.core.async.macros :refer [go]]))
(def pull (js/require "pull-stream"))
(let [source (. pull (values #js [1 2 3]))
ch (pull->chan source)]
(go (println (<! ch)) ; output: 1
(println (<! ch)) ; output: 2
(println (<! ch)) ; output: 3
(println (<! ch)) ; output: nil (channel is closed after source is depleted)
(println (<! ch)) ; output: nil (still closed...)
))
(let [ch (chan)
source (chan->pull ch)
sink (. pull (drain
#(println %)
#(println "done")))]
(pull source sink)
(go (>! ch 1)
(>! ch 2)
(>! ch 3)
(close! ch))
)
; output:
; 1
; 2
; 3
; done
Yes, you can go back and forth as much as you want!
See the tests for a few more examples
- Run
[rlwrap] lein figwheel devserver
in one session - Run
node target/devserver.js
in another
To run tests with hot reloading: lein doo node test