I just realized that I could probably get away with inserting clojurescript into one-off projects I do at work.
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.
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.
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?When I'm calling handler
locally with event bound to #js{:httpMethod "POST" ... }
I get expected result.
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".
Thank you! I'll look into it.
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)
Yep. :)
Hello Folks, how can I convert this piece of javascript to clojurescript (using interop?)
view[i] = s.charCodeAt(i) & 0xFF
particularly the last bit ' & 0xFF '
the rest i can do with (aset)
ooooooooooooh thereβs bit-and
In which cases was aset
not recommended again and should you use gobject/set
instead? And when is it actually recommended to use aset
?
when its not a property like 0, 1, 2, 3, 4, 5 - as in an array
then gobject/set
so for numeric indexes, use aset
?
i think so
and when should you use gobject/set
instead of set!
(aside from set!
not being as dynamic)? when dealing with Closure advanced, I guess?
Hi, does anybody know a good base64 encoder/decoder working on cljs & clj ?
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)
)
Can obviously reuse the encoder and decoder on the JVM β that's just an illustrative example.
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 ...
Oh, I see. π:skin-tone-2:
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
goog.object
when working with data, set!
when working with "code"
FYI found https://github.com/cloojure/tupelo
The Base64 bits seem to be Clojure only and use Java interop?
Jupp ... unfortunately ...
Decided to follow your advice: https://gitlab.com/domaindrivenarchitecture/k8s-keycloak/-/blob/master/src/main/clj/dda/k8s_keycloak/base64.clj
now using different impl. for clj & cljs
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).
π
thx π
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?
proxy?
Does that exist in cljs?
cljs.user> proxy
WARNING: Use of undeclared Var cljs.user/proxy at line 1 <cljs repl>
oh, sorry didn't realize what channel I was in
No worries! π
@bmaddy what about a defrecord?
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.
defrecord doesn't require all the fields - its just a named type
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 [])
would be neat if ILookup could be extended via metadata - that would solve the issue a lot cleaner
here is a reference that i remember reading forever ago
Yeah, we did find those. It looks like they say you need to implement the entire thing with deftype
.
I haven't read the whole article though, just skimmed.
I hadn't realized deftype maps were open like that. Good to know!
https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L7894
here are all the intrinsic impls for a hashmap - you can maybe make a custom overridable map type helper given this
Yeah, I think that's what I'll have to do. Thanks for taking a look!
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
Anyone from PenPot here?
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 π
For that matter, shadow-cljs wouldn't be a terrible alternative if I could it to work too π