Skip to content

Instantly share code, notes, and snippets.

@CodingOctocat
Last active March 7, 2025 12:09
Show Gist options
  • Save CodingOctocat/f9a28dd124d52f707a4a9fc8a4832bec to your computer and use it in GitHub Desktop.
Save CodingOctocat/f9a28dd124d52f707a4a9fc8a4832bec to your computer and use it in GitHub Desktop.
Debounce Dispatcher.
/// <summary>
/// Debounce Dispatcher.
/// <para>
/// forked from: <seealso href="https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/main/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs">Microsoft.Toolkit.Uwp.Extensions.DispatcherQueueTimerExtensions</seealso>.
/// </para>
/// </summary>
public static class DebounceDispatcher
{
private static readonly ConcurrentDictionary<DispatcherTimer, Action> _debounceActionDispatcherTimerInstances = new();
private static readonly ConcurrentDictionary<DispatcherTimer, Func<Task>> _debounceFuncDispatcherTimerInstances = new();
/// <summary>
/// <para>Used to debounce (rate-limit) an event. The action will be postponed and executed after the interval has elapsed. At the end of the interval, the function will be called with the arguments that were passed most recently to the debounced function.</para>
/// <para>Use this method to control the timer instead of calling Start/Interval/Stop manually.</para>
/// <para>A scheduled debounce can still be stopped by calling the stop method on the timer instance.</para>
/// <para>Each timer can only have one debounced function limited at a time.</para>
/// </summary>
/// <param name="timer">Timer instance, only one debounced function can be used per timer.</param>
/// <param name="action">Action to execute at the end of the interval.</param>
/// <param name="interval">Interval to wait before executing the action.</param>
/// <param name="immediate">Determines if the action execute on the leading edge instead of trailing edge.</param>
/// <example>
/// <code>
/// private DispatcherTimer _typeTimer = new();
///
/// _typeTimer.Debounce(async () =>
/// {
/// // Only executes this code after 0.3 seconds have elapsed since last trigger.
/// }, TimeSpan.FromSeconds(0.3));
/// </code>
/// </example>
public static void Debounce(this DispatcherTimer timer, Action action, TimeSpan interval, bool immediate = false)
{
// Check and stop any existing timer
bool timeout = timer.IsEnabled;
if (timeout)
{
timer.Stop();
}
// Reset timer parameters
timer.Tick -= Timer_Tick;
timer.Interval = interval;
if (immediate)
{
// If we're in immediate mode then we only execute if the timer wasn't running beforehand
if (!timeout)
{
action.Invoke();
}
}
else
{
// If we're not in immediate mode, then we'll execute when the current timer expires.
timer.Tick += Timer_Tick;
// Store/Update function
_debounceActionDispatcherTimerInstances.AddOrUpdate(timer, action, (k, v) => action);
}
// Start the timer to keep track of the last call here.
timer.Start();
}
/// <summary>
/// <para>Used to debounce (rate-limit) an event. The func will be postponed and executed after the interval has elapsed. At the end of the interval, the function will be called with the arguments that were passed most recently to the debounced function.</para>
/// <para>Use this method to control the timer instead of calling Start/Interval/Stop manually.</para>
/// <para>A scheduled debounce can still be stopped by calling the stop method on the timer instance.</para>
/// <para>Each timer can only have one debounced function limited at a time.</para>
/// </summary>
/// <param name="timer">Timer instance, only one debounced function can be used per timer.</param>
/// <param name="func">Func to execute at the end of the interval.</param>
/// <param name="interval">Interval to wait before executing the func.</param>
/// <param name="immediate">Determines if the func execute on the leading edge instead of trailing edge.</param>
/// <example>
/// <code>
/// private DispatcherTimer _typeTimer = new();
///
/// await _typeTimer.DebounceAsync(async () =>
/// {
/// // Only executes this code after 0.3 seconds have elapsed since last trigger.
/// }, TimeSpan.FromSeconds(0.3));
/// </code>
/// </example>
public static async Task DebounceAsync(this DispatcherTimer timer, Func<Task> func, TimeSpan interval, bool immediate = false)
{
// Check and stop any existing timer
bool timeout = timer.IsEnabled;
if (timeout)
{
timer.Stop();
}
// Reset timer parameters
timer.Tick -= Timer_TickAsync;
timer.Interval = interval;
if (immediate)
{
// If we're in immediate mode then we only execute if the timer wasn't running beforehand
if (!timeout)
{
await func();
}
}
else
{
// If we're not in immediate mode, then we'll execute when the current timer expires.
timer.Tick += Timer_Tick;
// Store/Update function
_debounceFuncDispatcherTimerInstances.AddOrUpdate(timer, func, (k, v) => func);
}
// Start the timer to keep track of the last call here.
timer.Start();
}
private static void Timer_Tick(object? sender, object e)
{
// This event is only registered/run if we weren't in immediate mode above
if (sender is DispatcherTimer timer)
{
timer.Tick -= Timer_Tick;
timer.Stop();
if (_debounceActionDispatcherTimerInstances.TryRemove(timer, out var action))
{
action?.Invoke();
}
}
}
private static async void Timer_TickAsync(object? sender, object e)
{
// This event is only registered/run if we weren't in immediate mode above
if (sender is DispatcherTimer timer)
{
timer.Tick -= Timer_TickAsync;
timer.Stop();
if (_debounceFuncDispatcherTimerInstances.TryRemove(timer, out var func))
{
await func();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment