Fork me on GitHub
#clojurescript
<
2022-05-24
>
Kimo11:05:15

Interop question. Trying to set and get properties on a js/Event:

(defn component []
  [:a {:on-click #(do (set! (.-newProp %) 1)   ;; assign to an undefined prop
                      (js/console.log (.-newProp %))
                      (set! (.-timeStamp %) 1) ;; assign to a defined prop
                      (js/console.log (.-timeStamp %)))} 
      "clickable"])
Setting timeStamp seems fine, but setting/getting this undefined newProp causes 2 warnings (see thread). Any idea how I can fix them?

Kimo11:05:55

------ WARNING #1 - :infer-warning ---------------------------------------------
 File: ...
--------------------------------------------------------------------------------
 224 |    [password-validation-rx]])
 225 | 
 226 | (defn component []
 227 |   [:a {:on-click #(do (set! (.-newProp %) 1)   ;; assign to an undefined prop
-----------------------------------^--------------------------------------------
 Cannot infer target type in expression (. p1__75371# -newProp)
--------------------------------------------------------------------------------
 228 |                       (js/console.log (.-newProp %))
 229 |                       (set! (.-timeStamp %) 1) ;; assign to a defined prop
 230 |                       (js/console.log (.-timeStamp %)))} "clickable"])
 231 | 
--------------------------------------------------------------------------------

------ WARNING #2 - :infer-warning ---------------------------------------------
 File: ...
--------------------------------------------------------------------------------
 225 | 
 226 | (defn component []
 227 |   [:a {:on-click #(do (set! (.-newProp %) 1)   ;; assign to an undefined prop
 228 |                       (js/console.log (.-newProp %))
---------------------------------------------^----------------------------------
 Cannot infer target type in expression (. p1__75371# -newProp)
--------------------------------------------------------------------------------
 229 |                       (set! (.-timeStamp %) 1) ;; assign to a defined prop
 230 |                       (js/console.log (.-timeStamp %)))} "clickable"])
 231 | 
 232 | (rf/reg-fx
--------------------------------------------------------------------------------

p-himik11:05:21

Add ^js in front of %.

augustl11:05:09

related; I tried to find ^js in the docs, but failed. What is it and what does it mean?

p-himik11:05:49

Not sure whether it's mentioned anywhere in the official CLJS docs, but there's this blog post: https://code.thheller.com/blog/shadow-cljs/2017/11/06/improved-externs-inference.html

🎉 1
💯 1
Kimo13:05:21

Thanks, knew it was something like that. I kept trying #js , but I guess I wanted metadata, not a literal.

augustl13:05:42

is ^js specific to shadow-cljs?

p-himik13:05:06

Shouldn't be.

jpmonettas13:05:34

hi everybody! how can I get all loaded namespaces in a cljs repl? Reading the documentation it says that cljs.core/**loaded-libs** should contain that info but it doesn't work for me

clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "1.11.4"}}}' -M -m cljs.main
ClojureScript 1.11.4
cljs.user=> (require 'clojure.string)
nil
cljs.user=> cljs.core/*loaded-libs*
nil

dnolen13:05:36

@jpmonettas that really an internal detail - there's not a way to do this

jpmonettas16:05:37

oh, I got it from the example here http://cljs.github.io/api/cljs.core/STARloaded-libsSTAR Is there a way of accomplishing something like that? Like clojure (all-ns) but from a cljs repl

dvingo16:05:37

it looks like it's just there for parity with clojure: https://github.com/clojure/clojurescript/search?q=loaded-libs it appears to never be mutated

jpmonettas16:05:55

yeah, weird, no idea where that example on http://cljs.github.io is coming from

dnolen16:05:50

cljs.analyzer.api/all-ns not it is not dynamic at all, it works by examining the compilation state

dnolen16:05:17

so not quite the same as the Clojure functionality - which can't really be replicated because namespaces are not reified

jpmonettas16:05:03

@U050B88UR can that be called from a normal cljs repl? I'm trying but (cljs.analyzer.api/current-state) is nil

dnolen16:05:53

you cannot call that stuff directly, you need to make a macro yourself that calls it

jpmonettas16:05:56

using &env I guess

dnolen16:05:31

you don't need &env

dnolen16:05:49

just make a macro that invokes that fn and returns the result

dnolen16:05:24

note it doesn't actually do what all-ns does

dnolen16:05:36

it returns what the compiler has seen, not what is actually loaded into the REPL

jpmonettas16:05:04

I see, maybe it works for my use case. Thanks @U050B88UR

zimablue14:05:59

is there anything in clojurescript/SCI ecosystem that wraps process initialization through nodejs? like this js lib: https://github.com/sindresorhus/open

p-himik14:05:08

The library's description says "open stuff like URLs" and you say "wrap process initialization". How are the two related? What do you need exactly?

zimablue14:05:16

just being lazy/looking for a default, something that understands cross-platform process initialization and paths, wrapping nodejs path, process libraries to be more comfortable, the js library I showed doesn't just open urls it translates WSL/win32 paths, redirects in/out streams etc in order to run arbitrary commands/.exes

zimablue14:05:25

sorry for the unclear question

zimablue14:05:16

that library doesn't do tons I could just hack out my own solution to these minor problems but if someone else has already tested a sane implementation...

emccue15:05:53

sounds like you want https://github.com/babashka/process but for the node world?

zimablue15:05:49

yes, I think that's right

emccue15:05:07

nbb makes working in node more ergonomic than its ever been and its a very recent thing

emccue15:05:20

so i would say you probably want to look for nodejs packages 🤷

emccue15:05:08

like, why not just use open?

emccue15:05:53

(p/let [_ (open/open "unicorn.png" #js {:wait true})]
  (println "The image viewer app quit"))

zimablue15:05:17

That's what I'm doing now, I just generally prefer cljx packages when they exist

emccue15:05:29

ah then i guess the answer to give is "no, doesn't exist"

emccue15:05:19

could in the future if someone does the work

johanatan21:05:31

and supposing it is possible, would the following be the correct "externs" for the objs touched in the following js console:

var re_com = {};
re_com.box = {};
re_com.box.box = {};

sansarip22:05:48

I definitely recall using an externs file with cljs in the past, but I can’t recall off the top of my head how it all worked 😓 ’twas before I regularly took notes Also - you may already be aware, or it may not fit the bill in your case but - I do want to shout out https://github.com/binaryage/cljs-oops! It’s my go-to when I need to interop with third-party JS libraries and it totally circumvents the whole externs and type-hints headache.