Fork me on GitHub
#clojurescript
<
2022-10-26
>
jaide17:10:20

Implementing a debounce util where a function returns a function that resets a timer before firing. The timer id variable typically changes over time. I know an atom could work but is there a better tool for that job?

p-himik17:10:17

There's something built into goog.

jaide17:10:40

Thanks I'll probably use that but if that didn't exist, what would one use?

p-himik18:10:16

I'd use an atom, yes. Unless the debouncing for some reason is in a hot spot, then a regular field on some JS object, probably on the function itself.

Lone Ranger03:10:28

@U8WFYMFRU I mean typically you would use lodash.debounce I would think

Lone Ranger03:10:55

unless there’s some reason not to

jaide04:10:23

It's a really small function to implement, lodash's implementation is gigantic and not needed for my use case. https://github.com/lodash/lodash/blob/master/debounce.js vs:

(defn debounce
  [ms f]
  (let [timer-atom (r/atom -1)]
    (fn [& args]
      (js/clearTimeout @timer-atom)
      (reset! timer-atom (js/setTimeout #(apply f args) ms)))))

jaide04:10:20

Needed it to work in shadow-cljs and nbb environments

alex20:10:01

I am hoping to capture a failing test name in test code and then run some code based off the test name. Is the best way to do this a custom test reporter that runs against the :fail event? Would I need to define a new multimethod to achieve this?

(defmulti custom-reporter
  (fn [m] [::custom-reporter (:type m)]))

(defmethod custom-reporter [::custom-reporter :fail]
  (fn [m] (reset! failed-test (:name m))
• Is there an easier way to capture a failing test name in test code? • How do I specify that the test runner use the custom reporter? • If I wanted the [::custom-reporter :fail] method to additionally call the default :fail implementation (the default logging on failed tests is useful), is there a way to do that?