does anyone see a problem with the following code?:
(require 'goog.object)
(def spawn (goog.object/get (js/require "child_process") "spawn"))
(def proc (spawn "ls" (clj->js ["-lh", "/usr"])))
((.. proc -stdout -on) "data" (fn [data] (js/console.log data)))
(js/console.log "we are here")
we are here
doesn't print
and i get the following exception:
Cannot read property '_events' of null
_addListener (events.cljs:201:19)
addListener (events.cljs:258:10)
Readable.on (_stream_readable.cljs:784:35)
(evalmachine.<anonymous>:11:26)
Script.runInThisContext (vm.cljs:65:33)
Object.runInThisContext (vm.cljs:197:38)
(<http://Object.yt|Object.yt>)
(Object.lumo.repl.caching_node_eval)
(NO_SOURCE_FILE <embedded>:5824:287)
z (NO_SOURCE_FILE <embedded>:5825:306)
@johanatan looking, FWIW you can use (require '[child_process :refer [spawn]])
feels more idiomatic to me
ah yea. that's better
@johanatan I think it’s a problem with JS this
binding
unfortunate
here’s how you can write it:
(.on (.-stdout ls) "data" (fn [data] (prn data)))
ah, so .on
instead of .-on
?
(.. proc -stdout -on)
-> (.on (.-stdout proc))
if you start lumo with -v
/ --verbose
here’s the generated code:
cljs.user=> ((.. ls -stdout -on) "data" (fn [data] (prn data)))
Evaluating ((.. ls -stdout -on) "data" (fn [data] (prn data)))
cljs.user.ls.stdout.on.call(null,"data",(function (data){
return cljs.core.prn.call(null,data);
}))
Cannot read property '_events' of null
_addListener (events.cljs:201:19)
addListener (events.cljs:258:10)
Readable.on (_stream_readable.cljs:784:35)
(evalmachine.<anonymous>:1:24)
Script.runInThisContext (vm.cljs:65:33)
Object.runInThisContext (vm.cljs:197:38)
(Object.wt)
(Object.lumo.repl.caching_node_eval)
(NO_SOURCE_FILE <embedded>:5824:287)
z (NO_SOURCE_FILE <embedded>:5825:306)
hmm, i got The "listener" argument must be of type Function
vs:
cljs.user=> (.on (.-stdout ls) "data" (fn [data] (prn data)))
Evaluating (.on (.-stdout ls) "data" (fn [data] (prn data)))
cljs.user.ls.stdout.on("data",(function (data){
return cljs.core.prn.call(null,data);
}))
#object[Socket [object Object]]
oh, oops, extra set of parens
you’re probably not passing "data"
as the arg
or something
but do you see the difference above?
yea, got it.
thx!
one is using .call(null)
^ this binds the function this
to null
which is wrong
btw, how do you get lumo to output the js it generates?
lumo --verbose
ah, cool
or -v