Fork me on GitHub
#clojurescript
<
2019-09-19
>
Ben Hammond10:09:15

I am trying to debug a rum/citrus app. https://github.com/clj-commons/citrus It queues UI events using a private function

(defn- queue-effects! [queue f]
  (vswap! queue conj f))
I would like to instrument this with an extra
(.log js/console "queue-effects! added" f)
so I can see what is going into the queue. I've tried
(with-redefs [citrus.reconciler/queue-effects! instrumented-queue-effects!]
    (citrus/reconciler {:state (atom {})
but my instrumented function doesn't seem to get called Is there an idiomatic way to do this? I'm not very familiar with the Clojurescript runtime

Ben Hammond10:09:38

I cannot just inclued

(ns citrus.reconciler)
(defn queue-effects! [q f]
  (.log js/console "queue-effects! added" f)
  (vswap! q conj f))
in my startup namespace; I get the error
base.js:305 Uncaught Error: Namespace "citrus.reconciler" already declared.
    at Object.goog.provide (base.js:305)
    at main.cljs?rel=1568887947127:50
`

niwinz10:09:57

I think that the problem is with-redef, it only applies redefinition when you are in scope of with-redef

niwinz10:09:22

try use (set! citrus.reconciler/queue-effects! <yourfnhere>)

👍 4
niwinz10:09:26

or alter-var-root

herald15:09:26

How do I do the following in clojurescript, when it doesn't have var? (basically stubbing and restoring a variable)

user=> (def x 1)
#'user/x
user=> (def hold [(var x) x])
#'user/hold
user=> (alter-var-root #'x (fn [_] 5))
5
user=> x
5
user=> (let [[variable value] hold] (alter-var-root variable (fn [_] value)))
1
user=> x
1

thheller15:09:05

in short you don't 😉

thheller15:09:19

why would you want to in the first place?

thheller15:09:04

why not just use an atom or so? why does it have to be var?

herald07:09:37

The var is an ordinary defn in my application, and I want to stub it in my tests (`with-redefs` won't work since it gets called outside its scope)

thheller09:09:33

yeah with-redefs is also problematic in async code

thheller09:09:54

what you can do is (set! that.ns/foo 123) but it really isn't recommended to do so

herald10:09:50

I've been doing that (hopefully it's okay just for testing) but I'm having problem saving that.ns/foo to an atom, so I can restore its original value.

herald10:09:07

It seems to work in Clojure where I can save (var that.ns/foo), but I can't figure out how to get the reference to that.ns/foo in clojurescript so I can set! it back to its original value in my fixtures.

thheller11:09:49

yeah thats not possible. you can however store a function if you have to? #(set! that.js/foo %)?

thheller11:09:28

well it is possible but it gets hairy quick so better avoid it

herald12:09:43

That works great for my use case!

(let [orig-fn fetch/session]
     (set! fetch/session (stub-fetch-fn "anon-token"))
     (swap! stubbed-variables conj #(set! fetch/session orig-fn)))
Then I just run all those functions in my fixtures. Not too hairy for me. Thanks!

thheller13:09:42

just be careful with async code

👌 4
norbert18:09:42

Has there been any integration with glimmerjs instead of react in cljs world?

lilactown18:09:14

@norbertpy no, not really. I think the barrier is that glimmerja requires a whole compilation toolchain itself and only works with TypeScript, so it's not very friendly to langs like CLJS building on top of it

👍 4
romain19:09:25

Instead of

(ns app.core
    (:require [lib1.namespace1 :as namespace1]
              [lib1.namespace2 :as namespace2] ... )
is it possible to require all? something like
(ns app.core
    (:require [lib1 :as lib1] ... )
(lib1/namespace1/foo ...)
?

lilactown19:09:29

you can require them without aliasing with :as, but you can’t require the top-level ns and get everything underneath it

romain19:09:33

Hm oki... I use a ui wrapper and each component is in a different namespace, I wanted to reduce the size of the :require, but it's not a big deal 🙂

athomasoriginal22:09:19

To give some confidence into the pragmatism of full requires : when your app scales it’s still searchable. I use the requires to work my way through large codebases 🙂 I actually think this is a super power of Clojure. Because the language is consistent and light on syntax it’s very easy to find things.

eskemojoe00721:09:06

I've been working forever on a pretty fun frontend. Its been working great with figwheel and the repl. I went to build it for the first time in forever (`uberjar`). I can make it compile and work but with no optimizations or any setting changes really. Even changing :pretty-print to false in :cljsbuild give me invalid hiccup errors in my console. Anyone got any clues for me?

joshkh22:09:44

i'm attempting to use @material-ui/core ^4.0.0 with shadow-cljs and reagent, however i get the following exception when attempting to use a Button component:

Uncaught TypeError: _react.default.useContext is not a function
does this indicate a ReactJS / hooks related problem?

sova-soars-the-sora22:09:47

I'm very confused...

sova-soars-the-sora22:09:49

Caused by: clojure.lang.ExceptionInfo: Call to clojure.core/defn- did not conform to spec. {:clojure.spec.alpha/problems [{:path [:fn-name], :pred clojure.core/simple-symbol?, :val clj-tuple/conj-tuple, :via [:clojure.core.specs.alpha/defn-args :clojure.core.specs.alpha/defn-args], :in [0]}], :clojure.spec.alpha/spec #object[clojure.spec.alpha$regex_spec_impl$reify__2509 0x2e090fb7 "clojure.spec.alpha$regex_spec_impl

lilactown22:09:47

@joshkh sounds like something is weird with your React being bundled

lilactown22:09:37

@sova that error isn’t helpful without the associated code. best guess: you gave it a weird function or arg name

joshkh22:09:07

hmm. in my project, simply changing @material-ui/core from v4 to v3 works fine.

lilactown22:09:44

what version of React are you using?

joshkh23:09:25

i cloned this project as a starting point which works like a charm, and then stripped out everything in the UI except for the Button in order to start small https://github.com/maxp/shadow-cljs-mui then i bumped @material-ui/core via yarn and that's when the exception started

joshkh23:09:19

hmm, it looks like i bumped react but not react-dom 😇 thanks @U4YGF4NGM

sova-soars-the-sora22:09:22

actually it is a result of including [stripe-clojure.core :as s]

sova-soars-the-sora22:09:48

someone mentioned it could be pulling in a transitive dependency on an old clojure.async? what to do

sova-soars-the-sora22:09:22

it's only using

[[org.clojure/clojure "1.6.0"]
		 [clj-http "1.0.1"]]

sova-soars-the-sora22:09:39

(ns stripe-clojure.core
  "Functions for Stripe Customers API"
  (:require [clj-http.client :as client])
  (:refer-clojure :exclude (list)))

sova-soars-the-sora22:09:54

why does it obliterate (defn-) when i include stripe-clojure?

sova-soars-the-sora22:09:14

(not actually a clojurescript question i guess?)

sova-soars-the-sora22:09:48

whatever i'll try another lib

lilactown22:09:10

I don’t know what you mean @sova. the error is saying it’s having trouble parsing a defn- somewhere, maybe in the stripe-clojure lib?

joshkh22:09:47

@sova for what it's worth, a friend had a lot of trouble using <some-stripe-cljs-library> and eventually went for direct js/cljs interop. i don't know if it was the same library, but it was a few versions behind stripe's current version.

🎉 4
4
lilactown23:09:21

that’s a very old version of clj-http

lilactown23:09:52

yeah the stripe-clojure lib seems pretty small, probably not worth going into it’s transitive deps to try and tease it out

lilactown23:09:16

also not related to clojurescript 🙂

🎯 4
sova-soars-the-sora23:09:15

yeah it looks like the best way to stripe-ify things is to just use their REST api yourself