Fork me on GitHub
#clojurescript
<
2017-10-29
>
Oliver George02:10:35

Is anyone successfully using stest/instrument to stub functions on node?

Oliver George02:10:27

in clj the second print statement returns a randomly generated :ret integer.

Oliver George02:10:31

under node it doesn't

Oliver George02:10:51

actually, doesn't work in browser either.

sandbags10:10:17

@mfikes that's a good thought... in this case I think the data should be available. Do you have an example of something like this at work?

sandbags10:10:19

hrmm... just looking at a macro from Klang I think i see, enough to give it a try

sandbags11:10:23

hrmm... maybe not, seems quite an awkward trick to pull off

mfikes11:10:00

@sandbags It is easier to see how you can slurp something at compile time using a macro, but the problem with spit in

(defmacro my-spit [content] (spit "foo" content))
is that content would need to be available at compile time, not runtime.

sandbags11:10:57

yes i had some problems with having to move stuff around, CLJC not working right, finally pulled it all into one file and then realised my data structure does have some dynamic elements in it that i'd forgotten about

mfikes12:10:02

In other words, macros can be used as compile-time side effects to read data in during compilation.

sandbags12:10:13

was a good thought though, thanks

mfikes12:10:58

Copy-n-paste is a very nice option that we all have, presuming the data is small enough 🙂

sandbags12:10:18

yes, it's getting a little unwieldy but it works ... irritating though

sandbags12:10:58

i wonder if i could just parse the source somehow, the bits of the map i want are static

sandbags12:10:19

this seems like it should be incredibly easy and i'm wondering if i am missing the blindingly obvious

sandbags12:10:56

ahh... let me put down my yak shaving knife a second

andrea.crotti12:10:22

does anyone uses clostache together with Clojurescript and figwheel?

andrea.crotti12:10:51

I just try to add clostache to my api.clj and Ring still works perfectly fine, but it breaks figwheel

andrea.crotti12:10:18

(defn render-homepage
  [language]
  (clostache/render-resource
   "public/index.moustache"
   (merge
    ENV
    {:language (str language)}
    (get TEXT language))))

(def home
  (-> (resp/response (render-homepage :en))
      (resp/content-type "text/html")))

andrea.crotti12:10:00

the code is just that really and if I evaluate in the REPL it seems perfectly fine, and in plus it works connecting to port 3000

andrea.crotti12:10:34

damn I think I understood why, figwheel doesn't go through Ring at all when serving the request

andrea.crotti12:10:15

so I would have to render the file manually first to make sure it's available

mfikes13:10:38

@sandbags It is possible to hack the ClojureScript REPL to essentially dump an evaluated form's value to a file. Example:

