nbb

2022-08-05T05:36:22.455079Z

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

2022-08-05T06:51:49.243049Z

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

borkdude 2022-08-05T08:23:11.954089Z

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

borkdude 2022-08-05T08:27:28.191379Z

I'll try to find a workaround

borkdude 2022-08-05T08:30:27.756859Z

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

2022-08-05T10:15:52.513899Z

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.

2022-08-05T10:18:39.022719Z

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

🀌 1
πŸŽ‰ 4
eval2020 2022-08-05T07:41:58.428469Z

nbb currently at the frontpage of HN: https://hn.algolia.com/?q=Clojure%20Scripting%20on%20Node.js πŸ™‚

πŸ‘ 2
littleli 2022-08-08T13:35:36.500609Z

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.

borkdude 2022-08-05T08:22:49.913649Z

Thanks for sharing!

πŸ‘ 1