ClojureScript debouncer

I’m developing Dashman in ClojureScript (and Clojure) and as I was developing the cookie handler I found sites that constantly modify the cookies. As in, many times per second. Obviously I don’t want to encrypt the cookies and send them to the server that often, but even without taking that into account, just storing them to keep track of them in an atom, as in:

(reset! cookies new-cookies)

was slowing down the app considerably. I found myself needing to debounce so many calls as there’s no value in the middle calls, only the last one. Let’s backtrack a little bit here.

The term to debounce comes from electronics:

To remove the small ripple of current that forms when a mechanical switch is pushed in an electrical circuit and makes a series of short contacts.

In programming it refers to a signal that gets emitted often and you want to process it but you don’t care about processing every intermediate value. To tie into electronics imagine you have a program that receives a message every time the temperature changes and the message contains all the temperature changes for the day. Clearly each message doesn’t have to be processed, only one, only the latest.

Here’s where a software debouncer can help. It’s a function that gets a function and returns another function. When you call the resulting function 10 times, it will only call the original function once, with the last value it received.

I found this debouncer written by David Nolan: https://gist.github.com/swannodette/5888989 and in the comments you can start to see that writing a debouncer might not be a trivial as you may think. One of the comments point to this other version: https://gist.github.com/scttnlsn/9744501. The comment implies this is a more correct version but I haven’t verified it myself. In that last page you can see someone found a bug and posted an improved version.

For me, when a supposedly simple code snippet generates so many comments and versions, it’s better to find a library that implements it than to copy and paste and never maintain it again. Unfortunately, I couldn’t find a ClojureScript debouncer library but I got pointed to goog.function.debounce which you can use this way;

(ns whatever
  (:require [goog.functions]))

(def debounced-println (goog.functions.debounce println))
Advertisements

Book Review: The 7 Habits of Highly Effective People: Powerful Lessons in Personal Change by Stephen R. Covey

The 7 Habits Of Highly Effective People 3I think I had my expectations wrong for this book. I was expecting it to focus on business and professional growth when most of its focus is on relationships and families. And when it addresses the professional life, it’s from the point of view of a manager/leader of a big company.

It has some interesting concepts such as the emotional account that I think if everybody followed them it would make the world a happier place. But I strongly disagree that there’s a correlation between following those values and one being more effective. For example, the book asserts that if you treat people respectfully, they’ll respect you back. This is not true. This is not how the world works. This is not how the brain work either and some of the facts about the brain that this book cite have been proved false.

Lastly but not least, the book gets awfully preachy, jumping into religion like if that was fact or proof of anything.

All in all I think the book pretends to be scientific but it’s very dogmatic. I’d recommend avoiding it.

★☆☆☆☆

Buy The 7 Habits of Highly Effective People: Powerful Lessons in Personal Change in USA
Buy The 7 Habits of Highly Effective People: Powerful Lessons in Personal Change in UK
Buy The 7 Habits of Highly Effective People: Powerful Lessons in Personal Change in Canada