-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement throttle
and debounce
as event actions
#3091
Conversation
Apply these settings similar to `stopPropagation` or `preventDefault` to influence how the event chain is processed on the frontend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, the UX will be much better for high fire-rate events !
Could we add a test when the event handler is a lambda? I expect the behaviour to be the same, but might as well be sure.
Fix integration test to allow one less throttled event than expected. Avoid flaky CI due to race conditions in the timing of clicks vs throttle limit.
rx.button( | ||
"Throttle", | ||
id="btn-throttle", | ||
on_click=lambda: EventActionState.on_click_throttle.throttle( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shoud have been more specific in the test I wanted, but since lambda are used to pass pre-set parameter, when we do, do we use
lambda value: EventActionState.on_change_throttle_with_args(value, "arg1").throttle(200).stop_propagation
or
(lambda value: EventActionState.on_change_throttle_with_args(value, "arg1")).throttle(200).stop_propagation
?
Though maybe it's more important to have this particular example in the docs than the tests.
This is a much needed feature, let me think a bit more on the final API but I like it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
works great with my color picker
Generic client side throttling and debounce for events.
Used just like
.stop_propagation
and.prevent_default
, so applicable to all event handler or event spec objects.Due to how events are handled, there isn't currently a way to apply these to only a single event in the chain. If multiple event handlers are specified and any of them have
debounce
orthrottle
, then it will apply to all of the other events as well.If the same function is throttled or debounced at the same limit/delay by multiple event triggers, these will all apply in the same domain. If different limit or delay is used, then they will be debounced or throttled separately.
Sample Code
Screen.Recording.2024-04-15.at.16.25.55.mov
Edit 2024-04-16: added a debounced slider example. Note it's important to wrap is the
is_hydrated
cond, otherwise the default_value after refreshing will be the initial value, not the last value.