clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
ns 2020-11-03T00:26:02.011800Z

How do I use debounce in combination with input on-change? I tried using goog.functions.debounce and goog.async Debouncer but I'm constantly running into "This synthetic event is reused for performance reasons....If you must keep the original synthetic event around, use event.persist()". Then I try adding (.persist e) but it doesn't help. I must be missing something but I've been stuck on this for hours. Any help is appreciated! Edit: Think I got it finally, this combination worked in case anyone is struggling with the same issue:

;; <https://martinklepsch.org/posts/simple-debouncing-in-clojurescript.html>
(:import [goog.async Debouncer])
(defn debounce [f interval]
  (let [dbnc (Debouncer. f interval)]
    (fn [&amp; args] (.apply (.-fire dbnc) dbnc (to-array args)))))
(defn input-on-change [e]
  (.log js/console (.. e -target -value)))
(def input-on-change-debounced
  (debounce input-on-change 1000))
..........
:on-change (fn [e]
               (.persist e)
               (input-on-change-debounced e))

Ronny Li 2020-11-03T00:46:39.015100Z

Hi, I have a re-frame interceptor set up after every event to check the modified db against my spec (following https://github.com/oskarth/re-frame-docs/blob/master/re-frame-one-doc.md#3-checking-db-integrity using prismatic/schema). However, the schema validator fails when my db is first being initialized since it's simply an empty map at that point. As a result I get errors like schema problem: {:logged-in-user {:uid missing-required-key}} because the user hasn't logged in yet. I know I could set optional keys but that seems a bit counter-productive to me. Does anyone have a better solution?

p-himik 2020-11-03T07:26:28.015700Z

Do you check your schema in an :after or a :before interceptor? You should use :after and have the very first event in your app set up the correct initial DB state.

p-himik 2020-11-03T07:26:35.015900Z

BTW there's also #re-frame

2020-11-03T18:47:51.017200Z

Hi ! Is there a way of of defining a macro that will change the current namespace in clojurescript at the repl?

(defmacro def-in-core []
  `(do
     (in-ns (quote cljs.core))
     (def a 42)))
This currently expands to (cljs.core/in-ns 'cljs.core) which doesn't exist. What I'm trying to acomplish is a macro that can create a def in a different ns.

p-himik 2020-11-03T18:50:58.017300Z

No idea if it can work at all, but you can try adding ~' in front of in-ns.

thheller 2020-11-03T18:54:45.017500Z

this doesn't work in CLJS. in-ns is a special form that won't be handled when emitted like that

2020-11-03T18:59:07.017700Z

yeah I imagined, hmmm

p-himik 2020-11-03T19:05:41.017900Z

@thheller When using that macro (assuming in-ns is quoted) as

(def x 1)
(def-in-core)
(def y 2)
how is that not equivalent to
(def x 1)
(do
  (in-ns 'cljs.core)
  (def a 42))
(def y 2)
? After all, macros expansion is done before CLJS compilation.

thheller 2020-11-03T19:07:05.018100Z

even the do will prevent in-ns from working

thheller 2020-11-03T19:07:28.018300Z

repl specials happen before macro expansion and not after

thheller 2020-11-03T19:07:49.018500Z

in CLJ these are all just functions so they all just work fine

thheller 2020-11-03T19:07:53.018700Z

in CLJS they are not

p-himik 2020-11-03T19:10:13.018900Z

> repl specials happen before macro expansion and not after Oh. Huh. So in-ns doesn't exist outside of REPL at all in CLJS?

Use of undeclared Var clj-playground.core/in-ns

thheller 2020-11-03T19:10:41.019100Z

yep

p-himik 2020-11-03T19:11:56.019300Z

Thanks! Do you know of any comprehensive resource that would describe all the steps/stages and what specials available at which step/stage?

thheller 2020-11-03T19:13:10.019500Z

dont know about default CLJS anymore. the shadow-cljs specials are here https://github.com/thheller/shadow-cljs/blob/master/src/main/shadow/cljs/repl.clj#L401-L425

thheller 2020-11-03T19:13:56.019900Z

basically when in the REPL the specials take a different path through the compiler

thheller 2020-11-03T19:14:00.020100Z

everything else compiles normally

thheller 2020-11-03T19:14:38.020300Z

this is mostly because things like require may require async IO for loading files and stuff

thheller 2020-11-03T19:14:59.020500Z

thats why you can't have dynamic require in code also

p-himik 2020-11-03T19:15:12.020700Z

I see. Thanks! > dont know about default CLJS anymore That evokes sad chuckles.

thheller 2020-11-03T19:15:49.020900Z

its definitely there and pretty sure it looks the same. just don't know where it is anymore 😉

2020-11-03T19:16:33.021100Z

Maybe I can use (goog/define "cljs.core.odd_QMARK_" (fn [n] 5)) or something like that if I want to lets say replace odd? in cljs.core :thinking_face:

2020-11-03T19:17:07.021300Z

I'm looking at the goog-define src

thheller 2020-11-03T19:17:28.021500Z

@jpmonettas no. you can use (set! cljs.core/odd? (fn [n] 5)) but not goog-define. be very careful doing this though.

2020-11-03T19:17:43.021700Z

oh that is better

2020-11-03T19:19:22.022Z

I'm creating a instrument macro, and want to be able to do (trace-var cljs.core/map) so it grabs the source using source-fn, instrument it, and then replace the original with the instrumented version

2020-11-03T19:20:05.022400Z

it is working fine in clojure, but was having trouble in cljs

thheller 2020-11-03T19:20:43.022600Z

yeah this will be extremely difficult in CLJS

thheller 2020-11-03T19:21:02.022800Z

I'd suggest looking at the spec instrument function to see how it works

thheller 2020-11-03T19:21:11.023Z

it can be done but it has all sorts of footguns

2020-11-03T19:22:03.023200Z

oh yeah I was going to take a look at instrument but forgot

2020-11-03T19:22:12.023400Z

thanks @thheller!

2020-11-03T20:36:20.025200Z

so apparently ClojureScript depends on a few parts of the com.google.javascript.jscomp API (eg. AnonymousFunctionNamingPolicy) that have been removed recently. in 20200920 for that enum specifically, not sure otherwise. is there a nightly or prerelease version of CLJS out there that works with the latest Closure compiler?

rakyi 2020-11-05T20:03:19.060400Z

you could try asking in #cljs-dev