(require '[cljs.repl :as repl])
(require '[cljs.repl.node :as node])
(let [dump? (atom false)
      my-print (fn [s]
                 (swap! dump? (fn [b] (when b (spit "/tmp/foo.txt" s)) false))
                 (when (= s ":dump") (reset! dump? true))
                 (println s))]
  (repl/repl (node/repl-env) :print my-print))
With this hack, if you evaluate the form :dump, then the value of the next form is written into /tmp/foo.txt.

mfikes13:10:03

In short, this is just defining a custom :print option that detects when you want to dump.

sandbags13:10:14

@mfikes i don't think works using the default figwheel repl

mfikes13:10:37

You'd have to revise your Figwheel startup to define a custom :print

mfikes13:10:55

(Dunno if it excludes you from being able to do that or not.)

sandbags13:10:10

The default REPL isn't node based I don't think.

mfikes13:10:30

The above is not specific to Node

mfikes13:10:04

(The ability to specify what function to be used for printing via :print is a generic ClojureScript REPL option.)

sandbags13:10:33

right, but if i had a function equivalent to spit in my REPL I could just call it directly

mfikes13:10:51

The spit above is the Clojure spit.

sandbags13:10:01

it's the lack of that function that is the problem... apparently i'd have to go to a node-repl (or some such) to get one

mfikes13:10:12

(All of that code is Clojure; it is the code being used to start the REPL.)

mfikes13:10:09

So, in theory, if the Clojure call that is used to start Figwheel is tweaked with a custom :print, then you can hook into it and optionally also dump the evaluated result to a file

mfikes13:10:58

At a high level, the trick is that Figwheel is really a Clojure process, and if you can hook into its read-eval-print loop, you can inject a spit call into it.

sandbags13:10:00

It's clever, thanks, but a step or two beyond how much messing about I want to do to avoid c&p

mfikes13:10:32

Yeah, I'd only resort to that if the value won't fit in the copy buffer. If it fits, copy and paste is much simpler.

andrea.crotti13:10:01

what's the usual way to pass variables set from Clojure to Clojurescript?

andrea.crotti13:10:33

for example I want to try to detect the language from the HTTP request

mingp13:10:33

Can you clarify what you mean by that or what you are more directly trying to do?

andrea.crotti13:10:57

and then have the language also available in clojurescript to set up the reframe db with that

andrea.crotti13:10:30

the only place I see where maybe I could do that is

(defn ^:export init []
  (re-frame/dispatch-sync [:initialize-db])
  (dev-setup)
  (mount-root))

andrea.crotti13:10:50

but still not too clear on how I would do that

mingp13:10:52

And you've already decided against, for example, exposing this as an API and having the client app call the API?

andrea.crotti13:10:46

not necessarily but not sure it would make any sense @yiuming

andrea.crotti13:10:55

the language I want to serve could change for every request

andrea.crotti13:10:06

(it's just 2 languages really)

andrea.crotti13:10:04

I mean I'm also going to append it to the url

andrea.crotti13:10:18

so could maybe just simply read it from there from both clojure and clojurescript

andrea.crotti13:10:24

I was just wondering if there was another way

andrea.crotti13:10:49

and even if I do read it from the url I still need to read in the initialize-db step

andrea.crotti14:10:45

so I think I can even simply access js/window.location.pathname from initialize-db (which however would maybe not work inside figwheel)

andrea.crotti14:10:16

it just sounds like a bad idea and I thought there must be something smarter to do

mingp14:10:37

@andrea.crotti I don't know the full story of what you're trying to do, so I can't say whether or not this is a good idea. But, supposing you wanted to go this route, one option I've used in the past is: (1) Server side code generates a block of JS along the lines of window.{{name}} = {{JS literal representation of data}} into the page source, which is run on page load. (2) Client side code, on initialization, reads that variable from window. Note that, because you won't have access to the ClojureScript runtime at that moment, this should be regular JS.

mingp14:10:28

@andrea.crotti Another option, similar but avoids constructing and evaluating JS, is HTML data attributes (https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes).

mingp14:10:04

(Note that neither of these two options is Clojure or ClojureScript specific. The same strategy works for any backend language + JS or any compile to JS frontend language.)

andrea.crotti14:10:22

yes ok sounds good I can try option 1)

andrea.crotti14:10:09

and well I'm trying to do something which I think it's quite common, since some of the information that might influence what language to serve is lost by the time you get to clojurescript

andrea.crotti14:10:30

I just wanted to decide on what language to serve in the server but I still need that in Clojurescript somehow

qqq17:10:40

(. js/console log (= \c "c")) currenty returns true. Is it guaranteed to always return true ?

thheller17:10:34

yes, there is no char type in javascript. chars are always one-char strings.

wontheone121:10:57

Hi would anyone so kind to offer me help? I try to run a dummy test with lein-doo in my project and could not get it work for so many hours already

noisesmith21:10:31

where is the file containing that namespace?

wontheone121:10:10

HI, its "test/cljs/smart_portfolio/test_runner.clj"

wontheone121:10:15

also only one dummy file in the same directory

wontheone121:10:12

Ah sorry, 😞 after so many hours I found out my file was clj, not cljs... it works now

noisesmith21:10:50

aha, I should have seen that from what you said ... you showed the .clj file name

wontheone121:10:33

no problem, it's hard to catch because it s easy to think the problem is anywhere else than that 😄 thank you for your time anyway