Hello all, very new to clj and cljs. have been trying to get a good cljs environment set up in vscode using calva, but cant seem to connect to a repl. I want to be able to create small scripts using cljs, so want to target node and be able to use npm packages. I have figured out how to interact with npm packages and how to compile the code, but cant seem to get a good environment set up
if anyone has went through this process before and can help, all the help is much appreciated
maybe also try #vscode
shadow-cljs and Calva are friends. Of that's not an option I think Clover might work.
does anybody have any tips for memoizing the result of a function that wraps a core.async go block in clojurescript?
go
returns a channel, so why don't you extract whatever you're doing inside the go block into its own function, and memoize that function instead?
one of the functions is a low level js call which returns a promise and i can't seem to get promises with <p!
to memoize.
i set up a small experiment like this:
(defonce mem-p (fn p [x]
(js/Promise. (fn [res rej]
(js/setTimeout #(res x) 1000)))))
(go
(print "starting")
(print (<p! (mem-p "x"))))
however it always takes one second to returnHello, does anyone have any experience with trying to write Office addins using the https://docs.microsoft.com/en-us/office/dev/add-ins/reference/javascript-api-for-office in Clojurescript?
defonce
does not memoize the function, it just prevents defining it again when it is already defined when you reload the namespace.
Here you need custom memoization, something like:
(def cache (atom {}))
(defn mem-p [x]
(if (contains? @cache x)
(js/Promise.resolve (@cache x))
(-> (p x)
(.then
(fn [val]
(swap! cache assoc x val)
val)))))
thanks @posobin!
> defonce
does not memoize the function
wow, somehow i forgot to actually wrap that in memoize
🤦
when i wrap the fn in memoize is starts be behave as expected:
(defonce mem-p (memoize (fn p [x]
(js/Promise. (fn [res rej]
(js/setTimeout #(res x) 1000))))))
sorry for the noise!i used wisp to write an office add-in so i may be able to help depending on what the issue is (wisp is not clojurescript but is clojure-like and shares some similarities)
the office api makes heavy use of promises so i imagine the new <p!
from core.async would come in very handy
Hmm, this will store the resolved promise, didn't expect this to work, but makes sense.
does anybody understand how the following deftest
(defined in a .clj file) is being run in this example project? the readme is woefully incomplete on this aspect of it.
https://github.com/oliyh/kamera/blob/master/example/test/example/kamera_test.clj#L5
I tried following this setup and my cljs test environment (w/ figwheel main & tools.deps) just ignores that file/test
@johanatan my guess would be lein test
?
@thheller yep, that’s my guess as well. But does this run both the cljs tests and the clj one?
In my case I already have a bunch of deftests that are running in the CLJS context
looks like CLJ is driving the CLJS tests in this case?
I mean isn't that was the library is about?
Not entirely no. This library does what it does. It doesn’t “drive” all of your “CLJS tests”
https://github.com/oliyh/kamera/blob/master/example/test/example/kamera_test.clj#L15 this looks to me like it does but I don't know the lib so no clue
@thheller that drives the devcards tests yes
The question is about how to integrate that into an existing CLJS test infrastructure
I might be missing the point of the library but it looks to me like that IS the test infrastructure. you define devcards, it takes screenshots and compares results.
That is one aspect of a testing infrastructure
It is not the totality
hi, i`m trying to set-up react-dropzone, but without success; can someone point me in the right direction with this one ? https://react-dropzone.js.org/ :
import React from 'react'
import Dropzone from 'react-dropzone'
<Dropzone onDrop={acceptedFiles => console.log(acceptedFiles)}>
{({getRootProps, getInputProps}) => (
<section>
<div {...getRootProps()}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
</section>
)}
</Dropzone>
my code in clojurescript:
(defn MyDropzone
[]
(let [onDrop (react/useCallback (fn [acceptedFiles] (js/console.log acceptedFiles)))
props (useDropzone (clj->js {"onDrop" onDrop}))
get-root-props (.-getRootProps props)
get-input-props (.-getInputProps props)]
[:> react/Dropzone [get-root-props get-input-props]
{:on-drop #(js/console.log (-> %))}
[:section
[:div (get-root-props)
[:input (get-input-props)]
[:p "Drag n' drop some files here"]]]]))errors:
main.js:1673 Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See <https://fb.me/react-invalid-hook-call> for tips about how to debug and fix this problem.
at invariant (<http://localhost:3000/js/cljs-runtime/module$node_modules$react_dom$cjs$react_dom_development.js:1:441>)
at Object.throwInvalidHookError (<http://localhost:3000/js/cljs-runtime/module$node_modules$react_dom$cjs$react_dom_development.js:211:57>)
at Object.useCallback (<http://localhost:3000/js/cljs-runtime/module$node_modules$react$cjs$react_development.js:54:274>)
at cmp.netdava$homebank$app$converter$MyDropzone [as reagentRender] (<http://localhost:3000/js/cljs-runtime/netdava.homebank.app.converter.js:196:46>)
at eval (<http://localhost:3000/js/cljs-runtime/reagent.impl.component.js:129:10>)
at Object.reagent$impl$component$wrap_render [as wrap_render] (<http://localhost:3000/js/cljs-runtime/reagent.impl.component.js:152:3>)
at Object.reagent$impl$component$do_render [as do_render] (<http://localhost:3000/js/cljs-runtime/reagent.impl.component.js:219:38>)
at eval (<http://localhost:3000/js/cljs-runtime/reagent.impl.component.js:244:31>)
at Object.reagent$ratom$in_context [as in_context] (<http://localhost:3000/js/cljs-runtime/reagent.ratom.js:60:85>)
at Object.reagent$ratom$deref_capture [as deref_capture] (<http://localhost:3000/js/cljs-runtime/reagent.ratom.js:77:25>)
react/Dropzone
doesn't look like react-dropzone/Dropzone
.
By default, Reagent generates class components. More details on how to overcome this are here and in the next section: https://github.com/reagent-project/reagent/blob/master/doc/ReactFeatures.md#function-components
@thheller even in that example project there are both types of tests: https://github.com/oliyh/kamera/blob/master/example/test/example/test_runner.cljs
“run-tests-async” runs CLJS tests
@johanatan I don't have a clue. you asked how the deftest
would be run and it looks like lein test
. I don't know anything about the rest.
thank you
I suppose the test.cljs.edn
and figwheel come into play somehow
Yes I asked that because the normal way this works is for the one I linked to run. Therefore there is a question about what mechanism precisely allows both varieties of these tests to work. Particularly the “abnormal” one.
It is a valid question whether you see it or accept it or not.
If you “don’t know anything about the rest” then perhaps waiting for someone who does to chime in would be a good tack?
ok, goodbye. sorry I tried answering your initial question.
No worries. I appreciate the attempt
That was rude!
i've found the answer: I need a separate (pure Clojure) "main" and to manually call "clojure.test/run-all-tests" (which leiningen is automagically doing).