Skip to content

Instantly share code, notes, and snippets.

@stuartc
Created November 9, 2018 07:09
Show Gist options
  • Save stuartc/c20797e5d3229eac438d6f937121a697 to your computer and use it in GitHub Desktop.
Save stuartc/c20797e5d3229eac438d6f937121a697 to your computer and use it in GitHub Desktop.
Clojurescript child_process with core.async
(ns testing.core
(:require-macros [cljs.core.async.macros :refer [go alt!]])
(:require [cljs.core.async :refer [<! take! chan close! put! go-loop >!] :as async])
(:require ["child_process" :refer [spawn]]))
(defn done-message? [message]
(and
(vector? message)
(= (message 0) :done)))
(defn main! []
(let [channel (chan)
command "echo 1 && sleep 1 && echo 2 && sleep 1 && echo 3"]
(go-loop []
(let [v (<! channel)]
(if (done-message? v)
(do
(println "closing channel")
(close! channel))
(do
(println (.toString v "UTF8"))
(recur)))))
(let [proc (spawn "bash" (clj->js ["-c" command]))]
(.on (.-stdout proc) "data" #(put! channel %))
(.on (.-stderr proc) "data" #(put! channel %))
(.on proc "close" #(put! channel [:done %])))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment