Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
namespace Titano.Data.Processing;
|
|
|
|
/// <summary>
|
|
/// Delivers the synchronous progress reports of the engine (analysis, render stages) to an asynchronous sink (job
|
|
/// context, render-node protocol) one after the other, in report order. Await <see cref="FlushAsync"/> before a
|
|
/// checkpoint or the end of the work to observe the deliveries.
|
|
/// </summary>
|
|
internal sealed class ProgressChain(Func<long, long, string?, ValueTask> sink)
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private Task _pending = Task.CompletedTask;
|
|
|
|
/// <summary>A progress receiver mapping its reports to done, total and message.</summary>
|
|
public IProgress<T> For<T>(Func<T, (long Done, long Total, string? Message)> map) => new Forward<T>(this, map);
|
|
|
|
public Task FlushAsync()
|
|
{
|
|
lock (_gate) return _pending;
|
|
}
|
|
|
|
private void Post(long done, long total, string? message)
|
|
{
|
|
lock (_gate) _pending = AfterAsync(_pending, done, total, message);
|
|
}
|
|
|
|
private async Task AfterAsync(Task previous, long done, long total, string? message)
|
|
{
|
|
await previous.ConfigureAwait(false);
|
|
await sink(done, total, message).ConfigureAwait(false);
|
|
}
|
|
|
|
private sealed class Forward<T>(ProgressChain chain, Func<T, (long Done, long Total, string? Message)> map) : IProgress<T>
|
|
{
|
|
public void Report(T value)
|
|
{
|
|
var (done, total, message) = map(value);
|
|
chain.Post(done, total, message);
|
|
}
|
|
}
|
|
}
|