-
Notifications
You must be signed in to change notification settings - Fork 1
Add ProgressMuxer and DelegateProgress utilities #48
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
7 commits
Select commit
Hold shift + click to select a range
beebf59
feat: add ProgressMuxer utility
Copilot d12d7cf
Update ProgressMuxerTests.cs
Tyrrrz 550b8e2
refactor: use Lock object and EnterScope in ProgressMuxer
Copilot 5f92d25
Update PowerKit.Tests/ProgressMuxerTests.cs
Tyrrrz 1b660fa
refactor: primary constructor and generic DelegateProgress<T> in Prog…
Copilot 0bd1f9f
fix: use Interlocked.Increment for thread-safe _splitCount in CreateI…
Copilot a7b0e90
refactor: extract DelegateProgress<T> into its own file as a standalo…
Copilot 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,59 @@ | ||
| using FluentAssertions; | ||
| using Gress; | ||
| using PowerKit; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests; | ||
|
|
||
| public class ProgressMuxerTests | ||
| { | ||
| [Fact] | ||
| public void CreateInput_Test() | ||
| { | ||
| // Arrange | ||
| var progress = new ProgressCollector<double>(); | ||
| var muxer = new ProgressMuxer(progress); | ||
| var input = muxer.CreateInput(); | ||
|
|
||
| // Act | ||
| input.Report(0.5); | ||
| input.Report(1.0); | ||
|
|
||
| // Assert | ||
| progress.GetValues().Should().Equal(0.5, 1.0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateInput_Weight_Test() | ||
| { | ||
| // Arrange | ||
| var progress = new ProgressCollector<double>(); | ||
| var muxer = new ProgressMuxer(progress); | ||
| var input = muxer.CreateInput(weight: 0.5); | ||
|
|
||
| // Act | ||
| input.Report(0.5); | ||
| input.Report(1.0); | ||
|
|
||
| // Assert | ||
| progress.GetValues().Should().Equal(0.25, 0.5); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateInput_MultipleInputs_Test() | ||
| { | ||
| // Arrange | ||
| var progress = new ProgressCollector<double>(); | ||
| var muxer = new ProgressMuxer(progress); | ||
| var input1 = muxer.CreateInput(weight: 0.6); | ||
| var input2 = muxer.CreateInput(weight: 0.4); | ||
|
|
||
| // Act | ||
| input1.Report(1.0); | ||
| input2.Report(1.0); | ||
|
|
||
| // Assert | ||
| var values = progress.GetValues(); | ||
| values[^1].Should().BeApproximately(1.0, 1e-10); | ||
| } | ||
| } |
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,20 @@ | ||
| #if NET40_OR_GREATER || NETSTANDARD || NET | ||
| #nullable enable | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace PowerKit; | ||
|
|
||
| /// <summary> | ||
| /// Provides a lightweight <see cref="IProgress{T}" /> implementation that delegates | ||
| /// progress reporting to an action. | ||
| /// </summary> | ||
| #if !POWERKIT_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal sealed class DelegateProgress<T>(Action<T> report) : IProgress<T> | ||
| { | ||
| /// <inheritdoc /> | ||
| public void Report(T value) => report(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,43 @@ | ||
| #if NET40_OR_GREATER || NETSTANDARD || NET | ||
| #nullable enable | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using System.Threading; | ||
|
|
||
| namespace PowerKit; | ||
|
|
||
| /// <summary> | ||
| /// Multiplexes multiple <see cref="IProgress{T}" /> reporters into a single output reporter, | ||
| /// combining weighted progress values from multiple sources. | ||
| /// </summary> | ||
| #if !POWERKIT_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal class ProgressMuxer(IProgress<double> output) | ||
| { | ||
| private readonly Lock _lock = new(); | ||
| private readonly Dictionary<int, double> _splitTotals = new(); | ||
|
|
||
| private int _splitCount; | ||
|
|
||
| /// <summary> | ||
| /// Creates a new progress input with the specified weight. | ||
| /// Progress reported to this input is multiplied by <paramref name="weight" /> | ||
| /// and combined with all other inputs before being forwarded to the output. | ||
| /// </summary> | ||
| public IProgress<double> CreateInput(double weight = 1) | ||
| { | ||
| var index = Interlocked.Increment(ref _splitCount) - 1; | ||
| return new DelegateProgress<double>(p => | ||
| { | ||
| using (_lock.EnterScope()) | ||
| { | ||
| _splitTotals[index] = weight * p; | ||
| output.Report(_splitTotals.Values.Sum()); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| #endif | ||
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.