Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the debounce extension in order to update to the last action pa… #4294

Merged
5 commits merged into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
<Compile Include="Converters\Test_StringFormatConverter.cs" />
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
<Compile Include="Extensions\Test_DispatcherQueueTimerExtensions.cs" />
<Compile Include="Extensions\Test_StringExtensions.cs" />
<Compile Include="Extensions\Test_UIElementExtensions_Coordinates.cs" />
<Compile Include="Extensions\Test_VisualTreeExtensions.cs" />
Expand Down