Fork me on GitHub
#clojurescript
<
2020-11-03
>
ns00:11:02

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:

;; 
(:import [goog.async Debouncer])
(defn debounce [f interval]
  (let [dbnc (Debouncer. f interval)]
    (fn [& 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 Li00:11:39

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-himik07:11:28

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-himik07:11:35

BTW there's also #re-frame

jpmonettas18:11:51

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-himik18:11:58

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

thheller18:11:45

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

jpmonettas18:11:07

yeah I imagined, hmmm

p-himik19:11:41

@U05224H0W 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.

thheller19:11:05

even the do will prevent in-ns from working

thheller19:11:28

repl specials happen before macro expansion and not after

thheller19:11:49

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

thheller19:11:53

in CLJS they are not

p-himik19:11:13

> 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

p-himik19:11:56

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

thheller19:11:10

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

thheller19:11:56

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

thheller19:11:00

everything else compiles normally

thheller19:11:38

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

thheller19:11:59

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

p-himik19:11:12

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

thheller19:11:49

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

jpmonettas19:11:33

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:

jpmonettas19:11:07

I'm looking at the goog-define src

thheller19:11:28

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

jpmonettas19:11:43

oh that is better

jpmonettas19:11:22

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

jpmonettas19:11:05

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

thheller19:11:43

yeah this will be extremely difficult in CLJS

thheller19:11:02

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

thheller19:11:11

it can be done but it has all sorts of footguns

jpmonettas19:11:03

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

Braden Shepherdson20:11:20

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?

rakyi20:11:19

you could try asking in #cljs-dev