clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
gekkou 2020-11-05T04:10:58.029500Z

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

gekkou 2020-11-05T04:11:39.030200Z

if anyone has went through this process before and can help, all the help is much appreciated

2020-11-05T04:49:55.030300Z

maybe also try #vscode

pez 2020-11-05T06:52:47.032800Z

shadow-cljs and Calva are friends. Of that's not an option I think Clover might work.

Chris McCormick 2020-11-05T12:08:06.034400Z

does anybody have any tips for memoizing the result of a function that wraps a core.async go block in clojurescript?

victorb 2020-11-05T12:13:49.034500Z

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?

Chris McCormick 2020-11-05T12:36:07.034800Z

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.

Chris McCormick 2020-11-05T12:36:44.035Z

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 return

Joe 2020-11-05T13:00:18.036200Z

Hello, 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?

Gleb Posobin 2020-11-05T13:23:56.036400Z

defonce does not memoize the function, it just prevents defining it again when it is already defined when you reload the namespace.

Gleb Posobin 2020-11-05T13:29:38.036600Z

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)))))

Chris McCormick 2020-11-05T13:37:23.037400Z

thanks @posobin!

Chris McCormick 2020-11-05T13:38:36.037600Z

> defonce does not memoize the function wow, somehow i forgot to actually wrap that in memoize 🤦

Chris McCormick 2020-11-05T13:39:13.037800Z

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!

Chris McCormick 2020-11-05T13:44:08.039Z

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)

Chris McCormick 2020-11-05T13:48:53.039700Z

the office api makes heavy use of promises so i imagine the new <p! from core.async would come in very handy

Gleb Posobin 2020-11-05T14:34:43.039800Z

Hmm, this will store the resolved promise, didn't expect this to work, but makes sense.

johanatan 2020-11-05T18:30:55.041900Z

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

johanatan 2020-11-05T18:31:35.042600Z

I tried following this setup and my cljs test environment (w/ figwheel main & tools.deps) just ignores that file/test

thheller 2020-11-05T19:30:53.043Z

@johanatan my guess would be lein test?

johanatan 2020-11-05T19:32:51.043800Z

@thheller yep, that’s my guess as well. But does this run both the cljs tests and the clj one?

johanatan 2020-11-05T19:33:37.044600Z

In my case I already have a bunch of deftests that are running in the CLJS context

thheller 2020-11-05T19:33:38.044700Z

looks like CLJ is driving the CLJS tests in this case?

thheller 2020-11-05T19:33:54.045Z

I mean isn't that was the library is about?

johanatan 2020-11-05T19:34:26.046200Z

Not entirely no. This library does what it does. It doesn’t “drive” all of your “CLJS tests”

thheller 2020-11-05T19:34:55.047Z

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

johanatan 2020-11-05T19:35:22.048Z

@thheller that drives the devcards tests yes

johanatan 2020-11-05T19:36:01.049100Z

The question is about how to integrate that into an existing CLJS test infrastructure

thheller 2020-11-05T19:36:56.050300Z

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.

johanatan 2020-11-05T19:37:21.051Z

That is one aspect of a testing infrastructure

johanatan 2020-11-05T19:37:24.051200Z

It is not the totality

Andrei Stan 2020-11-05T19:37:43.051400Z

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>)

p-himik 2020-11-05T19:42:25.051500Z

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

johanatan 2020-11-05T19:42:51.052400Z

@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

johanatan 2020-11-05T19:43:05.053100Z

“run-tests-async” runs CLJS tests

thheller 2020-11-05T19:43:30.053700Z

@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.

Andrei Stan 2020-11-05T19:44:06.054100Z

thank you

thheller 2020-11-05T19:44:20.054500Z

I suppose the test.cljs.edn and figwheel come into play somehow

johanatan 2020-11-05T19:48:07.056Z

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.

johanatan 2020-11-05T19:48:17.056400Z

It is a valid question whether you see it or accept it or not.

johanatan 2020-11-05T19:50:04.059Z

If you “don’t know anything about the rest” then perhaps waiting for someone who does to chime in would be a good tack?

thheller 2020-11-05T19:51:05.060Z

ok, goodbye. sorry I tried answering your initial question.

johanatan 2020-11-05T19:51:19.060300Z

No worries. I appreciate the attempt

Karol WĂłjcik 2020-11-05T20:49:56.061400Z

That was rude!

đź‘Ť 6
johanatan 2020-11-05T23:48:34.067900Z

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).