Fork me on GitHub
#clojurescript
<
2021-02-07
>
येसुदीप मंगलापिल्ली Yesudeep Mangalapilly03:02:22

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 Andersson10:02:58

I'm looking for a memoize last function

zendevil.eth11:02:32

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.eth11:02:45

It uses the useElements function.

zendevil.eth12:02:09

But I get the error:

zendevil.eth12:02:12

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.eth12:02:29

How to call the hook correctly?

p-himik12:02:48

BTW a better place for this question would be #reagent

zendevil.eth12:02:27

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-himik12:02:19

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-himik12:02:34

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

p-himik12:02:03

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-himik12:02:59

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.eth16:02:03

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.eth16:02:13

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

p-himik16:02:07

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

zendevil.eth17:02:42

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?

Corin (CWStra)19:02:01

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?

dpsutton19:02:32

/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=>

dpsutton19:02:58

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

Corin (CWStra)19:02:21

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

Corin (CWStra)19:02:08

Could it be a version problem with test.check?

Corin (CWStra)19:02:23

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.

dpsutton19:02:57

ok i can recreate this with shadow

dpsutton19:02:27

are you using CIDER?

dpsutton19:02:41

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

dpsutton19:02:10

shadow-cljs - server version: 2.9.10 running at 
shadow-cljs - nREPL server started on port 62853
cljs.user=> (require '[clojure.spec.test.alpha :as spec.test])
nil
cljs.user=> (spec.test/check)
Error: Require clojure.test.check and clojure.test.check.properties before calling check.
    at <eval>:3:25
    at cljsEval (<eval>: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=> (require 'clojure.test.check 'clojure.test.check.properties)
nil
cljs.user=> (spec.test/check)
[]
cljs.user=>

Corin (CWStra)19:02:49

I am using CIDER.

dpsutton20:02:10

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)20:02:54

Will do. Glad to know I wasn't going crazy. 😛

dpsutton20:02:31

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

Corin (CWStra)20:02:54

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

Corin (CWStra)20:02:55

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

dpsutton20:02:10

oh i was able to run things just fine from shadow

Corin (CWStra)20:02:27

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

dpsutton20:02:47

/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 
shadow-cljs - nREPL server started on port 62853
cljs.user=> (require '[clojure.spec.test.alpha :as spec.test])
nil
cljs.user=> (spec.test/check)
Error: Require clojure.test.check and clojure.test.check.properties before calling check.
    at <eval>:3:25
    at cljsEval (<eval>: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=> (require 'clojure.test.check 'clojure.test.check.properties)
nil
cljs.user=> (spec.test/check)
[]

Corin (CWStra)20:02:26

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

Corin (CWStra)20:02:56

How appropriate that I'm trying to shrink a breaking case for something that does generative testing. 😛

dpsutton20:02:25

that's a great observation 🙂

Corin (CWStra)20:02:42

Think I got it. Making a repo.

dpsutton20:02:26

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

Corin (CWStra)20:02:46

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

Corin (CWStra)20:02:35

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

dpsutton20:02:36

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

Corin (CWStra)20:02:29

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.

dpsutton20:02:30

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

Corin (CWStra)21:02:13

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)21:02:16

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

dpsutton21:02:04

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

Corin (CWStra)22:02:25

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

Milan Munzar19:02:42

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!

Milan Munzar20:02:54

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