-
Notifications
You must be signed in to change notification settings - Fork 5
Add polyfills for IProgress<T>, Progress<T>, and EventHandler<T> #120
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0ae389e
Add polyfills for IProgress<T> and Progress<T>
Copilot 914990a
Fix test naming convention in ProgressTests
Copilot c659c3e
Add missing using var for ManualResetEventSlim in ProgressTests
Copilot 8fb6a2e
Add EventHandler<T> polyfill; refactor Progress<T> with primary const…
Copilot c22f9c2
Revert primary constructor; use BCL-matching Progress() and Progress(…
Copilot a3a644b
Use target-typed new() and inverted early-return guard in OnReport
Copilot ff0f70d
Update Progress.cs
Tyrrrz c46f8ab
Inline OnReport guard: use if condition directly on Post call
Copilot b43eb04
Capture delegates at Report time to eliminate race in OnReport/Invoke…
Copilot a9f6af6
Update Progress.cs
Tyrrrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.NetCore10; | ||
|
|
||
| public class ProgressTests | ||
| { | ||
| [Fact] | ||
| public void Report_WithHandler_Test() | ||
| { | ||
| // Arrange | ||
| using var received = new ManualResetEventSlim(); | ||
| var receivedValue = default(int); | ||
| var progress = new Progress<int>(v => | ||
| { | ||
| receivedValue = v; | ||
| received.Set(); | ||
| }); | ||
|
|
||
| // Act | ||
| ((IProgress<int>)progress).Report(42); | ||
|
|
||
| // Assert | ||
| received.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue(); | ||
| receivedValue.Should().Be(42); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Report_WithEvent_Test() | ||
| { | ||
| // Arrange | ||
| using var received = new ManualResetEventSlim(); | ||
| var receivedValue = default(int); | ||
| var progress = new Progress<int>(); | ||
| progress.ProgressChanged += (_, v) => | ||
| { | ||
| receivedValue = v; | ||
| received.Set(); | ||
| }; | ||
|
|
||
| // Act | ||
| ((IProgress<int>)progress).Report(99); | ||
|
|
||
| // Assert | ||
| received.Wait(TimeSpan.FromSeconds(5)).Should().BeTrue(); | ||
| receivedValue.Should().Be(99); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Report_NoHandler_Test() | ||
| { | ||
| // Arrange | ||
| var progress = new Progress<int>(); | ||
|
|
||
| // Act & assert | ||
| var act = () => ((IProgress<int>)progress).Report(1); | ||
| act.Should().NotThrow(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #if NETFRAMEWORK && !NET45_OR_GREATER | ||
| #nullable enable | ||
| #pragma warning disable CS0436 | ||
|
|
||
| namespace System; | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.eventhandler-1 | ||
| internal delegate void EventHandler<TEventArgs>(object sender, TEventArgs e); | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #if (NETFRAMEWORK && !NET45_OR_GREATER) || (NETSTANDARD && !NETSTANDARD1_1_OR_GREATER) | ||
| #nullable enable | ||
| #pragma warning disable CS0436 | ||
|
|
||
| namespace System; | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.iprogress-1 | ||
| internal interface IProgress<in T> | ||
| { | ||
| void Report(T value); | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #if (NETFRAMEWORK && !NET45_OR_GREATER) || (NETSTANDARD && !NETSTANDARD1_3_OR_GREATER) | ||
| #nullable enable | ||
| #pragma warning disable CS0436 | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Threading; | ||
|
|
||
| namespace System; | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.progress-1 | ||
| #if !POLYSHIM_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal class Progress<T> : IProgress<T> | ||
| { | ||
| private readonly SynchronizationContext _synchronizationContext; | ||
| private readonly Action<T>? _handler; | ||
|
|
||
| public event EventHandler<T>? ProgressChanged; | ||
|
|
||
| public Progress() | ||
| { | ||
| _synchronizationContext = SynchronizationContext.Current ?? new(); | ||
|
Tyrrrz marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public Progress(Action<T> handler) | ||
| : this() => _handler = handler; | ||
|
|
||
| protected virtual void OnReport(T value) | ||
| { | ||
| var handler = _handler; | ||
| var progressChanged = ProgressChanged; | ||
|
|
||
| if (handler is not null || progressChanged is not null) | ||
| { | ||
| _synchronizationContext.Post( | ||
| s => | ||
| { | ||
| var (v, h, e) = ((T, Action<T>?, EventHandler<T>?))s!; | ||
| h?.Invoke(v); | ||
| e?.Invoke(this, v); | ||
| }, | ||
| (value, handler, progressChanged) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| void IProgress<T>.Report(T value) => OnReport(value); | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.