Fork me on GitHub
#nbb
<
2022-08-05
>
Tim Potter05:08:22

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

trgoodwin06:08:49

cljs has a macro named this-as in its core that may help you. have a look at that.

borkdude08:08:11

Unfortunately this-as isn't available in nbb, but you can work around this.

borkdude08:08:28

I'll try to find a workaround

borkdude08:08:27

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

Tim Potter10:08:52

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.

Tim Potter10:08:39

I’m super impressed with how compact the nbb code is compared with the equivalent in Nodejs.

🎉 4
🤌 1
borkdude08:08:49

Thanks for sharing!

👍 1
littleli13:08:36

This was by far the best received post about nbb on hackernews if I check this correctly. And @U04V15CAJ nicely replied to op questions. It's however quite obvious that Lisp (in general) is a niche in our industry.