Fork me on GitHub
#clojurescript
<
2016-08-29
>
johanatan02:08:48

Hi, suppose I specify a clojurescript keyword as the first argument to history.pushState and want to deserialize it back into a keyword in my popstate handler. How would I go about doing so?

johanatan02:08:06

i.e., short of doing: (keyword (.. % -state -name)) (which is kind of cheating)

shaunlebron02:08:56

@johanatan: in general, I’d probably use pr-str and read-string

shaunlebron02:08:45

(js/history.pushState #js {:state (pr-str data)}) and then (set! js/onpopstate (fn [e] (let [data (read-string (aget event “state” “state"))])))

shaunlebron02:08:40

i wouldn’t call it cheating, just necessary interop that you just write once

wilkerlucio08:08:24

there is an issue in current CLJS regarding generating functions from specs, ex: (clojure.test.check.generators/sample (s/gen (s/fspec :args (s/cat) :ret int?))) this code works on Clojure, but errors on Clojurescript: #object[TypeError TypeError: Cannot read property 'for_all_STAR_' of undefined]

Yehonathan Sharvit12:08:26

This is the code of test:

(defn test
  "test [v] finds fn at key :test in var metadata and calls it,
  presuming failure will throw exception"
  [v]
  (let [f (.-cljs$lang$test v)]
    (if f
      (do (f) :ok)
      :no-test)))

Yehonathan Sharvit12:08:56

How do I set the code of the test into (.-cljs$lang$test v)?

mfikes12:08:24

In short :test metadata

Yehonathan Sharvit12:08:50

But it doesn’t work

Yehonathan Sharvit12:08:16

cljs.user=>(defn my-function "test" {:test #(assert false)} ([]))
cljs.user=> (test #'my-function)
:no-test

Yehonathan Sharvit12:08:03

I ran it in a fresh repl with java -jar cljs.jar -m cljs.repl.node

Yehonathan Sharvit12:08:10

I also tried in planck and in klipse

Yehonathan Sharvit12:08:44

What’s the meaning of (.-cljs$lang$test v)? It is a javascript key, right?

mfikes12:08:08

@viebel It appears deftest does the right thing, but perhaps work needs to be done to make :test work

mfikes12:08:21

@viebel Interestingly, this appears to work:

(defn my-test
   [v]
   (let [f (:test (meta v))]
   (if f
    (do (f) :ok)
    :no-test)))

Yehonathan Sharvit12:08:39

Yeah. I noticed also.

Yehonathan Sharvit12:08:15

Do you think I could submit a patch with it? Or it might break deftest?

anmonteiro12:08:10

the CLJS implementation might have to do with the fact that you can’t add metadata to vars at runtime

anmonteiro12:08:21

hence the cljs$lang$test

mfikes12:08:19

Perhaps the :test path is just a matter of properly hooking up .-cljs$lang$test

Yehonathan Sharvit13:08:31

I discovered something: with java -jar cljs.jar -m cljs.repl.node the following works:

cljs.user=> (defn my-function "test" {:test #(assert true)} ([]))
#'cljs.user/my-function
cljs.user=> (test my-function)
:ok: 

Yehonathan Sharvit13:08:57

but this one doesn’t:

cljs.user=> (test #'my-function)
:no-test

Yehonathan Sharvit13:08:25

But in planck and klipse both don’t work => :no-test

dnolen13:08:05

@viebel @mfikes this conversation is better suited for #cljs-dev

dnolen13:08:06

@wilkerlucio hrm, just seems like you haven’t loaded the test.check namespaces, if that isn’t true then file an issue

si1414:08:35

I'm wondering what do you folks use for your non-cljs build pipelines. Gulp? SASS? Postcss? Webpack? Assets story feels a bit neglected in CLJS-related posts so it would be nice to hear your stories :)

juhoteperi14:08:27

@si14 There are Less and Sass plugins both for Boot and Lein, e.g. https://github.com/Deraen/less4clj https://github.com/Deraen/sass4clj

si1414:08:07

@juhoteperi thanks! However, if I want to add e.g. autoprefixer + postcss-flexbugs-fixes to the mix, it becomes somewhat more complicated :) Same with image compressing

wilkerlucio15:08:14

@dnolen you are right, requiring more namespaces fixed the issue, thanks

xcthulhu15:08:27

@juhoteperi: It's hacky, but I always fall back to this in a pinch - https://github.com/hyPiRion/lein-shell

dnolen15:08:05

@wilkerlucio Clojure can automatically loads those namespaces we cannot - we have some code to give a hint what went wrong but it probably needs more work

xcthulhu15:08:13

@juhoteperi : Similarly, if you want to make sure stuff is installed before you try to run shell commands, I use lein-npm : https://github.com/RyanMcG/lein-npm

nickt16:08:16

hey all. How can I make a file with defs that can be read in both cljs and clj?

nickt16:08:12

googlign around seems to suggest I should use reader conditionals and a cljc file

nickt16:08:36

but my defs are just constant strings and numbers... I don't really need to differentiate between clj and cljs

potetm16:08:43

Yeah if you want to cross-compile, you still have to use cljc

dnolen16:08:45

@nickt that’s what cljc is for

dnolen16:08:54

you don’t need reader conditionals for many things

potetm16:08:54

You don't have to use the conditional to use cljc

dnolen16:08:04

especially now with automatically aliasing of typical core namespaces

nickt16:08:24

ok so if I just make a cljc file with a couple def statements I should be able to require it in both clj and cljs?

dnolen16:08:53

@nickt not quite, CLJS doesn’t support namespace-less files yet

dnolen16:08:00

so you need at least an ns form

Tim16:08:32

@dnolen clojure supports .clj files without namespaces?

Tim16:08:54

didn’t even know that, although that makes sense as project.clj doesn’t have ns nor does build.boot

dnolen16:08:17

on the list of things to enhance in ClojureScript, it’s just trickier

Tim16:08:39

I thought that each namespace became a java class at compilation, so it was necessary somehow

potetm16:08:02

Yeah I've only ever used it for one-off scripts. clojure uses that feature internally for things iirc

dnolen16:08:38

@tmtwd well there is the implicit user ns so I should probably qualify here

Tim16:08:58

ah I see, interesting

nickt16:08:57

hm ok... completely unrelated question: Is there an option I can pass to cljs.build.api/build that disables caching? Always rebuilds completely?

nickt16:08:00

also, I made a file called constants.cljc with an ns form (ns iss.constants)

nickt16:08:34

from my macros.clj file I tried (ns-resolve 'iss.constants blah) and it tells me "no namespace iss.constants found"

nickt16:08:22

ahhh yes I figured it out, nvm 🙂

si1416:08:11

@xcthulhu @juhoteperi thank you (again) :)

nickt18:08:47

Hey, quick follow up to my previous question... if there's no way to disable the cache in cljsbuild, is there a way to manually clear it?

dnolen18:08:45

@nickt :cache-analysis false should work if that’s what you’re talking about

dnolen18:08:55

if you’re talking about writing out the cached compiled stuff, no

dnolen18:08:07

you just need to delete the output directory

nickt18:08:56

ah that makes sense!