diff --git a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs index 52a1b6e9ae6..ffb273c3d07 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs @@ -62,7 +62,7 @@ public static void Debounce(this DispatcherQueueTimer timer, Action action, Time timer.Tick += Timer_Tick; // Store/Update function - _debounceInstances.AddOrUpdate(timer, action, (k, v) => v); + _debounceInstances.AddOrUpdate(timer, action, (k, v) => action); } // Start the timer to keep track of the last call here. diff --git a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs new file mode 100644 index 00000000000..b77b54e4e3f --- /dev/null +++ b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs @@ -0,0 +1,49 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Toolkit.Uwp.UI; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Threading.Tasks; + +namespace UnitTests.Extensions +{ + [TestClass] + public class Test_DispatcherQueueTimerExtensions + { + [TestCategory("DispatcherQueueTimerExtensions")] + [TestMethod] + public async Task Test_DispatcherQueueTimerExtensions_Debounce() + { + var debounceTimer = App.DispatcherQueue.CreateTimer(); + + var triggeredCount = 0; + string triggeredValue = null; + + var value = "He"; + debounceTimer.Debounce( + () => + { + triggeredCount++; + triggeredValue = value; + }, + TimeSpan.FromMilliseconds(60)); + + var value2 = "Hello"; + debounceTimer.Debounce( + () => + { + triggeredCount++; + triggeredValue = value2; + }, + TimeSpan.FromMilliseconds(60)); + + await Task.Delay(TimeSpan.FromMilliseconds(110)); + + Assert.AreEqual(false, debounceTimer.IsRunning, "Expected to stop the timer."); + Assert.AreEqual(value2, triggeredValue, "Expected to execute the last action."); + Assert.AreEqual(1, triggeredCount, "Expected to postpone execution."); + } + } +} diff --git a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj index a93621cfa61..cdbb101c62a 100644 --- a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj +++ b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj @@ -165,6 +165,7 @@ +