refactor(ui): own retained controller lifetimes

This commit is contained in:
Erik 2026-07-10 23:35:26 +02:00
parent 921c388e2c
commit 5d9e98c118
21 changed files with 373 additions and 35 deletions

View file

@ -82,7 +82,15 @@ public class SelectedObjectControllerTests
=> SelectedObjectController.Bind(
layout,
subscribeSelectionChanged: h => SelectionHandler = h,
unsubscribeSelectionChanged: h =>
{
if (SelectionHandler == h) SelectionHandler = null;
},
subscribeHealthChanged: h => HealthHandler = h,
unsubscribeHealthChanged: h =>
{
if (HealthHandler == h) HealthHandler = null;
},
isHealthTarget: g => HealthTargetMap.TryGetValue(g, out var v) && v,
name: g => NameMap.TryGetValue(g, out var v) ? v : null,
healthPercent: g => HealthMap.TryGetValue(g, out var v) ? v : 1f,
@ -403,4 +411,23 @@ public class SelectedObjectControllerTests
h.FireSelection(null);
Assert.Equal(0f, healthMeterEl.Fill() ?? 0f);
}
[Fact]
public void Dispose_UnsubscribesBothHandlers_AndIsIdempotent()
{
var (layout, _, _, healthMeterEl) = FakeLayout();
var h = new Harness();
h.HealthTargetMap[0xAA09u] = true;
var controller = h.Bind(layout);
controller.Dispose();
controller.Dispose();
h.FireSelection(0xAA09u);
h.FireHealth(0xAA09u, 0.5f);
Assert.Null(h.SelectionHandler);
Assert.Null(h.HealthHandler);
Assert.False(healthMeterEl.Visible);
Assert.Empty(h.QueryHealthCalls);
}
}