Fork me on GitHub
#clojurescript
<
2022-02-04
>
Richie03:02:39

Hey. I want to save a https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview before and after every function in my app. Cljs doesn't have alter-var-root and I haven't figured out how to refer to the original function after I set! it to a new function. e.g.

(defn hello [] (println "hello world"))
(set! hello (fn [] (println "before") (hello)))
(hello)
recurses infinitely. If I had alter-var-root or something like it then I'd try to iterate through (ns-interns ') and modify each function. I also looked through the :compile-prepare shadow build context but I didn't find anything that I could use. Thanks!

phronmophobic03:02:35

not sure exactly what you're trying to do, but Chrome let's you profile your app and save it to a json file. You could then post process it that with a sourcemap.

phronmophobic03:02:37

there are a few other options, but it's not clear what your end goal is.

Richie03:02:14

(map #(alter-var-root %
                      (fn [f]
                        (fn [& args]
                          (println "before")
                          (apply f args)
                          (println "after"))))
     (vals (ns-interns ')))
I want to instrument every function in my namespace.

Richie03:02:59

I don't see how to get what I want out of tools.trace.

Richie03:02:49

If that even works on cljs, I haven't tried it.

Richie04:02:16

I'm actually interested in generate a trace and then importing it into chrome to browse it.

phronmophobic04:02:30

right, but why do you want the trace?

phronmophobic04:02:09

the profiler does a decent job of tracing, but it looks like it's a sampling profiler.

Richie04:02:58

The trace that chrome gives me isn't helpful. I see mostly anonymous functions and stuff unrelated to what I'm trying to debug in my app. You're suggesting that if I postprocess it with a source map then maybe I can get what I want? I haven't considered that.

phronmophobic04:02:10

if there's a sourcemap, I think google chrome might do it for you

Richie04:02:15

I'm still interested in generating a trace of my functions. I'm sorry if my first example was unclear or confusing. Hopefully the pseudocode example makes it clear what I'm trying to do.

phronmophobic04:02:29

I know Chrome does use sourcemaps with debugger

phronmophobic04:02:12

looks pretty hairy

Richie04:02:40

That's a good point, thanks.

olaf03:02:56

What is the easy way to import a JS module (it exports a JS class) to use inside cljs? Where should I put the JS file? Is not an NPM package and does not attach to window . I’m using shadow-cljs if it helps

Drew Verlee04:02:33

With shadow you declare js deps in a package.json. from there, check the shadow docs to match how the code is imported in js and against the shadow/cljs equivalent.

👍 1
winsome16:02:07

What's the equivalent of byte-array in cljs? Do you just make an array of ints?

winsome16:02:22

Wait, js doesn't have ints, does it?

winsome16:02:51

Oh bother, I'm going to have to do some re-learning of the basics

dnolen16:02:07

@winsome JS has typed arrays now

winsome16:02:01

Alright, so in cljc, these are equivalent:

(def b [0x72 0x65 0x6e 0x63 0x68])
#?(:clj (byte-array b) :cljs (js/Uint8Array. b))

p-himik16:02:32

They are equivalent in terms of both being collections of bytes. But they most likely provide you with differing APIs.

quoll18:02:44

JS byte arrays can be accessed with aset/`aget` just like byte arrays in Clojure

p-himik18:02:41

I should've been more clear - by their API I meant the fields/methods accessible via interop. Java arrays should have things that all Java objects have. Same for JS objects, but those methods are different, plus UInt8Array has a whole bunch of other methods. It might or might not matter - impossible to say without the context.

Zayn Malik17:02:09

Hello everyone! How can I use "export default" in cljs?

thheller17:02:04

(:require ["lib-with-default$default" :as x]) and then x in the code. assuming you mean using a npm library with a default export.

Zayn Malik17:02:06

No I actually want to export a map from a file as default and then import it in another file.

Zayn Malik17:02:46

I have come to know that I can ^:export to export but how to do it as default

p-himik17:02:39

So the file with the map is a CLJS file and the file that needs that map is a JS file?

Zayn Malik17:02:48

nope both of the files are cljs files? please tell me if there is a workaround too

p-himik17:02:39

There's no workaround - your mindset is simply wrong about the problem. Exporting as default is a JS construct, it doesn't exist in CLJS. If you have something like (def my-map {...}) in namespace a and you need that map in namespace b then all you have to do is add [a :refer [my-map]] to the :require list of the (ns ...) form in the namespace b. To avoid polluting namespace b with all the new names that you require, you can require namespaces with aliases or with only their names. So if you require [some.long.namespace.name], then you can use . And if you require [some.long.namespace.name :as n], then you can use n/my-map.

Zayn Malik17:02:49

oh thank you very much, I am actually translating a react app and became a perfectionist in translating each and every construct.

p-himik17:02:31

That's alright, but I would retarget that perfectionism. :) From "it must be 100% as the original" to "it must behave in the same exact manner but be written the CLJS way". Otherwise, you'll be writing JS in CLJS, and that's a painful road.

1
🙌 1
Aleed20:02:14

what are the considerations for using fetch versus xhrlo for http requests? most cljs http libraries I could find seem to use the latter, but seems like fetch would be simpler to use outright unless there’s extra benefits I’ve yet to read into

p-himik21:02:59

fetch is a more recent API. Pretty sure that's the reason.

👍 1
1