Philiprehberger.Debounce

Debounce and throttle functions for .NET with configurable delay, leading/trailing edge, and async support.
Installation
dotnet add package Philiprehberger.Debounce
Usage
Trailing Edge Debounce
using Philiprehberger.Debounce;
var debounced = Debounce.Create(() =>
{
Console.WriteLine("Saving...");
}, TimeSpan.FromMilliseconds(300));
debounced.Invoke(); // resets timer
debounced.Invoke(); // resets timer again
// "Saving..." fires 300ms after the last Invoke()
Leading Edge Debounce
using Philiprehberger.Debounce;
var leading = Debounce.Create(() =>
{
Console.WriteLine("Clicked!");
}, TimeSpan.FromMilliseconds(500), leading: true);
leading.Invoke(); // fires immediately
leading.Invoke(); // ignored (within 500ms window)
Async Debounce with Manual Control
using Philiprehberger.Debounce;
var debounced = Debounce.Create(async () =>
{
await SaveToDatabase();
}, TimeSpan.FromSeconds(1));
await debounced.InvokeAsync();
debounced.Cancel(); // cancel pending execution
await debounced.FlushAsync(); // execute immediately if pending
Console.WriteLine($"Pending: {debounced.IsPending}");
Throttle
using Philiprehberger.Debounce;
var throttled = Throttle.Create(() =>
{
Console.WriteLine("Processing...");
}, TimeSpan.FromSeconds(1));
throttled.Invoke(); // fires
throttled.Invoke(); // ignored (within 1s)
throttled.Reset(); // reset the throttle window
throttled.Invoke(); // fires again
API
Debounce
| Member | Description |
|---|
Create(Action, TimeSpan, bool leading) | Create a debounced action (trailing edge by default) |
Create(Func<Task>, TimeSpan, bool leading) | Create a debounced async action |
DebouncedAction
| Member | Description |
|---|
Invoke() | Trigger the debounced action (resets timer) |
InvokeAsync() | Trigger the debounced async action |
Cancel() | Cancel any pending execution |
Flush() | Execute immediately if a call is pending |
FlushAsync() | Execute async immediately if a call is pending |
IsPending | Whether an execution is pending |
Throttle
| Member | Description |
|---|
Create(Action, TimeSpan) | Create a throttled action |
Create(Func<Task>, TimeSpan) | Create a throttled async action |
ThrottledAction
| Member | Description |
|---|
Invoke() | Execute if the interval has elapsed since last execution |
InvokeAsync() | Execute async if the interval has elapsed |
Reset() | Reset the throttle window |
Development
dotnet build src/Philiprehberger.Debounce.csproj --configuration Release
Support
If you find this project useful:
⭐ Star the repo
🐛 Report issues
💡 Suggest features
❤️ Sponsor development
🌐 All Open Source Projects
💻 GitHub Profile
🔗 LinkedIn Profile
License
MIT