Fork me on GitHub
#clojurescript
<
2021-05-28
>
Jacob Rosenzweig01:05:58

I just realized that I could probably get away with inserting clojurescript into one-off projects I do at work.

Jacob Rosenzweig01:05:50

E.g. I made a "return to campus" bot for our employees and I'm basically under no review for that. I guess I'd have to go to make a ticket to get the cljs compiler approved, but that wouldn't be hard.

😎 6
Jacob Rosenzweig01:05:52

Kind of OT but @dnolenΒ I am watching an old talk you gave and I still am surprised that some projects still try to make "readable javascript". "Rescript" (a rebranding of "Reason" from Facebook) attempts to reproduce readable javascript as well.

Stas Makarov13:05:35

I've just got a strange bug while playing with cljs on aws lambda (via node). I have this code:

(defn handler [event ctx callback]
   (println "event:  " event)
   (println "http method " (.-httpMethod event) (. event -httpMethod) (goog.object/get event "httpMethod") (aget event "httpMethod"))
.... 
In cloudwatch logs I get: INFO event: #js {:resource /patients, :path /patients, :httpMethod POST, ... INFO http method nil nil POST POST Why (.-httpMethod event) and (. event -httpMethod) might not work as expected?

Stas Makarov13:05:15

When I'm calling handler locally with event bound to #js{:httpMethod "POST" ... } I get expected result.

βœ… 3
p-himik13:05:03

I'm guessing advanced compilation. httpMethod and other field names were mangled. Add ^js in front of event in the function signature. Words for looking up why: "clojurescript externs inference".

Stas Makarov13:05:23

Thank you! I'll look into it.

Stas Makarov13:05:38

hehe "Externs or rather the lack thereof are often to blame here and possibly the most frequent issue coming up in the #clojurescript Slack (and elsewhere)." ( https://code.thheller.com/blog/shadow-cljs/2017/10/15/externs-the-bane-of-every-release-build.html)

kah0ona14:05:27

Hello Folks, how can I convert this piece of javascript to clojurescript (using interop?) view[i] = s.charCodeAt(i) & 0xFF

kah0ona14:05:38

particularly the last bit ' & 0xFF '

kah0ona14:05:53

the rest i can do with (aset)

kah0ona14:05:39

ooooooooooooh there’s bit-and

borkdude14:05:57

In which cases was aset not recommended again and should you use gobject/set instead? And when is it actually recommended to use aset ?

emccue14:05:39

when its not a property like 0, 1, 2, 3, 4, 5 - as in an array

emccue14:05:59

then gobject/set

borkdude14:05:02

so for numeric indexes, use aset?

thheller17:05:44

all the aset aget and related functions are for working with arrays, just like in clojure. in JS it just happens to also work for property access since it uses the same thing["foo"] and thing[0] notation. so aset for arrays, which also implies numeric indexes

emccue14:05:14

i think so

borkdude14:05:43

and when should you use gobject/set instead of set! (aside from set! not being as dynamic)? when dealing with Closure advanced, I guess?

thheller17:05:17

goog.object when working with data, set! when working with "code"

jerger_at_dda16:05:59

Hi, does anybody know a good base64 encoder/decoder working on cljs & clj ?

flowthing16:05:56

If you're targeting the browser, I guess this is one option:

(ns my.base64
  #?(:clj (:import (java.util Base64))))

(defn base64-encode
  [string]
  #?(:clj (.encodeToString (Base64/getEncoder) (.getBytes string))
     :cljs (.btoa js/window string)))

(defn base64-decode
  [string]
  #?(:clj (String. (.decode (Base64/getDecoder) string) "UTF-8")
     :cljs (.atob js/window string)))

(comment
  (-> "Hello, world!" base64-encode base64-decode)
  )

flowthing16:05:04

Can obviously reuse the encoder and decoder on the JVM β€” that's just an illustrative example.

jerger_at_dda17:05:42

we're writing apps targeting to cljs & clj So I'm searching for a pure clojure implementation in order to minimize platform specific code parts ...

flowthing17:05:38

Oh, I see. πŸ‘:skin-tone-2:

flowthing17:05:51

The Base64 bits seem to be Clojure only and use Java interop?

jerger_at_dda18:05:12

Jupp ... unfortunately ...

jerger_at_dda18:05:19

now using different impl. for clj & cljs

flowthing18:05:09

As I mentioned earlier, I believe you can reuse both the encoder and the decoder, instead of making a new one for each call. Also, you might want (.getBytes string "UTF-8") (unless you know you specifically want your platform's default encoding).

bmaddy19:05:57

I have a map that is used many places in the codebase. Sometimes keys are looked up on it that don't exist. I'd like to know what keys are being looked up and print them, store them somewhere, or whatever. My first thought is that I should be able to make a new type, inherit from PersistentHashMap and only implement ILookup. Is there a way to do that or do I really need to reimplement the entire map type?

bmaddy20:05:25

Does that exist in cljs?

cljs.user> proxy
WARNING: Use of undeclared Var cljs.user/proxy at line 1 <cljs repl>

Alex Miller (Clojure team)20:05:23

oh, sorry didn't realize what channel I was in

bmaddy20:05:48

No worries! πŸ™‚

emccue20:05:33

@bmaddy what about a defrecord?

bmaddy20:05:18

Unfortunately, I don't know all the fields, so I don't think I can do that. More context: My map is being built from a response from Datomic and the pull expression has a wildcard in it that I'm trying to get rid of. πŸ˜• Good thought though.

emccue20:05:43

defrecord doesn't require all the fields - its just a named type

emccue20:05:01

i'm not sure how to get around the circularness of it - how to access the underlying "get" functionality if you are overriding it - but at the very least you can store any fields you want in a defrecord like (defrecord ABC [])

emccue20:05:55

would be neat if ILookup could be extended via metadata - that would solve the issue a lot cleaner

emccue20:05:51

here is a reference that i remember reading forever ago

bmaddy20:05:59

Yeah, we did find those. It looks like they say you need to implement the entire thing with deftype.

bmaddy20:05:09

I haven't read the whole article though, just skimmed.

bmaddy20:05:55

I hadn't realized deftype maps were open like that. Good to know!

emccue20:05:09

here are all the intrinsic impls for a hashmap - you can maybe make a custom overridable map type helper given this

bmaddy20:05:44

Yeah, I think that's what I'll have to do. Thanks for taking a look!

bmaddy02:05:24

Yep, emccue mentioned that one. I was hoping there was a way to do it without having to reimplement the entire type for such a tiny change, but It looks like there isn't.

Rob Haisfield22:05:51

I know Figwheel and Reagent are pretty cool together for ClojureScript front-end. Is there anything really cool for direct output manipulation? https://youtu.be/jC2_O5Jh_Rg

Rob Haisfield00:05:00

Anyone from PenPot here?

Donnie West23:05:04

Does anyone know a good way to interop w/ JS tooling that expects a default esm export in the index.js file using ClojureScript's build tooling? Trying something akin to https://clojurescript.org/guides/webpack but having trouble adapting it πŸ˜•

Donnie West23:05:17

For that matter, shadow-cljs wouldn't be a terrible alternative if I could it to work too πŸ˜‚

Donnie West00:05:42

Finally figured this out with shadow-cljs, but I'm really curious how this would be done w/ clojurescript's build tools. I'm guessing that it's going to mesh better with what I'm trying to accomplish too