add keyed based publication to mediator
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
namespace Hyperbar.Windows.Primary;
|
||||
|
||||
public class PrimaryWidgetConfigurationHandler(IMediator mediator,
|
||||
PrimaryWidgetConfiguration configuration,
|
||||
@@ -9,26 +10,52 @@ public class PrimaryWidgetConfigurationHandler(IMediator mediator,
|
||||
public async Task Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
HashSet<Guid> configurationIds = new(configuration.SelectMany(item => new[] { item }
|
||||
.Concat(item.Commands).Select(x => x.Id)));
|
||||
List<(Guid ParentId, Guid Id, PrimaryCommandConfiguration Configuration)> items = [];
|
||||
|
||||
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache.Where(x => !configurationIds.Contains(x.Key)))
|
||||
void AddToItems(Guid parentId, Guid id, List<PrimaryCommandConfiguration> configurations)
|
||||
{
|
||||
if (configurations is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<(Guid, List<PrimaryCommandConfiguration>)> stack = new();
|
||||
stack.Push((parentId, configurations));
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
(Guid currentParentId, List<PrimaryCommandConfiguration> currentConfigurations) = stack.Pop();
|
||||
foreach (PrimaryCommandConfiguration configuration in currentConfigurations)
|
||||
{
|
||||
items.Add((currentParentId, configuration.Id, configuration));
|
||||
if (configuration.Commands is not null && configuration.Commands.Count > 0)
|
||||
{
|
||||
stack.Push((configuration.Id, configuration.Commands));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AddToItems(Guid.Empty, Guid.Empty, configuration.Commands);
|
||||
|
||||
foreach (KeyValuePair<Guid, IWidgetComponentViewModel> item in cache
|
||||
.Where(x => !items.Any(k => x.Key == k.Id)))
|
||||
{
|
||||
await mediator.PublishAsync(new Removed<IWidgetComponentViewModel>(item.Value),
|
||||
cancellationToken);
|
||||
nameof(PrimaryWidgetViewModel),
|
||||
cancellationToken);
|
||||
|
||||
cache.Remove(item.Key);
|
||||
}
|
||||
|
||||
foreach (PrimaryCommandConfiguration item in configuration)
|
||||
foreach ((Guid ParentId, Guid Id, PrimaryCommandConfiguration Configuration) item in
|
||||
items.Where(x => !cache.Any(k => x.Id == k.Key)))
|
||||
{
|
||||
if (!cache.ContainsKey(item.Id))
|
||||
if (factory.Create(item.Configuration) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
if (factory.Create(item) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
await mediator.PublishAsync(Inserted<IWidgetComponentViewModel>
|
||||
.For<PrimaryWidgetViewModel>(item.Order, viewModel),
|
||||
cancellationToken);
|
||||
}
|
||||
await mediator.PublishAsync(new Inserted<IWidgetComponentViewModel>(item.Configuration.Order, viewModel),
|
||||
nameof(PrimaryWidgetViewModel),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user