Soenneker.Utils.Debounce
3.0.10
Prefix Reserved
dotnet add package Soenneker.Utils.Debounce --version 3.0.10
NuGet\Install-Package Soenneker.Utils.Debounce -Version 3.0.10
<PackageReference Include="Soenneker.Utils.Debounce" Version="3.0.10" />
<PackageVersion Include="Soenneker.Utils.Debounce" Version="3.0.10" />
<PackageReference Include="Soenneker.Utils.Debounce" />
paket add Soenneker.Utils.Debounce --version 3.0.10
#r "nuget: Soenneker.Utils.Debounce, 3.0.10"
#:package Soenneker.Utils.Debounce@3.0.10
#addin nuget:?package=Soenneker.Utils.Debounce&version=3.0.10
#tool nuget:?package=Soenneker.Utils.Debounce&version=3.0.10
Soenneker.Utils.Debounce
A utility that lets you debounce work in .NET.
Give it a delay, async/sync delegate, and the Debouncer
guarantees that multiple rapid calls collapse into exactly one invocation.
Why would I need this?
- API calls: Prevent hammering a server while the user types.
- Disk I/O: Batch frequent save requests into a single write.
- Telemetry: Send aggregated metrics after bursts of activity.
- Search boxes / auto-complete: React only after the user pauses typing.
Quick start
dotnet add package Soenneker.Utils.Debounce
using Soenneker.Utils.Debounce;
var debouncer = new Debouncer();
// Fire only once, 300 ms after the *last* request:
void OnTextChanged(string text)
{
debouncer.Debounce(
delayMs: 300,
action: async ct =>
{
var results = await SearchAsync(text, ct);
UpdateUI(results);
});
}
void OnResize()
{
debouncer.Debounce(
delayMs: 250,
action: () =>
{
// Runs on the thread-pool after 250 ms of quiescence
SaveWindowLayout();
});
}
Leading-edge execution
Pass runLeading: true
if you want the first call to run immediately and the trailing call to run after the quiet period:
debouncer.Debounce(
delayMs: 500,
runLeading: true,
action: ct => Logger.LogAsync("Burst started", ct));
Either wrap it in a using
statement or dispose the debouncer when you’re done:
await debouncer.DisposeAsync();
DisposeAsync()
waits for any in-flight work to finish, ensuring graceful shutdown.
Design highlights
- Pure TPL: Built on
System.Threading.Timer
- Thread-safe: Internal state is guarded with
Interlocked
swaps. - Cancellation-friendly: Each queued delegate receives its own
CancellationToken
. - Zero allocations on idle: Work objects are created only when you call
Debounce
. - Tested: xUnit suite covering timing, cancellation, and disposal semantics.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Soenneker.Extensions.Task (>= 3.0.105)
- Soenneker.Extensions.ValueTask (>= 3.0.95)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Utils.Debounce:
Package | Downloads |
---|---|
Soenneker.Quark.Table
A native Blazor table component. |
GitHub repositories
This package is not used by any popular GitHub repositories.