clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
khargawsh 2021-02-07T03:34:22.022900Z

hi. i'm currently working on Bazel integration for clojurescript and have been looking at the compiler options. would it be possible to expose several of the --compile-opts as regular command line options as well?

Fredrik Andersson 2021-02-07T10:06:58.024100Z

I'm looking for a memoize last function

Fredrik Andersson 2021-02-07T11:24:13.025900Z

Thanks again @p-himik!

πŸ‘ 1
zendevil 2021-02-07T11:59:32.026900Z

I’m trying to use hooks in a react app like so:

(let [elements (useElements)] (if @s/user
             [:> div
              (e/load-stripe elements)
              [:> div (tw [:flex-row])
               [:> CardElement {:options (clj->js {:style {:base
                                                           {:fontSize "16px"
                                                            :color "#424770"
                                                            ;;:width "30em"
                                                            }
                                                           :invalid {:color "#9e2146"}}})}]
               [:button (tw style/button [:bg-green-700
                                          :text-white
                                          :text-sm]
                            {:on-click #(dispatch [:purchase-plan])}) "Purchase Plan"]
               ]
              ]
             [:button (tw style/button [:text-base :bg-green-700
                                        :text-white :m-0
                                        :px-6
                                        :py-5
                                        ]
                          {:on-click #(dispatch [:login])})
              "Subscribe to Exclusive Content"
              ]))

zendevil 2021-02-07T11:59:45.027200Z

It uses the useElements function.

zendevil 2021-02-07T12:00:09.027400Z

But I get the error:

zendevil 2021-02-07T12:00:12.027700Z

react-dom.development.js:14747 Uncaught Error: 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

zendevil 2021-02-07T12:00:29.028100Z

How to call the hook correctly?

p-himik 2021-02-07T12:06:48.028500Z

BTW a better place for this question would be #reagent

zendevil 2021-02-07T12:12:21.028700Z

thanks

zendevil 2021-02-07T12:42:27.029900Z

I’m converting the following code to cljs:

const {error, paymentMethod} = await stripe.createPaymentMethod({
      type: 'card',
      card: cardElement,
    });
Here’s the cljs:
(go
                (prn "stripe is " stripe)
                (let [[error payment-method]
                      (<! (.createPaymentMethod stripe {:type "card" :card card-element}))]
                  (prn "error is " error)
                  (js/console.log "payment method is " payment-method)))
But am getting the error:
IntegrationError: Invalid value for createPaymentMethod: type should be string. You specified: undefined.
How to fix this?

p-himik 2021-02-07T12:51:19.030Z

createPaymentMethod expects a JS object but you're giving it a CLJS map. If card-element is some JS value, then it can be fixed by just adding #js in front of the map literal.

p-himik 2021-02-07T12:52:34.030200Z

Also, I don't think that go and <! work with JS functions that return promises.

p-himik 2021-02-07T12:53:03.030400Z

There are many ways to work with async JS functions in CLJS, one of them would be to just use the promise API via interop.

p-himik 2021-02-07T12:53:59.030600Z

Finally, you're trying to destructure a JS object as if it were an array. It won't work - you will have to use JS interop for that.

zendevil 2021-02-07T16:34:03.031100Z

after adding the #js in front of the map, I’m getting the following error:

IntegrationError: Please use the same instance of `Stripe` you used to create this Element to create your Source or Token.

zendevil 2021-02-07T16:34:13.031300Z

even though I have a single stripe object that I’m using

p-himik 2021-02-07T16:36:07.031500Z

No idea, I don't have clue what "Stripe" is or how it works and why.

zendevil 2021-02-07T17:51:42.032800Z

I have the following code for Stripe integration:

(if card-element
              (go
                (prn "stripe is " stripe)
                (let [error-payment-method
                      (<! (.createPaymentMethod stripe #js {:type "card" :card card-element}))]
                  (prn "error is " error-payment-method)
                  ))
              (prn "card element is nil")
              )
But this gives me the error
IntegrationError: Please use the same instance of `Stripe` you used to create this Element to create your Source or Token.
How to fix this error?

zendevil 2021-02-07T17:52:43.032900Z

http://stripe.com

Corin (CWStra) 2021-02-07T19:20:01.035100Z

Realized my question in #beginners might not be very beginner-y, and it's been a day, so: I'm trying to use clojure.spec.test.alpha/check in a cljs repl, but I constantly get an error about needing to require clojure.test.check and clojure.test.check.properties before calling it, even though I did that. Is there a way to make sure the namespaces are properly imported?

dpsutton 2021-02-07T19:29:32.035200Z

/tmp ❯❯❯ clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "RELEASE"} org.clojure/test.check {:mvn/version "RELEASE"}}}' -M -m cljs.main -re node -r
ClojureScript 1.10.773
cljs.user=> (require '[clojure.spec.test.alpha :as spec.test])
nil
cljs.user=> (spec.test/check)
Execution error (Error) at (<cljs repl>:1).
Require clojure.test.check and clojure.test.check.properties before calling check.

cljs.user=> (require 'clojure.test.check 'clojure.test.check.properties)
nil
cljs.user=> (spec.test/check)
[]
cljs.user=>

dpsutton 2021-02-07T19:29:58.035400Z

it seems to work for me. what's your setup look like?

Corin (CWStra) 2021-02-07T19:37:21.035600Z

Using shadow-cljs, have [org.clojure/test.check "1.1.0"] in my :dependencies. Using spacemacs a-la http://Practical.li

Corin (CWStra) 2021-02-07T19:38:08.036200Z

Could it be a version problem with test.check?

Milan Munzar 2021-02-07T19:40:42.038100Z

Hey, πŸ™‚ I am trying to define a test support macro for a common pattern when testing with go blocks. Could somebody point me in the right direction? This is the pseudocode:

(defmacro deftest-go
  [name & body]
  `(cljs.test.deftest ~name
     (cljs.test.async done#
            (cljs.core.async/go
              ~@body
              (done#)))))
This fails due to not being able to resolve deftest macro. I have seen http://blog.fogus.me/2010/09/03/monkeying-with-clojures-deftest/) a different approach, which uses deftest implementation. Thank you!

Corin (CWStra) 2021-02-07T19:42:23.038300Z

Running your code as above in the repl for my project fails, though it complies if I call clojure at the command line directly. Weirdly, it fails at the initial (spec.test/check) , saying no such namespace could be located, even though I did that the line above.

dpsutton 2021-02-07T19:53:57.038900Z

ok i can recreate this with shadow

dpsutton 2021-02-07T19:55:27.039100Z

are you using CIDER?

dpsutton 2021-02-07T19:55:41.039300Z

i tried to recreate with just shadow-cljs on the command line and it actually succeeded

dpsutton 2021-02-07T19:56:10.039500Z

shadow-cljs - server version: 2.9.10 running at <http://localhost:9631>
shadow-cljs - nREPL server started on port 62853
cljs.user=&gt; (require '[clojure.spec.test.alpha :as spec.test])
nil
cljs.user=&gt; (spec.test/check)
Error: Require clojure.test.check and clojure.test.check.properties before calling check.
    at &lt;eval&gt;:3:25
    at cljsEval (&lt;eval&gt;:3:137)
    at global.SHADOW_NODE_EVAL ([stdin]:105:10)
    at Object.shadow$cljs$devtools$client$node$node_eval [as node_eval] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:24:1)
    at /private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:49:13
    at Object.shadow$cljs$devtools$client$env$repl_call [as repl_call] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/env.cljs:122:11)
    at Object.shadow$cljs$devtools$client$node$repl_invoke [as repl_invoke] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:47:1)
    at shadow$cljs$devtools$client$node$process_message (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:106:5)
    at /private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/env.cljs:244:9
    at Object.shadow$cljs$devtools$client$env$process_next_BANG_ [as process_next_BANG_] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/env.cljs:196:7)
cljs.user=&gt; (require 'clojure.test.check 'clojure.test.check.properties)
nil
cljs.user=&gt; (spec.test/check)
[]
cljs.user=&gt;

Corin (CWStra) 2021-02-07T19:57:49.039700Z

I am using CIDER.

dpsutton 2021-02-07T20:00:10.039900Z

yeah its not working for me in CIDER. can you file an issue on github? a minimal repo is just {:dependencies [[org.clojure/test.check "1.1.0"]]} as the shadow-cljs.edn file. npm i shadow-cljs. and then jack in clojurescript and choose the node-repl

Corin (CWStra) 2021-02-07T20:00:54.040100Z

Will do. Glad to know I wasn't going crazy. πŸ˜›

dpsutton 2021-02-07T20:07:31.040300Z

got a gist here with some more info and repros of how crazy this is

dpsutton 2021-02-07T20:07:46.041500Z

<https://gist.github.com/dpsutton/763aea2bfbc9440f7f918bc342a79727>

Corin (CWStra) 2021-02-07T20:08:54.041800Z

To be clear, I should make an issue on the CIDER repo?

Corin (CWStra) 2021-02-07T20:12:55.042Z

I ask mostly because I'm getting the same issue when running things in shadow-cljs cljs-repl on the command line

dpsutton 2021-02-07T20:27:10.042300Z

oh i was able to run things just fine from shadow

Corin (CWStra) 2021-02-07T20:27:27.042500Z

I'll see if I can get a minimal example for that.

dpsutton 2021-02-07T20:27:47.042700Z

/t/shadow ❯❯❯ cat shadow-cljs.edn
{:dependencies [[org.clojure/test.check "1.1.0"]]}
/t/shadow ❯❯❯ shadow-cljs node-repl
shadow-cljs - config: /private/tmp/shadow/shadow-cljs.edn
shadow-cljs - socket connect failed, server process dead?
shadow-cljs - updating dependencies
shadow-cljs - dependencies updated
[2021-02-07 13:54:32.385 - WARNING] TCP Port 9630 in use.
[2021-02-07 13:54:37.557 - WARNING] :shadow.cljs.devtools.server.fs-watch-hawk/hawk-start-ex
ArityException Wrong number of args (0) passed to: clojure.core/juxt
	clojure.core/apply (core.clj:665)
	clojure.core/apply (core.clj:660)
	hawk.core/watch! (core.clj:79)
	hawk.core/watch! (core.clj:59)
	shadow.cljs.devtools.server.fs-watch-hawk/start* (fs_watch_hawk.clj:42)
	shadow.cljs.devtools.server.fs-watch-hawk/start* (fs_watch_hawk.clj:30)
	shadow.cljs.devtools.server.fs-watch-hawk/start (fs_watch_hawk.clj:103)
	shadow.cljs.devtools.server.fs-watch-hawk/start (fs_watch_hawk.clj:101)
	clojure.lang.Var.invoke (Var.java:399)
	shadow.cljs.devtools.server.fs-watch/start (fs_watch.clj:26)
	shadow.cljs.devtools.server.fs-watch/start (fs_watch.clj:11)
	shadow.cljs.devtools.server/start!/fn--17712 (server.clj:407)
shadow-cljs - server version: 2.9.10 running at <http://localhost:9631>
shadow-cljs - nREPL server started on port 62853
cljs.user=&gt; (require '[clojure.spec.test.alpha :as spec.test])
nil
cljs.user=&gt; (spec.test/check)
Error: Require clojure.test.check and clojure.test.check.properties before calling check.
    at &lt;eval&gt;:3:25
    at cljsEval (&lt;eval&gt;:3:137)
    at global.SHADOW_NODE_EVAL ([stdin]:105:10)
    at Object.shadow$cljs$devtools$client$node$node_eval [as node_eval] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:24:1)
    at /private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:49:13
    at Object.shadow$cljs$devtools$client$env$repl_call [as repl_call] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/env.cljs:122:11)
    at Object.shadow$cljs$devtools$client$node$repl_invoke [as repl_invoke] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:47:1)
    at shadow$cljs$devtools$client$node$process_message (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/node.cljs:106:5)
    at /private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/env.cljs:244:9
    at Object.shadow$cljs$devtools$client$env$process_next_BANG_ [as process_next_BANG_] (/private/tmp/shadow/.shadow-cljs/builds/node-repl/dev/out/cljs-runtime/shadow/cljs/devtools/client/env.cljs:196:7)
cljs.user=&gt; (require 'clojure.test.check 'clojure.test.check.properties)
nil
cljs.user=&gt; (spec.test/check)
[]

Corin (CWStra) 2021-02-07T20:28:26.042900Z

I was running it for a particular build, so that could be the difference.

Corin (CWStra) 2021-02-07T20:30:56.043200Z

How appropriate that I'm trying to shrink a breaking case for something that does generative testing. πŸ˜›

dpsutton 2021-02-07T20:31:17.043400Z

haha

dpsutton 2021-02-07T20:31:25.043600Z

that's a great observation πŸ™‚

Corin (CWStra) 2021-02-07T20:39:42.043900Z

Think I got it. Making a repo.

dpsutton 2021-02-07T20:40:26.044100Z

i thought i already had a minimal. the dep is just test.check

Milan Munzar 2021-02-07T20:40:54.044300Z

I think I have done it. The first implementation of macro file is:

(ns commons.utils.test-support
  (:require
    [clojure.test]))


(defonce ^:dynamic
  *load-tests* true)

(defmacro deftest-go
  [name &amp; body]
  (when *load-tests*
    `(def ~(vary-meta name assoc :test `(fn []
                                          (cljs.test/async done#
                                            (cljs.core.async/go
                                              ~@body
                                              (done#)))))
          (fn [] (clojure.test.test-var (var ~name))))))

Corin (CWStra) 2021-02-07T20:41:46.044500Z

I meant a minimal to break it at the command line, not just in CIDER.

Corin (CWStra) 2021-02-07T20:42:35.044800Z

Could be breaking something else, could demonstrate my incorrect dependencies. Either way, useful to know.

dpsutton 2021-02-07T20:49:36.046Z

My example showed it not working in cider yet working on the command line. A perfect small example

Corin (CWStra) 2021-02-07T20:51:29.046200Z

Well, that comes back to where I report the problem. I thought we thought it was just a CIDER thing, but apparently, it's not. Or I've missed something obvious.

dpsutton 2021-02-07T20:53:30.046700Z

ah then i'm confusing things. i'll wait to see your bug report

Corin (CWStra) 2021-02-07T21:04:13.046900Z

https://github.com/cwstra/shadow-test-check a small shadow-cljs project. Installing the deps with npm install , then running npx shadow-cljs watch example and node output/example.js in the project root, before starting the repl with npx shadow-cljs cljs-repl example seems to replicate the behavior you found in the CIDER example.

Corin (CWStra) 2021-02-07T21:17:16.047400Z

Accidentally had an extra edn file in there. Now it should be fairly minimal, as far as I can tell.

dpsutton 2021-02-07T21:19:04.047600Z

ah. if there's no CIDER involved the bug report should go to shadow

Corin (CWStra) 2021-02-07T21:19:25.047800Z

Alrighty.

Corin (CWStra) 2021-02-07T22:08:25.048Z

Issue made. Thanks for the help; I was concerned I had missed something obvious.