diff --git a/src/Core/Utilities/Debounce.cs b/src/Core/Utilities/Debounce.cs new file mode 100644 index 0000000000..6cac0f44b1 --- /dev/null +++ b/src/Core/Utilities/Debounce.cs @@ -0,0 +1,13 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + +namespace Microsoft.FluentUI.AspNetCore.Components.Utilities; + +/// +/// The DebounceTask dispatcher delays the invocation of an action until a predetermined interval has elapsed since the last call. +/// This ensures that the action is only invoked once after the calls have stopped for the specified duration. +/// +public sealed class Debounce : InternalDebounce.DebounceTask +{ +} diff --git a/src/Core/Utilities/InternalDebounce/DebounceAction.cs b/src/Core/Utilities/InternalDebounce/DebounceAction.cs new file mode 100644 index 0000000000..907da424a2 --- /dev/null +++ b/src/Core/Utilities/InternalDebounce/DebounceAction.cs @@ -0,0 +1,61 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + +namespace Microsoft.FluentUI.AspNetCore.Components.Utilities.InternalDebounce; + +/// +/// The DebounceTask dispatcher delays the invocation of an action until a predetermined interval has elapsed since the last call. +/// This ensures that the action is only invoked once after the calls have stopped for the specified duration. +/// +[Obsolete("Use Debounce, which inherits from DebounceTask.")] +internal class DebounceAction : IDisposable +{ + private bool _disposed; + private readonly System.Timers.Timer _timer = new(); + private TaskCompletionSource? _taskCompletionSource; + + /// + /// Gets a value indicating whether the DebounceTask dispatcher is busy. + /// + public bool Busy => _taskCompletionSource?.Task.Status == TaskStatus.Running && !_disposed; + + /// + /// Gets the current task. + /// + public Task CurrentTask => _taskCompletionSource?.Task ?? Task.CompletedTask; + + /// + /// Delays the invocation of an action until a predetermined interval has elapsed since the last call. + /// + /// + /// + /// + public void Run(int milliseconds, Func action) + { + // Check arguments + if (milliseconds <= 0) + { + throw new ArgumentOutOfRangeException(nameof(milliseconds), milliseconds, "The milliseconds must be greater than to zero."); + } + + ArgumentNullException.ThrowIfNull(action); + + // DebounceTask + if (!_disposed) + { + _taskCompletionSource = _timer.Debounce(action, milliseconds); + } + } + + /// + /// Releases all resources used by the DebounceTask dispatcher. + /// + public void Dispose() + { + _taskCompletionSource = null; + _timer.Dispose(); + _disposed = true; + GC.SuppressFinalize(this); + } +} diff --git a/src/Core/Utilities/InternalDebounce/DebounceTask.cs b/src/Core/Utilities/InternalDebounce/DebounceTask.cs new file mode 100644 index 0000000000..79746d957c --- /dev/null +++ b/src/Core/Utilities/InternalDebounce/DebounceTask.cs @@ -0,0 +1,84 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + +namespace Microsoft.FluentUI.AspNetCore.Components.Utilities.InternalDebounce; + +/// +/// The DebounceTask dispatcher delays the invocation of an action until a predetermined interval has elapsed since the last call. +/// This ensures that the action is only invoked once after the calls have stopped for the specified duration. +/// +public class DebounceTask : IDisposable +{ +#if NET9_0_OR_GREATER + private readonly System.Threading.Lock _syncRoot = new(); +#else + private readonly object _syncRoot = new(); +#endif + + private bool _disposed; + private Task? _task; + private CancellationTokenSource? _cts; + + /// + /// Gets a value indicating whether the DebounceTask dispatcher is busy. + /// + public bool Busy => _task?.Status == TaskStatus.Running && !_disposed; + + /// + /// Gets the current task. + /// + public Task CurrentTask => _task ?? Task.CompletedTask; + + /// + /// Delays the invocation of an action until a predetermined interval has elapsed since the last call. + /// + /// + /// + /// + public void Run(int milliseconds, Func action) + { + // Check arguments + if (milliseconds <= 0) + { + throw new ArgumentOutOfRangeException(nameof(milliseconds), milliseconds, "The milliseconds must be greater than to zero."); + } + + ArgumentNullException.ThrowIfNull(action); + + // Cancel the previous task if it's still running + _cts?.Cancel(); + + // Create a new cancellation token source + _cts = new CancellationTokenSource(); + + try + { + // Wait for the specified time + _task = Task.Delay(TimeSpan.FromMilliseconds(milliseconds), _cts.Token) + .ContinueWith(t => + { + if (!_disposed && !_cts.IsCancellationRequested) + { + lock (_syncRoot) + { + _ = action.Invoke(); + } + } + }, _cts.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current); + } + catch (TaskCanceledException) + { + // Task was canceled + } + } + + /// + /// Releases all resources used by the DebounceTask dispatcher. + /// + public void Dispose() + { + _disposed = true; + _cts?.Cancel(); + } +} diff --git a/src/Core/Utilities/InternalDebounce/DispatcherTimerExtensions.cs b/src/Core/Utilities/InternalDebounce/DispatcherTimerExtensions.cs new file mode 100644 index 0000000000..1fd89d3861 --- /dev/null +++ b/src/Core/Utilities/InternalDebounce/DispatcherTimerExtensions.cs @@ -0,0 +1,102 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + +using System.Collections.Concurrent; + +namespace Microsoft.FluentUI.AspNetCore.Components.Utilities.InternalDebounce; + +/// +/// Extension methods for . +/// +/// +/// Inspired from Microsoft.Toolkit.Uwp.UI.DispatcherQueueTimerExtensions +/// +internal static class DispatcherTimerExtensions +{ + private static readonly ConcurrentDictionary _debounceInstances = new(); + + /// + /// Delays the invocation of an action until a predetermined interval has elapsed since the last call. + /// + /// + /// + /// + /// + public static TaskCompletionSource Debounce(this System.Timers.Timer timer, Func action, double interval) + { + // Check and stop any existing timer + timer.Stop(); + + // Reset timer parameters + timer.Elapsed -= Timer_Elapsed; + timer.Interval = interval; + + // If we're not in immediate mode, then we'll execute when the current timer expires. + timer.Elapsed += Timer_Elapsed; + + // Store/Update function + TimerDebounceItem updateValueFactory(System.Timers.Timer k, TimerDebounceItem v) => v.UpdateAction(action); + var item = _debounceInstances.AddOrUpdate( + key: timer, + addValue: new TimerDebounceItem() + { + Status = new TaskCompletionSource(), + Action = action, + }, + updateValueFactory: updateValueFactory); + + // Start the timer to keep track of the last call here. + timer.Start(); + + return item.Status; + } + + /// + /// Timer elapsed event handler. + /// + /// + /// + private static void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) + { + // This event is only registered/run if we weren't in immediate mode above + if (sender is System.Timers.Timer timer) + { + timer.Elapsed -= Timer_Elapsed; + timer.Stop(); + + if (_debounceInstances.TryRemove(timer, out var item)) + { + _ = (item?.Action.Invoke()); + item?.Status.SetResult(); + } + } + } + + /// + /// Timer debounce item. + /// + private class TimerDebounceItem + { + /// + /// Gets the task completion source. + /// + public required TaskCompletionSource Status { get; init; } + + /// + /// Gets or sets the action to execute. + /// + public required Func Action { get; set; } + + /// + /// Updates the action to execute. + /// + /// + /// + public TimerDebounceItem UpdateAction(Func action) + { + Action = action; + return this; + } + } +} diff --git a/tests/Core/Utilities/DebounceActionTests.cs b/tests/Core/Utilities/DebounceActionTests.cs new file mode 100644 index 0000000000..f103ed9101 --- /dev/null +++ b/tests/Core/Utilities/DebounceActionTests.cs @@ -0,0 +1,236 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading.Tasks; +using Bunit; +using Microsoft.FluentUI.AspNetCore.Components.Utilities; +using Microsoft.FluentUI.AspNetCore.Components.Utilities.InternalDebounce; +using Microsoft.VisualStudio.TestPlatform.Utilities; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.FluentUI.AspNetCore.Components.Tests.Utilities; + +#pragma warning disable CS0618 // Type or member is obsolete + +public class DebounceActionTests +{ + private readonly ITestOutputHelper Output; + + public DebounceActionTests(ITestOutputHelper output) + { + Output = output; + } + + [Fact] + public async Task Debounce_Default() + { + // Arrange + var debounce = new DebounceAction(); + var actionCalled = false; + var watcher = Stopwatch.StartNew(); + + // Act + debounce.Run(50, async () => + { + actionCalled = true; + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.True(watcher.ElapsedMilliseconds >= 50); + Assert.True(actionCalled); + } + + [Fact] + public async Task Debounce_MultipleCalls() + { + // Arrange + var debounce = new DebounceAction(); + var actionCalledCount = 0; + var actionCalled = string.Empty; + + // Act + debounce.Run(50, async () => + { + actionCalled = "Step1"; + actionCalledCount++; + await Task.CompletedTask; + }); + + debounce.Run(40, async () => + { + actionCalled = "Step2"; + actionCalledCount++; + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.Equal("Step2", actionCalled); + Assert.Equal(1, actionCalledCount); + } + + [Fact] + public async Task Debounce_MultipleCalls_Async() + { + // Arrange + var debounce = new DebounceAction(); + var actionCalledCount = 0; + var actionCalled = string.Empty; + + // Act: simulate two async calls + _ = Task.Run(() => + { + debounce.Run(50, async () => + { + actionCalled = "Step1"; + actionCalledCount++; + await Task.CompletedTask; + }); + }); + + await Task.Delay(5); // To ensure the second call is made after the first one + + _ = Task.Run(() => + { + debounce.Run(40, async () => + { + actionCalled = "Step2"; + actionCalledCount++; + await Task.CompletedTask; + }); + }); + + await Task.Delay(100); // Wait for the debounce to complete + + // Assert + Assert.Equal("Step2", actionCalled); + Assert.Equal(1, actionCalledCount); + } + + [Fact] + public async Task Debounce_Disposed() + { + // Arrange + var debounce = new DebounceAction(); + var actionCalled = false; + + // Act + debounce.Dispose(); + + debounce.Run(50, async () => + { + actionCalled = true; + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.False(actionCalled); + } + + [Fact] + public async Task Debounce_Busy() + { + // Arrange + var debounce = new DebounceAction(); + + // Act + debounce.Run(50, async () => + { + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.False(debounce.Busy); + } + + [Fact] + public async Task Debounce_Exception() + { + // Arrange + var debounce = new DebounceAction(); + + // Act + debounce.Run(50, async () => + { + await Task.CompletedTask; + throw new InvalidProgramException("Error"); // Simulate an exception + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.False(debounce.Busy); + } + + [Fact] + public void Debounce_DelayMustBePositive() + { + // Arrange + var debounce = new DebounceAction(); + + // Act + Assert.Throws(() => + { + debounce.Run(-10, () => Task.CompletedTask); + }); + } + + [Fact] + public async Task Debounce_FirstRunAlreadyStarted() + { + // Arrange + var debounce = new DebounceAction(); + var actionCalledCount = 0; + + // Act + debounce.Run(10, async () => + { + Output.WriteLine("Step1 - Started"); + + await Task.Delay(100); + actionCalledCount++; + + Output.WriteLine("Step1 - Completed"); + }); + + await Task.Delay(20); // Wait for Step1 to start. + + debounce.Run(10, async () => + { + Output.WriteLine("Step2 - Started"); + + await Task.CompletedTask; + actionCalledCount++; + + Output.WriteLine("Step2 - Completed"); + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + await Task.Delay(200); + + // Assert + Assert.Equal(2, actionCalledCount); + } +} + +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/tests/Core/Utilities/DebounceTaskTests.cs b/tests/Core/Utilities/DebounceTaskTests.cs new file mode 100644 index 0000000000..1ab311f827 --- /dev/null +++ b/tests/Core/Utilities/DebounceTaskTests.cs @@ -0,0 +1,232 @@ +// ------------------------------------------------------------------------ +// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. +// ------------------------------------------------------------------------ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading.Tasks; +using Bunit; +using Microsoft.FluentUI.AspNetCore.Components.Utilities; +using Microsoft.FluentUI.AspNetCore.Components.Utilities.InternalDebounce; +using Microsoft.VisualStudio.TestPlatform.Utilities; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.FluentUI.AspNetCore.Components.Tests.Utilities; + +public class DebounceTaskTests +{ + private readonly ITestOutputHelper Output; + + public DebounceTaskTests(ITestOutputHelper output) + { + Output = output; + } + + [Fact] + public async Task Debounce_Default() + { + // Arrange + var debounce = new DebounceTask(); + var actionCalled = false; + var watcher = Stopwatch.StartNew(); + + // Act + debounce.Run(50, async () => + { + actionCalled = true; + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.True(watcher.ElapsedMilliseconds >= 50); + Assert.True(actionCalled); + } + + [Fact] + public async Task Debounce_MultipleCalls() + { + // Arrange + var debounce = new DebounceTask(); + var actionCalledCount = 0; + var actionCalled = string.Empty; + + // Act + debounce.Run(50, async () => + { + actionCalled = "Step1"; + actionCalledCount++; + await Task.CompletedTask; + }); + + debounce.Run(40, async () => + { + actionCalled = "Step2"; + actionCalledCount++; + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.Equal("Step2", actionCalled); + Assert.Equal(1, actionCalledCount); + } + + [Fact] + public async Task Debounce_MultipleCalls_Async() + { + // Arrange + var debounce = new DebounceTask(); + var actionCalledCount = 0; + var actionCalled = string.Empty; + + // Act: simulate two async calls + _ = Task.Run(() => + { + debounce.Run(50, async () => + { + actionCalled = "Step1"; + actionCalledCount++; + await Task.CompletedTask; + }); + }); + + await Task.Delay(5); // To ensure the second call is made after the first one + + _ = Task.Run(() => + { + debounce.Run(40, async () => + { + actionCalled = "Step2"; + actionCalledCount++; + await Task.CompletedTask; + }); + }); + + await Task.Delay(100); // Wait for the debounce to complete + + // Assert + Assert.Equal("Step2", actionCalled); + Assert.Equal(1, actionCalledCount); + } + + [Fact] + public async Task Debounce_Disposed() + { + // Arrange + var debounce = new DebounceTask(); + var actionCalled = false; + + // Act + debounce.Dispose(); + + debounce.Run(50, async () => + { + actionCalled = true; + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.False(actionCalled); + } + + [Fact] + public async Task Debounce_Busy() + { + // Arrange + var debounce = new DebounceTask(); + + // Act + debounce.Run(50, async () => + { + await Task.CompletedTask; + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.False(debounce.Busy); + } + + [Fact] + public async Task Debounce_Exception() + { + // Arrange + var debounce = new DebounceTask(); + + // Act + debounce.Run(50, async () => + { + await Task.CompletedTask; + throw new InvalidProgramException("Error"); // Simulate an exception + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + + // Assert + Assert.False(debounce.Busy); + } + + [Fact] + public void Debounce_DelayMustBePositive() + { + // Arrange + var debounce = new DebounceTask(); + + // Act + Assert.Throws(() => + { + debounce.Run(-10, () => Task.CompletedTask); + }); + } + + [Fact] + public async Task Debounce_FirstRunAlreadyStarted() + { + // Arrange + var debounce = new DebounceTask(); + var actionCalledCount = 0; + + // Act + debounce.Run(10, async () => + { + Output.WriteLine("Step1 - Started"); + + await Task.Delay(100); + actionCalledCount++; + + Output.WriteLine("Step1 - Completed"); + }); + + await Task.Delay(20); // Wait for Step1 to start. + + debounce.Run(10, async () => + { + Output.WriteLine("Step2 - Started"); + + await Task.CompletedTask; + actionCalledCount++; + + Output.WriteLine("Step2 - Completed"); + }); + + // Wait for the debounce to complete + await debounce.CurrentTask; + await Task.Delay(200); + + // Assert + Assert.Equal(2, actionCalledCount); + } +}