Fixed bug where items are destroyed during configuration update

This commit is contained in:
TheXamlGuy
2024-01-17 18:58:57 +00:00
parent 2f22c81384
commit 67b0456cbd
8 changed files with 162 additions and 108 deletions
@@ -12,5 +12,5 @@ public class PrimaryCommandConfiguration
public required string Text { get; set; }
public List<PrimaryCommandConfiguration>? Commands { get; set; } = [];
public List<PrimaryCommandConfiguration> Commands { get; set; } = [];
}
@@ -9,15 +9,14 @@ public class PrimaryWidgetConfigurationHandler(IMediator mediator,
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
CancellationToken cancellationToken)
{
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache)
{
if (configuration.FirstOrDefault(x => x.Id == item.Key) == null)
{
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value),
cancellationToken);
HashSet<Guid> configurationIds = new(configuration.SelectMany(item => new[] { item }
.Concat(item.Commands).Select(x => x.Id)));
cache.Remove(item.Key);
}
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache.Where(x => !configurationIds.Contains(x.Key)))
{
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value),
cancellationToken);
cache.Remove(item.Key);
}
foreach (PrimaryCommandConfiguration item in configuration)
@@ -25,7 +25,6 @@ public class WidgetComponentViewModelFactory(IServiceFactory service,
if (processCommandConfiguration.Commands is { Count: > 0 } childCommandConfigurations)
{
List<IWidgetComponentViewModel> childViewModels = [];
foreach (PrimaryCommandConfiguration childCommandConfiguration in childCommandConfigurations)
{
WidgetComponentViewModel? childViewModel = null;