-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
feat: Debounce #900
base: next
Are you sure you want to change the base?
feat: Debounce #900
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
commit: |
8e035d3
to
1a733f0
Compare
Can't wait to test this feature 😁 |
@Kavan72 you can! Follow the preview install instructions here, and the docs from the PR description:
|
71d0e92
to
846e029
Compare
92b44ce
to
f8f318b
Compare
f8f318b
to
fda6ffb
Compare
fda6ffb
to
bbf3496
Compare
Sorry, I didn’t get a chance to test this, but here are some of my thoughts related to the current issues:
|
Thanks for your feedback. I agree with point 2, I updated the implementation to do just that yesterday. For point 1, I didn't think of the "apply before navigating", which makes sense, unfortunately I doubt we'll be able to reliably inject custom behaviours before navigation occurs in all frameworks (and even doing so after navigation would have its issues). I was thinking of providing a way to abort pending debounced updates, what do you think? |
- Move shared code into src/lib, leaving only features at the top level - Move the throttle queue into a class to help with testing & multi-storage support later - Add stubs for debounce queue
Also renamed the property to avoid clashes with the throttleMs option.
Removed premature optimisation: skipping scheduling a flush returns a different Promise reference, breaking existing behaviour.
Documentation
This PR introduces a new method of rate-limiting URL updates, via debouncing rather than throttling (that was already possible via the
throttleMs
option).Debouncing vs throttling
https://kettanaito.com/blog/debounce-vs-throttle
Throttling gives you predictable, consistent updates at a given rate. It's necessary to mitigate rate limits of the browser History API methods. One advantage of throttling is that, assuming there was sufficient inactivity before an event occurred, it will be emitted instantly, allowing for a reactive first update (and delaying subsequent ones).
Debouncing on the other hand gives you one eventual update after a given amount of inactivity has passed. Debouncing is useful for high-frequency inputs where only the final value is valuable, like moving a slider or typing some text.
Both of these mechanisms are in place to rate-limit updates to the URL. The internal state returned by
useQueryState(s)
is always updated instantly (just likeReact.useState
would).Specifying options
The
throttleMs
option is now marked as deprecated (but still supported until a later major, maybe v4, TBC), and replaced with alimitUrlUpdates
option:Shorthands
You can use the
throttle
anddebounce
shorthand methods, as well as thedefaultRateLimit
export to shorten your options:Interaction with other options
The
startTransition
function is used only when the URL is being updated, so for a debounced update, the loading state will only be triggered once the debounce time has elapsed, not while it is pending.You'll likely want to set
shallow: false
to notify the server of updates, as I don't see many reasons to debounce a client-only URL update.If you set both
throttleMs
andlimitUrlUpdates
,limitUrlUpdates
will take precedence.Tips & tricks
You will likely want to turn on debounce on high-frequency state updates. But often, inputs have multiple source of updates with different event firing rates, for example:
<input type="range">
) has a very high event emitting frequency foronChange
on drag that you may want to debounce, but clicking on a point on the slider track (not dragging) should likely be an instant update.onChange
when typing that warrant debouncing, but actions like clearing the input (select all + backspace) or pressing Enter should commit the value instantly.One recommendation is to define debouncing when calling the state updater function:
Internal
Prep work:
multi-storage support later
Tasks
See discussions #373 & #449.
Closes #291.