Skip to content

Commit 1586743

Browse files
committed
Update code paths for .NET 7 as base
1 parent a51efde commit 1586743

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/CommunityToolkit.Common/Deferred/EventDeferral.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
using System;
66
using System.ComponentModel;
7+
#if NET8_0_OR_GREATER
8+
using System.Runtime.CompilerServices;
9+
#endif
710
using System.Threading;
811
using System.Threading.Tasks;
912

@@ -16,8 +19,11 @@ namespace CommunityToolkit.Common.Deferred;
1619
/// </summary>
1720
public class EventDeferral : IDisposable
1821
{
19-
// TODO: If/when .NET 6 is base, we can upgrade to non-generic version
22+
#if NET8_0_OR_GREATER
23+
private readonly TaskCompletionSource taskCompletionSource = new();
24+
#else
2025
private readonly TaskCompletionSource<object?> taskCompletionSource = new();
26+
#endif
2127

2228
internal EventDeferral()
2329
{
@@ -26,7 +32,14 @@ internal EventDeferral()
2632
/// <summary>
2733
/// Call when finished with the Deferral.
2834
/// </summary>
29-
public void Complete() => this.taskCompletionSource.TrySetResult(null);
35+
public void Complete()
36+
{
37+
#if NET8_0_OR_GREATER
38+
this.taskCompletionSource.TrySetResult();
39+
#else
40+
this.taskCompletionSource.TrySetResult(null);
41+
#endif
42+
}
3043

3144
/// <summary>
3245
/// Waits for the <see cref="EventDeferral"/> to be completed by the event handler.
@@ -38,9 +51,19 @@ internal EventDeferral()
3851
[Obsolete("This is an internal only method to be used by EventHandler extension classes, public callers should call GetDeferral() instead on the DeferredEventArgs.")]
3952
public async Task WaitForCompletion(CancellationToken cancellationToken)
4053
{
41-
using (cancellationToken.Register(() => this.taskCompletionSource.TrySetCanceled()))
54+
using (cancellationToken.Register(
55+
#if NET8_0_OR_GREATER
56+
callback: static obj => Unsafe.As<EventDeferral>(obj!).taskCompletionSource.TrySetCanceled(),
57+
#else
58+
callback: static obj => ((EventDeferral)obj).taskCompletionSource.TrySetCanceled(),
59+
#endif
60+
state: this))
4261
{
62+
#if NET8_0_OR_GREATER
63+
await this.taskCompletionSource.Task;
64+
#else
4365
_ = await this.taskCompletionSource.Task;
66+
#endif
4467
}
4568
}
4669

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -709,12 +709,8 @@ public void Test_ObservableProperty_NullabilityAnnotations_Complex()
709709
NullabilityInfo rightInfo2 = rightInnerInfo.GenericTypeArguments[2];
710710

711711
Assert.AreEqual(typeof(object), rightInfo2.Type);
712-
//Assert.AreEqual(NullabilityState.NotNull, rightInfo2.ReadState);
713-
//Assert.AreEqual(NullabilityState.NotNull, rightInfo2.WriteState);
714-
715-
// The commented out lines are to work around a bug in the NullabilityInfo API in .NET 6.
716-
// This has been fixed for .NET 7: https://github.com/dotnet/runtime/pull/63556. The test
717-
// cases above can be uncommented when the .NET 7 target (or a more recent version) is added.
712+
Assert.AreEqual(NullabilityState.NotNull, rightInfo2.ReadState);
713+
Assert.AreEqual(NullabilityState.NotNull, rightInfo2.WriteState);
718714
}
719715
#endif
720716

0 commit comments

Comments
 (0)