Fork me on GitHub
#clojurescript
<
2017-10-27
>
rauh06:10:30

@jiyinyiyong 1. Generate a let in your macro or you'll duplicate all your passed props code many times and possibly suffer from multiple execution of code that you wouldn't expect

rauh06:10:43

2. (macroexpand '($ ....)) to see what the macro does

Jon06:10:57

@rauh but how about the error?

rauh06:10:42

@jiyinyiyong Well first post the result of the macroexpand. Also, name your anonymous functions that you see in the stacktrace

Jon06:10:26

foo.core$macros=> (defmacro $ [tag props & children]
             #_=>   `(let [props# ~props
             #_=>          attrs# (respo.util.list/pick-attrs props#)
             #_=>          styles# (if (contains? props# :style) (sort-by first (:style props#)) (list))
             #_=>          event# (or (:on props#) (:event props#) {})
             #_=>          child-map# (respo.util.list/arrange-children-old ~children)]
             #_=>      {:name ~tag,
             #_=>       :coord nil,
             #_=>       :attrs attrs#,
             #_=>       :style styles#,
             #_=>       :event event#,
             #_=>       :children child-map#}))
#'foo.core$macros/$
foo.core$macros=> (macroexpand '(foo.core/$ :div {} []))
(let*
 [props__41__auto__
  {}
  attrs__42__auto__
  (respo.util.list/pick-attrs props__41__auto__)
  styles__43__auto__
  (if
   (cljs.core/contains? props__41__auto__ :style)
   (cljs.core/sort-by cljs.core/first (:style props__41__auto__))
   (cljs.core/list))
  event__44__auto__
  (cljs.core/or (:on props__41__auto__) (:event props__41__auto__) {})
  child-map__45__auto__
  (respo.util.list/arrange-children-old ([]))]
 {:name :div,
  :coord nil,
  :attrs attrs__42__auto__,
  :style styles__43__auto__,
  :event event__44__auto__,
  :children child-map__45__auto__})
foo.core$macros=>

Jon06:10:41

@rauh where is the anonymous function?

Jon07:10:25

@thheller do you have any idea on this error?

vemv08:10:06

Hi! I have 2 tooling needs: - Anyone using a robust/trustworthy code coverage reporter for clojurescript? - Say I want to move (def x ...) from ns A to ns B, how do you do it automatically (due to x being used in many consumers ns's)? https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-move-form seems broken for cljs. Does Cursive or others work well for this?

thheller08:10:49

@jiyinyiyong that doesn’t look correct (respo.util.list/arrange-children-old ([])), thats the arity 0 error since you are calling a vector with zero args

Jon08:10:43

it was added by the macro itself?

Jon08:10:59

I'm going to try identity.... still throwing error.

Jon08:10:22

@thheller thanks, fixed child-map# (respo.util.list/arrange-children-old (list ~@children))]

Jon08:10:20

in macro a sequence is turned into a function application...

vinai11:10:30

Can someone here share experience on connecting to a figwheel repl with the intellij plugin Clojure-Kit?

zalky13:10:10

Hi all: trying some more examples with code splitting, specifically the example António Monteiro shows in his clojuere/conj talk: https://youtu.be/63K--ctvT-g?t=937 TLDR, it involves splitting common vendor libraries, like reagent, into their own module. Here is a very simple gist that implments the example: https://gist.github.com/zalky/47dd7ee21f713b532fcb45093956d7c1 While the example works in the browser, I end up with no js in app.js, no js in vendor.js, and a bit of js in data.js implementing the vars in data.cljs. Presumably much of the missing js ended up in cljs_base.js. I would have expected some js in app.js implementing the vars in app.cljs, and based on the talk and the Clojurescript documentation, I would have also expected reagent.core, and it's dependencies, like cljsjs.react, to be in vendor.js, which is empty. (Using cljs 1.9.946). Clearly I'm missing something. Thoughts?

pesterhazy13:10:32

Yeah but don't I have to use js/goog.global.location.hash then? With the result of having to be careful about the name goog?

pesterhazy13:10:29

or is plain goog.global.location.hash or goog/global.location.hash acceptable?

pesterhazy13:10:55

I know both of those forms work in practice, but not sure if they're frowned upon

mfikes15:10:20

@pesterhazy Curious if (.-hash js/location) works.

juhoteperi16:10:33

It works, and so does js/location.hash

tungsten16:10:12

Does anyone know how to get http headers out of a request with cljs-ajax? Or do I have to find another library?

fbielejec22:10:47

first you need to whitelist the headers server-side, for example:

(defn wrap-expose-headers
  [handler]
  (fn [request]
    (let [response (handler request)]
      (assoc-in response [:headers "Access-Control-Expose-Headers"] "Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Content-Length,Date,Server"))))

fbielejec22:10:51

Then in a handler:

(defn response-handler [[ok xhrio]]
  (if ok (let [response (.getResponse xhrio)
               headers (.getAllResponseHeaders xhrio)]
           (re-frame/dispatch [:response-convert {:response response :headers headers}]))
      (let [error-response (.getResponse (:response xhrio))
            headers (.getAllResponseHeaders (:response xhrio))]
        (prn headers))))

(ajax.core/ajax-request
  {:uri "/enpoint"
   :method (:method request)
   :headers (:headers request)
   :params (:body request)
   :format (text-request-format)
   :response-format {:read identity :description "raw"}
   :handler response-handler})

fbielejec22:10:36

where

(def request {some-request-map})

pesterhazy16:10:27

in such a context, the let binding will shadow js/location, but not goog/global.location.hash

mfikes16:10:36

Wow. Ouch.

bronsa16:10:28

yeah the lack of munging for let locals has caused many pains

mfikes16:10:49

It will evidently munge a local for something like (let [cljs 3] (println "hi")))

mfikes16:10:20

I get var cljs__$1 = (3); in that case.

bronsa16:10:36

(fn [_] ....< many lines later> (js/_.debounce ..)) was a fun one to debug

bronsa16:10:50

(lodash uses _)

mfikes16:10:30

Ahh. The compiler will emit a warning if you have (fn [_] (.-debounce js/_)) Hrm.

mfikes16:10:41

WARNING: js/_ is shadowed by a local at line 1

bronsa16:10:00

it definitely didn't when I was debugging that :P

mfikes16:10:18

Perhaps an argument for not using js/foo.bar constructs. Hmm.

bronsa16:10:44

yeah I never particularly liked that syntax (I've always felt like it worked by accident)

pesterhazy16:10:04

to me it's similar to js* - kind of rough around the edges

mfikes16:10:25

IMHO, it feels like if you are using dots in your symbols, you might be essentially embedding JavaScript in your ClojureScript.

mfikes16:10:06

(In particular if the dots are in the name part of the symbol.)

mfikes16:10:14

I think it is probably easier to see this if you don't know JavaScript. 🙂

mfikes16:10:20

Sane:

cljs.user=> (defn foo []
  (let [location 12345] (prn (.-hash js/location))))
WARNING: js/location is shadowed by a local at line 2 <cljs repl>

pesterhazy16:10:02

the compiler warning is definitely super helpful

pesterhazy16:10:52

doesn't always work though:

cljs.user=> (do (set! js/global.location #js{:hash "correct"}) (let [location #js{:hash "incorrect"}] (prn js/location.hash)))
"incorrect"

pesterhazy16:10:10

(tested in lumo)

pesterhazy16:10:18

same in planck 2.8.1

mfikes17:10:35

if you instead write that as

(do (set! (.-location js/global) #js{:hash "correct"}) (let [location #js{:hash "incorrect"}] (prn (.-hash js/location))))
you get the same JavaScript, along with a nice warning about shadowing

sandbags20:10:46

I have a CLJS source file containing a map, part of which I would like to translate into a DOT file for drawing with GraphViz. I wrote a function to translate the map that I can call from the Figwheel repl only I just realised I don't have spit or anything to actually write the DOT syntax onto disk. Or do I? Did I miss a way to write files?

sandbags20:10:11

at a pinch I suppose I can copy&paste but it would be nice not to have to

phronmophobic20:10:49

if the cljs ends up running as js in a browser, then there’s not an exact equivalent to spit

phronmophobic20:10:59

unless you use something like node webkit to run things

phronmophobic20:10:12

or you have the cljs end up running in a node environment

phronmophobic20:10:05

if you are running the cljs as js in the browser, you can try the html5 file apis

sandbags20:10:11

i'm running it from the Figwheel REPL

sandbags20:10:27

sounds like it's probably easiest for me to just copy & paste

thheller20:10:25

@sandbags you could try the node-repl instead

sandbags20:10:03

thanks for the suggestions, i was just hoping i'd missed some supported alternative to spit. I'm not doing this so often than C&P is a terrible solution

sandbags20:10:07

thanks anyway

noisesmith21:10:00

one option is to generate a download prompt for the file

mfikes21:10:18

@sandbags Sometimes macros can be used to achieve Clojure interop

mfikes21:10:19

I suppose your data is runtime data, so it might not be possible (to get a macro to spit the data out)

tomjkidd22:10:02

Using hiccup, is there a way to set a bare data attribute? [:div {:data-my-attr true}] will return <div data-my-attr=true></div> when the desire is to get <div data-my-attr></div>

tomjkidd22:10:17

^ Using sablono for this

fbielejec22:10:47

first you need to whitelist the headers server-side, for example:

(defn wrap-expose-headers
  [handler]
  (fn [request]
    (let [response (handler request)]
      (assoc-in response [:headers "Access-Control-Expose-Headers"] "Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Content-Length,Date,Server"))))