-
-
Notifications
You must be signed in to change notification settings - Fork 168
feat: Debounce #900
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
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
This can't work since we do a hard load of the page, losing queued values.
And partially revert "fixes" made to the tests to bring back isolation.
- Detect userland navigation in next/app, this is kind of tedious but since Next.js also patches the History API and calls it on its own in our nuqs updates flow, we need an alternate approach (spin lock mutex-like). We should now have the whole debounce experience everywhere, checking with CI on various Next.js versions.
- Rename repro-702 into life-and-death, testing component mount/unmount with pending states - Move shared tests into a single fixture for faster bootstrapping - Assert against null transitive states (requires waiting for router.isReady in the pages router, see issue #290) - Order the routes definitions in React SPA, RRv6 & RRv7 to avoid merge conflicts in the future.
Some adapters will need to reset the throttle queue in different ways, to ensure a stable number of renders and initialising mounted components with queued states in the right way. By default, the queue is reset as it was before, and adapters can opt-out of this auto-reset and have the responsibility of doing it when a URL udpate occurs.
This caused issues in tests where we need to reset the queue in the same tick as after pushing changes. It's unlikely to have an effect in userland apps, but it makes the behaviour more robust and consistent.
I can't wait for this PR to be merged 👀 |
@devhasson have you tried it? See the "try it" section in the PR description. It's in a state where there are a lot of internal changes that should not (theoretically) affect userland behaviours, but I'm looking for folks to try it and give feedback of things that might have been missed by the test suite. Earlier this week I've been working at getting the "auto-abort pending updates on user navigation" detection working, it's now green in all adapters, but I'd like to try it in a couple of actual apps before it lands, just in case we end up with another aha moment like last week. One last suite of tests I'd like to add relates to the introduction of useSyncExternalStore inside the hooks, to deal with reactive optimistic state. For this I'll need to test it in a Suspense-heavy application. |
Try it
Documentation
This PR introduces a new method of rate-limiting URL updates, via debouncing rather than throttling (which 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
Investigate double-update mitigation (debounced update followed by a pre-empted default set, eg: pressing Enter to submit immediately on a debounced input).See discussions #373 & #449.
Closes #291.