refactor(app): compose live presentation startup

This commit is contained in:
Erik 2026-07-22 17:55:15 +02:00
parent aa6ffa5176
commit 88f32dc4e2
23 changed files with 1767 additions and 626 deletions

View file

@ -0,0 +1,97 @@
using AcDream.App.Rendering;
using AcDream.App.World;
namespace AcDream.App.Composition;
/// <summary>
/// Exact-owner leases installed by live-presentation composition. Successful
/// releases are removed immediately, so a failed cleanup retries only its
/// remaining suffix and never replays completed detach operations.
/// </summary>
internal sealed class LivePresentationRuntimeBindings : IDisposable
{
private readonly List<(string Name, IDisposable Binding)> _bindings = [];
private bool _deactivationStarted;
public void Adopt(string name, IDisposable binding)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(binding);
ObjectDisposedException.ThrowIf(_deactivationStarted, this);
_bindings.Add((name, binding));
}
public void AdoptRelease(string name, Action release) =>
Adopt(name, new DelegateBinding(release));
public void BindProjectionVisibility(
LiveEntityRuntime source,
Action<LiveEntityRecord, bool> handler,
string name)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(handler);
source.ProjectionVisibilityChanged += handler;
Adopt(name, new DelegateBinding(
() => source.ProjectionVisibilityChanged -= handler));
}
public void BindProjectionPoseReady(
EquippedChildRenderController source,
Action<uint> handler)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(handler);
source.ProjectionPoseReady += handler;
Adopt("equipped-child projection pose", new DelegateBinding(
() => source.ProjectionPoseReady -= handler));
}
public void Dispose()
{
if (_deactivationStarted && _bindings.Count == 0)
return;
_deactivationStarted = true;
List<Exception>? failures = null;
for (int i = _bindings.Count - 1; i >= 0; i--)
{
(string name, IDisposable binding) = _bindings[i];
try
{
binding.Dispose();
_bindings.RemoveAt(i);
}
catch (Exception failure)
{
(failures ??= []).Add(new InvalidOperationException(
$"Live-presentation binding '{name}' did not detach.",
failure));
}
}
if (failures is not null)
{
throw new AggregateException(
"Live-presentation binding cleanup remains incomplete.",
failures);
}
}
private sealed class DelegateBinding : IDisposable
{
private Action? _release;
public DelegateBinding(Action release) =>
_release = release ?? throw new ArgumentNullException(nameof(release));
public void Dispose()
{
Action? release = _release;
if (release is null)
return;
release();
_release = null;
}
}
}