using Microsoft.Extensions.DependencyInjection; namespace Hyperbar.Widget.MediaController.Windows; public class MediaControllerHandler(IPublisher publisher, IServiceScopeProvider scopeProvider, ICache cache) : INotificationHandler>, INotificationHandler> { public async Task Handle(Create notification, CancellationToken cancellationToken) { if (notification.Value is MediaController mediaController && scopeProvider.TryGet(mediaController, out IServiceScope? serviceScope) && serviceScope?.ServiceProvider.GetService>() is IFactory factory && factory.Create(mediaController) is MediaControllerViewModel viewModel) { cache.Add(mediaController, viewModel); await publisher.PublishAsync(new Create(viewModel), cancellationToken); } } public async Task Handle(Remove notification, CancellationToken cancellationToken) { if (notification.Value is MediaController mediaController && cache.TryGetValue(mediaController, out MediaControllerViewModel? viewModel) && viewModel is not null) { await publisher.PublishAsync(new Remove(viewModel), cancellationToken); cache.Remove(mediaController); } } }