This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-29
Channels
- # aws (2)
- # bangalore-clj (2)
- # beginners (36)
- # boot (10)
- # cider (9)
- # cljs-dev (19)
- # clojure (47)
- # clojure-russia (4)
- # clojure-spec (18)
- # clojure-uk (4)
- # clojurescript (71)
- # core-async (20)
- # core-logic (2)
- # css (3)
- # cursive (5)
- # data-science (15)
- # datomic (7)
- # emacs (13)
- # figwheel (4)
- # klipse (1)
- # luminus (5)
- # lumo (1)
- # off-topic (33)
- # re-frame (17)
- # shadow-cljs (1)
- # spacemacs (5)
- # specter (21)
- # unrepl (1)
- # vim (7)
Is anyone successfully using stest/instrument to stub functions on node?
Here's my simple test case https://gist.github.com/olivergeorge/0e9e02eafdc2da62ca781c38d32d3bae
in clj the second print statement returns a randomly generated :ret integer.
under node it doesn't
actually, doesn't work in browser either.
@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?
@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.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
In other words, macros can be used as compile-time side effects to read data in during compilation.
Copy-n-paste is a very nice option that we all have, presuming the data is small enough 🙂
i wonder if i could just parse the source somehow, the bits of the map i want are static
this seems like it should be incredibly easy and i'm wondering if i am missing the blindingly obvious
does anyone uses clostache
together with Clojurescript and figwheel?
I just try to add clostache to my api.clj
and Ring
still works perfectly fine, but it breaks figwheel
(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")))
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
damn I think I understood why, figwheel doesn't go through Ring at all when serving the request
so I would have to render the file manually first to make sure it's available
@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
.In short, this is just defining a custom :print
option that detects when you want to dump.
(The ability to specify what function to be used for printing via :print
is a generic ClojureScript REPL option.)
right, but if i had a function equivalent to spit
in my REPL I could just call it directly
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
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
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.
It's clever, thanks, but a step or two beyond how much messing about I want to do to avoid c&p
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.
what's the usual way to pass variables set from Clojure to Clojurescript?
for example I want to try to detect the language from the HTTP request
and then have the language also available in clojurescript to set up the reframe db with that
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))
but still not too clear on how I would do that
And you've already decided against, for example, exposing this as an API and having the client app call the API?
not necessarily but not sure it would make any sense @yiuming
the language I want to serve could change for every request
(it's just 2 languages really)
I mean I'm also going to append it to the url
so could maybe just simply read it from there from both clojure and clojurescript
I was just wondering if there was another way
and even if I do read it from the url I still need to read in the initialize-db
step
so I think I can even simply access js/window.location.pathname
from initialize-db
(which however would maybe not work inside figwheel)
it just sounds like a bad idea and I thought there must be something smarter to do
@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.
@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).
(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.)
yes ok sounds good I can try option 1)
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
I just wanted to decide on what language to serve in the server but I still need that in Clojurescript somehow
(. js/console log (= \c "c"))
currenty returns true. Is it guaranteed to always return true ?
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
where is the file containing that namespace?
HI, its "test/cljs/smart_portfolio/test_runner.clj"
also only one dummy file in the same directory
Ah sorry, 😞 after so many hours I found out my file was clj, not cljs... it works now
aha, I should have seen that from what you said ... you showed the .clj file name
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