Fork me on GitHub
#malli
<
2022-03-26
>
Carlo15:03:51

I got an error about trying to import malli.instrument in cljs, and in fact I see that in the malli repo we have only a instrument.clj file. Does this mean that I can't do instrumentation in cljs?

ambrosebs20:03:59

have you tried malli.instrument.cljs? It's apparently seen some bugfixes since the last release so I don't know if it works, but I happened upon it a few days ago https://github.com/metosin/malli/blob/master/src/malli/instrument/cljs.clj

🙌 1
Carlo20:03:27

Thank you, you're right! Here's how I missed that: I looked at this folder https://github.com/metosin/malli/tree/master/src/malli and I could only find the clj version. So I wrongly assumed that was the only available possibility!

Carlo20:03:17

How does the naming schema in the instrument folder work? I have never seen something like this!

ambrosebs02:03:59

I'm guessing that malli.instrument contains functions for the clojure impl. the cljs impl must be implemented via macros (but otherwise with the same names), so a new namespace malli.instrument.cljs was created.

Carlo20:03:41

I'm still getting this at times, though:

------ REPL Error while processing ---------------------------------------------
(ns couperin.user
  (:require [portal.web :as p]
            [malli.core :as m]
            [malli.instrument :as mi]
            [malli.dev :as dev]))
The required namespace "malli.instrument" is not available, it was required by "couperin/user.cljs".
"malli/instrument.clj" was found on the classpath. Maybe this library only supports CLJ?

Carlo20:03:05

Same thing for the namespace

dvingo03:03:15

hey, I contributed the cljs support, it's in need of some documentation. When using the instrumentation for cljs the namespaces should be: [malli.instrument.cljs :as mi] [malli.dev.cljs :as md] and there is kondo support but it must be executed at runtime (because the schemas aren't available during compilation (macroexpansion)) and prints to the console, with the intention being you copy it to a kondo config file. https://github.com/metosin/malli/blob/400dc0c79805028a6d85413086d4d6d627231940/src/malli/clj_kondo.cljc#L203 the js console errors need some work (the pretty one is designed with terminal emulators in mind, not js web console) but if you execute an instrumented fn via nrepl in an editor the error output there is better.

🙌 1
Carlo12:03:55

Thank you @U051V5LLP, I'm a bit confused on how instrumentation works on cljs. Even after importing:

(:require [portal.web :as p]
            [malli.core :as m]
            [malli.instrument.cljs :as mi]
            [malli.dev.cljs :as dev])
doing
(comment
  (dev/start!))
and adding:
(m/=> -add [:=> [:cat :int :int] :int])
(defn -add [x y]
  "abcd")
I can still execute calls like:
(-add 1 "a")
getting "abcd" as my answer (and I see that I have the instrumentation message in the browser console:
..instrumented 
{ns: 'couperin.user', name: '-add', str: 'couperin.user/-add', _hash: 150786855, _meta: null, …}
just not any error whatsoever

Carlo12:03:13

Ok, if I call (dev/start!) just before a call to -add, then I get the errors, but if I then modify the definition of -add , the instrumentation is not redone automatically

dvingo13:03:42

instrumentation works by replacing the function implementations so when hot reload happens the original functions will be replace the instrumented code. https://shadow-cljs.github.io/docs/UsersGuide.html#_lifecycle_hooks you'll want to call instrument! or start! after the hot code reload runs (usually something like (defn ^:dev/after-load refresh [] (md/start!)...) for shadow.cljs

Carlo14:03:34

Thank you, would you still advice to do https://clojureverse.org/t/problem-using-malli-clojurescript-instrumentation-and-shadow-cljs/8612/2 ? I can get it to check something but it's clearly confused by namespaces. Is there a particular place the refresh function you just mentioned should live?