generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Open
franky47
wants to merge
14
commits into
next
Choose a base branch
from
feat/debounce
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Debounce #900
+1,441
−325
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
|
- 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.
71d0e92
to
846e029
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WIP, opening it early to have the nice round PR number 🤭
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.