Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
61 changes: 61 additions & 0 deletions PowerKit.Tests/ProgressMuxerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
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);
}
}
61 changes: 61 additions & 0 deletions PowerKit/ProgressMuxer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#if NET40_OR_GREATER || NETSTANDARD || NET
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

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
{
private readonly object _lock = new();
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
private readonly IProgress<double> _output;
private readonly Dictionary<int, double> _splitTotals;

private int _splitCount;

/// <summary>
/// Initializes a new instance of <see cref="ProgressMuxer" /> that forwards combined
/// progress to the specified output reporter.
/// </summary>
public ProgressMuxer(IProgress<double> output)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
_output = output;
_splitTotals = new Dictionary<int, double>();
}

/// <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 = _splitCount++;
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
return new DelegateProgress(p =>
{
lock (_lock)
{
_splitTotals[index] = weight * p;
_output.Report(_splitTotals.Values.Sum());
}
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
Comment thread
Tyrrrz marked this conversation as resolved.
});
}
}

#if !POWERKIT_INCLUDE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
file sealed class DelegateProgress(Action<double> report) : IProgress<double>
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
public void Report(double value) => report(value);
}
#endif