Fork me on GitHub
#lumo
<
2018-05-23
>
johanatan18:05:23

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")

johanatan18:05:31

we are here doesn't print

johanatan18:05:47

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)
	 ()
	 (Object.lumo.repl.caching_node_eval)
	 (NO_SOURCE_FILE <embedded>:5824:287)
	 z (NO_SOURCE_FILE <embedded>:5825:306)

anmonteiro18:05:15

@johanatan looking, FWIW you can use (require '[child_process :refer [spawn]])

馃槻 4
anmonteiro18:05:39

feels more idiomatic to me

johanatan18:05:47

ah yea. that's better

anmonteiro18:05:23

@johanatan I think it鈥檚 a problem with JS this binding

anmonteiro18:05:33

here鈥檚 how you can write it:

anmonteiro18:05:43

(.on (.-stdout ls) "data" (fn [data] (prn data)))

johanatan18:05:04

ah, so .on instead of .-on ?

anmonteiro18:05:08

(.. proc -stdout -on) -> (.on (.-stdout proc))

anmonteiro18:05:15

if you start lumo with -v / --verbose here鈥檚 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)

johanatan18:05:15

hmm, i got The "listener" argument must be of type Function

anmonteiro18:05:28

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]]

johanatan18:05:45

oh, oops, extra set of parens

anmonteiro18:05:46

you鈥檙e probably not passing "data" as the arg

anmonteiro18:05:50

or something

anmonteiro18:05:05

but do you see the difference above?

johanatan18:05:09

yea, got it.

anmonteiro18:05:10

one is using .call(null)

anmonteiro18:05:21

^ this binds the function this to null

anmonteiro18:05:26

which is wrong

johanatan18:05:29

btw, how do you get lumo to output the js it generates?

anmonteiro18:05:33

lumo --verbose