Fork me on GitHub
#clojurescript
<
2020-10-25
>
datran04:10:09

I want to add nice error messages in my cljs macro, and I've come across cljs.analyzer/error with an arglist like ([env msg]) - does anybody know what env is and how I can get it so I can pass it?

datran04:10:48

I suspect it gives file line/column information, but I'm not sure and haven't found any documentation so far

datran04:10:03

> Clojure macros provide an implicit argument &env that's a map of the local bindings available at macro-expansion time

datran04:10:13

From The Joy of Clojure__, I'll see if that works

datran05:10:46

Question about :arglists for docs. This is my macro:

(defmacro
  ^{:doc "Register re-frame db event and bind it to a symbol. Now instead of
(rf/dispatch [::event arg1 arg2])

you can use function call syntax like this:
(rf/dispatch (event arg1 arg2))

Benefits: jump-to-def and arglists work nicely.
"
    :arglists '([event-sym doc-string? [handler-params] [interceptors?] handler-body])}
  def-event-db
  [& args]
  (def-event-db* &env args))

datran05:10:29

I took the meta map from the defn source, however when I start typing (def-event-db ,,,) the arglist documentation that shows up is just ([& args]) instead of what's in the :arglists metadata.

datran05:10:48

Is there something more that I have to do to get the proper docstring associated with a macro?

danieroux19:10:55

This line, returns true under :none - but false under :advanced. I need help figuring it out: (def has-provider (some? (.-Provider (react/createContext "hmm")))) This is my smallest repro: https://github.com/danieroux/create-context-repro/blob/main/src/create_context_repro/core.cljs#L13

p-himik21:10:30

If you're using shadow-cljs, just add ^js in front of (react/createContext "hmm").

p-himik21:10:05

The issue is due to name mangling during advanced optimizations. Provider in your code becomes something else. ^js tells the optimizer to skip it.

p-himik21:10:15

Or something like that. :) I'm not an expert there.

danieroux22:10:30

Thanks @U2FRKM4TW! That works for me, on the standard ClojureScript compiler too

👍 2
leif21:10:59

I'm trying to load a clojurescript file from an ajax request using XMLHttpRequest, following this snippet: https://stackoverflow.com/questions/18126406/how-can-i-get-the-progress-of-a-downloading-script (rather than putting a script tag directly in my html). However, it seems like doing this causes clojurescript to erase the entire DOM. As such reagent fails at (.getElementById js/document "app"). I don't see this behavior if I insert a script tag with the resource directly.

leif21:10:44

Does anyone have any idea why clojurescript might be doing this? I can also upload a small project with this behavior if that helps.

leif21:10:20

Running lein figwheel shows it.

leif21:10:25

Oh, and for context, the reason I'm trying to do this is I have a fairly large (~ 7 MB) js file for my actual application, show I would like to show a 'loading bar' for the resource download, otherwise I'd just put the script tag directly in the html, which does work.

p-himik21:10:57

Seems to be an issue with having document.write() being called from within document.documentElement.appendChild(). Same for document.body.appendChild().

p-himik21:10:13

As a widely supported alternative to manually loading a document, you can try code splitting. Shadow-cljs supports it, maybe figwheel does as well.

leif21:10:22

So, I ultimately want something that can be put on something like github pages, I'm only using figwheel because it was reagent's default test environment. How hard would it to switch to shadow-cljs?

leif21:10:23

(I am using :target :bundle in my actual application for npm dependencies if that's a problem.)

leif21:10:35

err :target :bundle + webpack.

p-himik21:10:25

No idea about github pages or :target :bundle, let alone webpack, sorry. But FWIW for me switching to shadow-cljs was quite painless. Had to do some reading, sure (it has a great user's guide), but it was all a smooth ride.

p-himik21:10:32

thheller, the author of shadow-cljs, is very active in #shadow-cljs - perhaps he can give some tips if you get stuck.

leif21:10:24

Okay, I'll give it a look, thanks.

leif01:10:14

@U2FRKM4TW Hmm..it actually looks to be unrelated to fighweel, like the error persists even when I run the compiled JS from an entirely different server.

leif01:10:01

Its because if write is called after a page is loaded the document gets erased.

leif01:10:57

Which....seems to indicate that as long as the cojurescript compiler uses write it might not be possible, at least not without reconstructing the whole document within clojurescript itself.

leif01:10:20

Either way though, this seems like an interesting enough question on its own I'm going to ask it in the main thread. Thanks again for your feedback.

p-himik07:10:00

write can definitely be called after a page is loaded. If called properly, it will just replace the contents of body, but in your initial code it was replacing the whole html. At least, that's how it worked for me. CLJS should not use write with modules created by code splitting, so that might still be an option.