Replies: 9 comments
-
There's a built-in These issues for re-frame are for bug reports and feature requests. Support requests and usage questions should go to the Clojure Slack channel, the ClojureScript mailing list, or the Reagent mailing list. Feel free to discuss this further on one of those channels. I've thought a little bit about debouncing, but haven't really needed it so far. |
Beta Was this translation helpful? Give feedback.
-
First, let me say that I've never run into this issue, so I'm not confident I fully understand all the requirements. I'm going to need guidance here on what's needed, particularly on any fine detail. My first thought is that this should be done via an effect handler, supplied in a library (not in re-frame itself). That's exactly why the effect handlers system was created to be extensible. So, this would be a very small library (30 lines of code?) for someone to write (not me) but we can thrash out the details in this ticket. An event handler (which needed to issue a debouncable event) might look like this: (def-event-fx
:search-key-pressed
(fn [cofx [_ current-search-string]]
{:dispatch-debounce {:key ::search ;; a unique key
:event [:do-search current-search-string] ;; an event vector
:delay 250}]})) ;; the future time at which to issue The idea is that the event Notes:
Later: Uggh. I accidentally managed to click "Close and Comment' instead of "Comment". So read nothing into the following closing and reopening noise. |
Beta Was this translation helpful? Give feedback.
-
Looks good to me! The requirements for the usage I have in mind are all there.
|
Beta Was this translation helpful? Give feedback.
-
I've just run into this for a project, and implemented it as follows: (defonce timeouts
(atom {}))
(reg-fx :dispatch-debounce
(fn [[id event-vec n]]
(js/clearTimeout (@timeouts id))
(swap! timeouts assoc id
(js/setTimeout (fn []
(dispatch event-vec)
(swap! timeouts dissoc id))
n))))
(reg-fx :stop-debounce
(fn [id]
(js/clearTimeout (@timeouts id))
(swap! timeouts dissoc id))) Example of using it: (def-event-fx
:search-field-updated
(fn [_ [_ current-search-string]]
{:dispatch-debounce [::search [:do-search current-search-string] 250]}))
(def-event-fx
:cancel-search
(fn [_ _]
{:stop-debounce ::search})) |
Beta Was this translation helpful? Give feedback.
-
@ff- I think a For example, say I had two search fields, with dispatches such as: In my handler, if I did: (def-event-fx
:search-field-updated
(fn [_ [_ field current-search-string]]
{:dispatch-debounce [[:do-search field current-search-string] 250]})) ...then the two fields wouldn't be debounced independently. This specific example is a bit contrived, but I have run into the need for a |
Beta Was this translation helpful? Give feedback.
-
@rafd very true, thanks for the answer! |
Beta Was this translation helpful? Give feedback.
-
I won't comment for Mike, but will give my thoughts on this:
I have no particularly strong feelings either way, just sharing my perspective. |
Beta Was this translation helpful? Give feedback.
-
There's a PR for this in #249. |
Beta Was this translation helpful? Give feedback.
-
At this stage, we're going to keep debounce and throttle effects as separate from the core of re-frame. More details in #258 (comment). |
Beta Was this translation helpful? Give feedback.
-
Sorry for the ignorance ... what's the best way to
debounce
ordelay
a dispatch? In redux land there are various kinds of middleware for that.Maybe you could add an answer to this to the FAQ section?!
Beta Was this translation helpful? Give feedback.
All reactions