Hiho everyone. How do I access this inside a class method? Thought it might be passed as the 0th arg and next guess is js/this but no luck with either of these.
(ns stream-test
(:require [cljs.pprint :refer [pprint]]
["readable-stream" :as stream]))
(def number-stream
(new stream.Readable
#js{:objectMode true
:read (fn [count] (.push js/this βhelloβ))}))
(-> number-stream
(.on "data" (fn [chunk] (pprint (list "got" chunk))))
(.on "error" (fn [err] (pprint (list "err" err)))))
;; ("err" #object[TypeError TypeError: Cannot read property 'push' of undefined])cljs has a macro named this-as in its core that may help you. have a look at that.
Unfortunately this-as isn't available in nbb, but you can work around this.
I'll try to find a workaround
Ah the workaround is simple:
(ns stream-test
(:require [cljs.pprint :refer [pprint]]
["readable-stream" :as stream]))
(def number-stream
(new stream.Readable
#js{:objectMode true
:read (fn [_count] (.push number-stream "hello"))}))
(-> number-stream
(.on "data" (fn [chunk] (pprint (list "got" chunk))))
(.on "error" (fn [err] (pprint (list "err" err)))))Aha - thanks! Something like that was next on the list to try but good to know there isnβt a pseudo- this I should be using. Calling the object instance makes more sense anyway.
Iβm super impressed with how compact the nbb code is compared with the equivalent in Nodejs.
nbb currently at the frontpage of HN: https://hn.algolia.com/?q=Clojure%20Scripting%20on%20Node.js π
This was by far the best received post about nbb on hackernews if I check this correctly. And @borkdude nicely replied to op questions. It's however quite obvious that Lisp (in general) is a niche in our industry.
Thanks for sharing!