Skip to content

Commit 84adf91

Browse files
author
msftbot[bot]
authored
Fixed the debounce extension in order to update to the last action pa… (#4294)
## Fixes #4293 ## PR Type What kind of change does this PR introduce? Bugfix ## What is the current behavior? The debounce extension was incorrectly updating the action in the ConcurrentList. According to documentation the value passed in the `updateValueFactory` function, of the `AddOrUpdate` method, is the current value in the dictionary. https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.addorupdate?view=net-5.0 ![image](https://user-images.githubusercontent.com/29243277/135733394-f7c4d682-9c24-45cd-af4d-69721a7c6383.png) Therefore the current behavior is maintaining the old action and not updating with the new passed action. ```csharp _debounceInstances.AddOrUpdate(timer, action, (k, v) => v); ``` ## What is the new behavior? Changed so that the action in the `_ debounceInstances` Dictionary is updated with the latest version. ## PR Checklist Please check if your PR fulfills the following requirements: <!-- and remove the ones that are not applicable to the current PR --> - [x] Tested code with current [supported SDKs](../#supported) - [x] Contains **NO** breaking changes - [x] Tests for the changes have been added - [x] Header has been added to all new source files (run build/UpdateHeaders.bat) ## Other information PS: The debuonce extension is referenced in the `CanvasPathGeometryPage` class (although this changes does not seem to break it).
2 parents 1dd9c46 + 9419c7e commit 84adf91

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void Debounce(this DispatcherQueueTimer timer, Action action, Time
6262
timer.Tick += Timer_Tick;
6363

6464
// Store/Update function
65-
_debounceInstances.AddOrUpdate(timer, action, (k, v) => v);
65+
_debounceInstances.AddOrUpdate(timer, action, (k, v) => action);
6666
}
6767

6868
// Start the timer to keep track of the last call here.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Toolkit.Uwp.UI;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using System;
8+
using System.Threading.Tasks;
9+
10+
namespace UnitTests.Extensions
11+
{
12+
[TestClass]
13+
public class Test_DispatcherQueueTimerExtensions
14+
{
15+
[TestCategory("DispatcherQueueTimerExtensions")]
16+
[TestMethod]
17+
public async Task Test_DispatcherQueueTimerExtensions_Debounce()
18+
{
19+
var debounceTimer = App.DispatcherQueue.CreateTimer();
20+
21+
var triggeredCount = 0;
22+
string triggeredValue = null;
23+
24+
var value = "He";
25+
debounceTimer.Debounce(
26+
() =>
27+
{
28+
triggeredCount++;
29+
triggeredValue = value;
30+
},
31+
TimeSpan.FromMilliseconds(60));
32+
33+
var value2 = "Hello";
34+
debounceTimer.Debounce(
35+
() =>
36+
{
37+
triggeredCount++;
38+
triggeredValue = value2;
39+
},
40+
TimeSpan.FromMilliseconds(60));
41+
42+
await Task.Delay(TimeSpan.FromMilliseconds(110));
43+
44+
Assert.AreEqual(false, debounceTimer.IsRunning, "Expected to stop the timer.");
45+
Assert.AreEqual(value2, triggeredValue, "Expected to execute the last action.");
46+
Assert.AreEqual(1, triggeredCount, "Expected to postpone execution.");
47+
}
48+
}
49+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<Compile Include="Converters\Test_StringFormatConverter.cs" />
166166
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
167167
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
168+
<Compile Include="Extensions\Test_DispatcherQueueTimerExtensions.cs" />
168169
<Compile Include="Extensions\Test_StringExtensions.cs" />
169170
<Compile Include="Extensions\Test_UIElementExtensions_Coordinates.cs" />
170171
<Compile Include="Extensions\Test_VisualTreeExtensions.cs" />

0 commit comments

Comments
 (0)