Replace Mediator with Messenger
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Activation
|
||||
{
|
||||
public static ActivationEventArgs<TSender, TValue> As<TSender, TValue>(TValue value) => new(value);
|
||||
|
||||
public static ActivationEventArgs<TSender> As<TSender>() =>
|
||||
new();
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record ActivationBuilder(IActivation Value, object? Key = null);
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record ActivationEventArgs<TSynchronize, TValue>(TValue? Value = default) :
|
||||
IActivation;
|
||||
|
||||
public record ActivationEventArgs<TSynchronize>() :
|
||||
IActivation;
|
||||
@@ -1,10 +1,11 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class AppService(IEnumerable<IInitialization> initializations,
|
||||
IEnumerable<IAsyncInitialization> asyncInitializations,
|
||||
IPublisher publisher) :
|
||||
IMessenger messenger) :
|
||||
IHostedService
|
||||
{
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -18,8 +19,8 @@ public class AppService(IEnumerable<IInitialization> initializations,
|
||||
{
|
||||
await initialization.Initialize();
|
||||
}
|
||||
|
||||
publisher.Publish<StartedEventArgs>();
|
||||
|
||||
messenger.Send<StartedEventArgs>();
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize() => WeakReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, TResponse>>(provider,
|
||||
async (provider, args) => args.Reply(await provider.GetRequiredService<THandler>().Handle(args.Message, args.CancellationToken)));
|
||||
}
|
||||
|
||||
public class AsyncHandlerInitialization<TMessage, THandler>(IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IAsyncHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize() => WeakReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, Unit>>(provider,
|
||||
async (provider, args) => await provider.GetRequiredService<THandler>().Handle(args.Message, args.CancellationToken));
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class AsyncResponseEventArgs<TMessage, TResponse> :
|
||||
AsyncRequestMessage<TResponse>
|
||||
{
|
||||
public TMessage? Message { get; set; }
|
||||
|
||||
public CancellationToken CancellationToken { get; set; }
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public partial class CommandValueViewModel<TValue>(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer) :
|
||||
Observable<TValue>(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
Observable<TValue>(provider, factory, messenger, disposer)
|
||||
where TValue : notnull
|
||||
{
|
||||
public IRelayCommand InvokeCommand =>
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public partial class CommandViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer) :
|
||||
Observable(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
Observable(provider, factory, messenger, disposer)
|
||||
{
|
||||
public IRelayCommand InvokeCommand =>
|
||||
new AsyncRelayCommand(InvokeAsync);
|
||||
|
||||
@@ -26,12 +26,12 @@ public class ComponentBuilder :
|
||||
|
||||
services.AddSingleton<IDisposer, Disposer>();
|
||||
|
||||
services.AddScoped<SubscriptionCollection>();
|
||||
//services.AddScoped<SubscriptionCollection>();
|
||||
|
||||
services.AddTransient<IHandlerProvider, HandlerProvider>();
|
||||
services.AddScoped<ISubscriber, Subscriber>();
|
||||
services.AddTransient<IPublisher, Publisher>();
|
||||
services.AddTransient<IMediator, Mediator>();
|
||||
//services.AddTransient<IHandlerProvider, HandlerProvider>();
|
||||
//services.AddScoped<ISubscriber, Subscriber>();
|
||||
//services.AddTransient<IPublisher, Publisher>();
|
||||
//services.AddTransient<IMediator, Mediator>();
|
||||
|
||||
services.AddTransient<IValidation, Validation>();
|
||||
services.AddTransient<IValidatorCollection, ValidatorCollection>();
|
||||
@@ -42,8 +42,8 @@ public class ComponentBuilder :
|
||||
services.AddScoped<INavigationRegionCollection, NavigationRegionCollection>();
|
||||
services.AddTransient<INavigationRegionProvider, NavigationRegionProvider>();
|
||||
|
||||
services.AddHandler<NavigateHandler>();
|
||||
services.AddHandler<NavigateBackHandler>();
|
||||
//services.AddHandler<NavigateHandler>();
|
||||
//services.AddHandler<NavigateBackHandler>();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
@@ -28,7 +29,7 @@ public class ComponentFactory(IServiceProvider provider,
|
||||
provider.GetRequiredService<IComponentFactory>());
|
||||
|
||||
services.AddTransient(_ =>
|
||||
provider.GetRequiredService<IProxyService<IPublisher>>());
|
||||
provider.GetRequiredService<IProxyService<IMessenger>>());
|
||||
|
||||
services.AddTransient(_ =>
|
||||
provider.GetRequiredService<IProxyService<IComponentHostCollection>>());
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
@@ -18,7 +19,7 @@ public class ComponentInitializer(IEnumerable<IComponent> components,
|
||||
builder.AddServices(services =>
|
||||
{
|
||||
services.AddTransient(_ =>
|
||||
provider.GetRequiredService<IProxyService<IPublisher>>());
|
||||
provider.GetRequiredService<IProxyService<IMessenger>>());
|
||||
|
||||
services.AddTransient(_ =>
|
||||
provider.GetRequiredService<IProxyService<IComponentHostCollection>>());
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ConfigurationChangedHandler<TConfiguration, TValue>(ConfigurationValue<TConfiguration, TValue> configurationValue) :
|
||||
INotificationHandler<ChangedEventArgs<TConfiguration>>
|
||||
IHandler<ChangedEventArgs<TConfiguration>>
|
||||
where TValue :
|
||||
class, new()
|
||||
{
|
||||
public Task Handle(ChangedEventArgs<TConfiguration> args)
|
||||
public void Handle(ChangedEventArgs<TConfiguration> args)
|
||||
{
|
||||
if (args.Sender is TConfiguration configuration)
|
||||
{
|
||||
@@ -13,7 +13,5 @@ public class ConfigurationChangedHandler<TConfiguration, TValue>(ConfigurationVa
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace Toolkit.Foundation;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ConfigurationInitializer<TConfiguration>(IConfigurationReader<TConfiguration> reader,
|
||||
IConfigurationWriter<TConfiguration> writer,
|
||||
IConfigurationFactory<TConfiguration> factory,
|
||||
IPublisher publisher) :
|
||||
IMessenger messenger) :
|
||||
IConfigurationInitializer<TConfiguration>,
|
||||
IInitialization
|
||||
where TConfiguration :
|
||||
@@ -20,6 +22,6 @@ public class ConfigurationInitializer<TConfiguration>(IConfigurationReader<TConf
|
||||
}
|
||||
}
|
||||
|
||||
publisher.PublishUI(new ActivatedEventArgs<TConfiguration>(configuration));
|
||||
messenger.Send(new ActivatedEventArgs<TConfiguration>(configuration));
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ConfigurationMonitor<TConfiguration>(string section,
|
||||
IConfigurationCache cache,
|
||||
IConfigurationFile<TConfiguration> file,
|
||||
IServiceProvider serviceProvider,
|
||||
IPublisher publisher) :
|
||||
IServiceProvider provider,
|
||||
IMessenger messenger) :
|
||||
IConfigurationMonitor<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
@@ -18,11 +19,11 @@ public class ConfigurationMonitor<TConfiguration>(string section,
|
||||
void ChangedHandler(object sender,
|
||||
FileSystemEventArgs args)
|
||||
{
|
||||
if (serviceProvider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section) is
|
||||
if (provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section) is
|
||||
IConfigurationDescriptor<TConfiguration> configuration)
|
||||
{
|
||||
cache.Remove(section);
|
||||
publisher.PublishUI(new ChangedEventArgs<TConfiguration>(configuration.Value));
|
||||
messenger.Send(new ChangedEventArgs<TConfiguration>(configuration.Value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public partial class ConfigurationValueViewModel<TConfiguration, TValue>(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
TConfiguration configuration,
|
||||
IWritableConfiguration<TConfiguration> writer,
|
||||
Func<TConfiguration, TValue?> read,
|
||||
Action<TValue?, TConfiguration> write) :
|
||||
Observable<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
|
||||
INotificationHandler<ChangedEventArgs<TConfiguration>>
|
||||
Observable<TValue>(provider, factory, messenger, disposer),
|
||||
IHandler<ChangedEventArgs<TConfiguration>>
|
||||
where TConfiguration : class
|
||||
{
|
||||
public async Task Handle(ChangedEventArgs<TConfiguration> args)
|
||||
public void Handle(ChangedEventArgs<TConfiguration> args)
|
||||
{
|
||||
if (args.Sender is TConfiguration configuration)
|
||||
{
|
||||
await Task.Run(() => Value = read(configuration));
|
||||
Value = read(configuration);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task OnActivated()
|
||||
protected override void OnActivated()
|
||||
{
|
||||
await Task.Run(() => Value = read(configuration));
|
||||
await base.OnActivated();
|
||||
Value = read(configuration);
|
||||
base.OnActivated();
|
||||
}
|
||||
|
||||
protected override async void OnChanged(TValue? value)
|
||||
protected override void OnChanged(TValue? value)
|
||||
{
|
||||
if (IsActivated)
|
||||
if (IsActive)
|
||||
{
|
||||
await Task.Run(() => writer.Write(args => write(value, args)));
|
||||
writer.Write(args => write(value, args));
|
||||
}
|
||||
|
||||
base.OnChanged(value);
|
||||
@@ -42,7 +42,7 @@ public partial class ConfigurationValueViewModel<TConfiguration, TValue>(IServic
|
||||
|
||||
public partial class ConfigurationValueViewModel<TConfiguration, TValue, TItem> :
|
||||
ObservableCollection<TValue, TItem>,
|
||||
INotificationHandler<ChangedEventArgs<TConfiguration>>
|
||||
IHandler<ChangedEventArgs<TConfiguration>>
|
||||
where TConfiguration : class
|
||||
where TItem : notnull,
|
||||
IDisposable
|
||||
@@ -51,17 +51,16 @@ public partial class ConfigurationValueViewModel<TConfiguration, TValue, TItem>
|
||||
private readonly Func<TConfiguration, TValue?> read;
|
||||
private readonly Action<TValue?, TConfiguration> write;
|
||||
private readonly IWritableConfiguration<TConfiguration> writer;
|
||||
|
||||
public ConfigurationValueViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IServiceFactory factory,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
TConfiguration configuration,
|
||||
IWritableConfiguration<TConfiguration> writer,
|
||||
Func<TConfiguration, TValue?> read,
|
||||
Action<TValue?, TConfiguration> write,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer, value)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer, value)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.writer = writer;
|
||||
@@ -73,16 +72,14 @@ public partial class ConfigurationValueViewModel<TConfiguration, TValue, TItem>
|
||||
|
||||
public ConfigurationValueViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
IEnumerable<TItem> items,
|
||||
TConfiguration configuration,
|
||||
IWritableConfiguration<TConfiguration> writer,
|
||||
Func<TConfiguration, TValue?> read,
|
||||
Action<TValue?, TConfiguration> write,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer, items, value)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer, items, value)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.writer = writer;
|
||||
@@ -92,25 +89,25 @@ public partial class ConfigurationValueViewModel<TConfiguration, TValue, TItem>
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public async Task Handle(ChangedEventArgs<TConfiguration> args)
|
||||
public void Handle(ChangedEventArgs<TConfiguration> args)
|
||||
{
|
||||
if (args.Sender is TConfiguration configuration)
|
||||
{
|
||||
await Task.Run(() => Value = read(configuration));
|
||||
Value = read(configuration);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task OnActivated()
|
||||
protected override void OnActivated()
|
||||
{
|
||||
await Task.Run(() => Value = read(configuration));
|
||||
await base.OnActivated();
|
||||
Value = read(configuration);
|
||||
base.OnActivated();
|
||||
}
|
||||
|
||||
protected override async void OnChanged(TValue? value)
|
||||
protected override void OnChanged(TValue? value)
|
||||
{
|
||||
if (IsActivated)
|
||||
if (IsActive)
|
||||
{
|
||||
await Task.Run(() => writer.Write(args => write(value, args)));
|
||||
writer.Write(args => write(value, args));
|
||||
}
|
||||
|
||||
base.OnChanged(value);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
@@ -20,19 +20,20 @@ public class DefaultHostBuilder :
|
||||
ComponentHostCollection>();
|
||||
|
||||
services.AddSingleton<IDisposer, Disposer>();
|
||||
services.AddSingleton<IMessenger, WeakReferenceMessenger>(_ => WeakReferenceMessenger.Default);
|
||||
|
||||
services.AddScoped<SubscriptionCollection>();
|
||||
//services.AddScoped<SubscriptionCollection>();
|
||||
|
||||
services.AddTransient<IHandlerProvider, HandlerProvider>();
|
||||
services.AddScoped<ISubscriber, Subscriber>();
|
||||
services.AddTransient<IPublisher, Publisher>();
|
||||
services.AddTransient<IMediator, Mediator>();
|
||||
//services.AddTransient<IHandlerProvider, HandlerProvider>();
|
||||
//services.AddScoped<ISubscriber, Subscriber>();
|
||||
//services.AddTransient<IPublisher, Publisher>();
|
||||
//services.AddTransient<IMediator, Mediator>();
|
||||
|
||||
services.AddTransient<IValidation, Validation>();
|
||||
services.AddTransient<IValidatorCollection, ValidatorCollection>();
|
||||
|
||||
services.AddScoped<IProxyService<IPublisher>>(provider =>
|
||||
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
|
||||
services.AddScoped<IProxyService<IMessenger>>(provider =>
|
||||
new ProxyService<IMessenger>(provider.GetRequiredService<IMessenger>()));
|
||||
|
||||
services.AddScoped<IProxyService<INavigationRegionProvider>>(provider =>
|
||||
new ProxyService<INavigationRegionProvider>(provider.GetRequiredService<INavigationRegionProvider>()));
|
||||
@@ -56,10 +57,10 @@ public class DefaultHostBuilder :
|
||||
services.AddTransient<IComponentFactory, ComponentFactory>();
|
||||
services.AddTransient<IComponentScopeProvider, ComponentScopeProvider>();
|
||||
|
||||
services.AddHandler<NavigateHandler>();
|
||||
services.AddHandler<NavigateBackHandler>();
|
||||
//services.AddHandler<NavigateHandler>();
|
||||
//services.AddHandler<NavigateBackHandler>();
|
||||
|
||||
services.AddInitialization<ComponentInitializer>();
|
||||
//services.AddInitialization<ComponentInitializer>();
|
||||
services.AddHostedService<AppService>();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class HandlerInitialization<TMessage, TResponse, THandler>(IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize() => WeakReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>>(provider,
|
||||
(provider, args) => args.Reply(provider.GetRequiredService<THandler>().Handle(args.Message)));
|
||||
}
|
||||
|
||||
public class HandlerInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize() => WeakReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
|
||||
(provider, args) => provider.GetRequiredKeyedService<THandler>(key).Handle(args));
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class HandlerProvider(SubscriptionCollection subscriptions) :
|
||||
IHandlerProvider
|
||||
{
|
||||
public IEnumerable<object?> Get(object key)
|
||||
{
|
||||
var d = subscriptions;
|
||||
if (subscriptions.TryGetValue(key, out List<WeakReference>? subscribers))
|
||||
{
|
||||
foreach (WeakReference weakRef in subscribers.ToArray())
|
||||
{
|
||||
object? target = weakRef.Target;
|
||||
if (target != null)
|
||||
{
|
||||
yield return target;
|
||||
}
|
||||
else
|
||||
{
|
||||
subscribers.Remove(weakRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class HandlerWrapper<TRequest, TResponse>(IHandler<TRequest, TResponse> handler,
|
||||
IEnumerable<IPipelineBehaviour<TRequest, TResponse>> pipelineBehaviours)
|
||||
where TRequest : notnull
|
||||
{
|
||||
private readonly IEnumerable<IPipelineBehaviour<TRequest, TResponse>> pipelineBehaviours =
|
||||
pipelineBehaviours.Reverse();
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
HandlerDelegate<TRequest, TResponse> currentHandler = handler.Handle;
|
||||
foreach (IPipelineBehaviour<TRequest, TResponse> behaviour in pipelineBehaviours)
|
||||
{
|
||||
HandlerDelegate<TRequest, TResponse> previousHandler = currentHandler;
|
||||
currentHandler = async (args, token) =>
|
||||
{
|
||||
return await behaviour.Handle(args, previousHandler, token);
|
||||
};
|
||||
}
|
||||
|
||||
return await currentHandler(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IActivated
|
||||
{
|
||||
Task OnActivated();
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IActivation;
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IAsyncHandler<TMessage, TResponse> :
|
||||
IHandler
|
||||
{
|
||||
Task<TResponse> Handle(TMessage args, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public interface IAsyncHandler<TMessage> :
|
||||
IHandler
|
||||
{
|
||||
Task Handle(TMessage args, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IDeactivated
|
||||
{
|
||||
Task OnDeactivated();
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IDeactivating
|
||||
{
|
||||
Task OnDeactivating();
|
||||
}
|
||||
@@ -2,12 +2,14 @@
|
||||
|
||||
public interface IHandler;
|
||||
|
||||
public interface IHandler<in TRequest, TResponse> :
|
||||
public interface IHandler<TMessage> :
|
||||
IHandler
|
||||
{
|
||||
Task<TResponse> Handle(TRequest args,
|
||||
CancellationToken cancellationToken);
|
||||
void Handle(TMessage args);
|
||||
}
|
||||
|
||||
public interface IHandler<in TRequest> :
|
||||
IHandler<TRequest, Unit>;
|
||||
public interface IHandler<TMessage, TResponse> :
|
||||
IHandler
|
||||
{
|
||||
TResponse Handle(TMessage args);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IHandlerProvider
|
||||
{
|
||||
IEnumerable<object?> Get(object key);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
@@ -163,7 +164,7 @@ public static class IHostBuilderExtension
|
||||
new ConfigurationInitializer<TConfiguration>(provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(section),
|
||||
provider.GetRequiredKeyedService<IConfigurationWriter<TConfiguration>>(section),
|
||||
provider.GetRequiredKeyedService<IConfigurationFactory<TConfiguration>>(section),
|
||||
provider.GetRequiredService<IPublisher>()));
|
||||
provider.GetRequiredService<IMessenger>()));
|
||||
|
||||
services.AddTransient<IConfigurationInitializer<TConfiguration>, ConfigurationInitializer<TConfiguration>>(provider =>
|
||||
provider.GetRequiredService<IServiceFactory>().Create<ConfigurationInitializer<TConfiguration>>(section));
|
||||
@@ -188,7 +189,7 @@ public static class IHostBuilderExtension
|
||||
provider.GetRequiredKeyedService<IConfigurationCache>(section),
|
||||
provider.GetRequiredKeyedService<IConfigurationFile<TConfiguration>>(section),
|
||||
provider.GetRequiredService<IServiceProvider>(),
|
||||
provider.GetRequiredService<IPublisher>()));
|
||||
provider.GetRequiredService<IMessenger>()));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IMediator
|
||||
{
|
||||
Task<object?> Handle(Type responseType,
|
||||
object message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task<TResponse?> Handle<TMessage, TResponse>(TMessage message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull;
|
||||
|
||||
IAsyncEnumerable<object?> HandleAsyncMany(Type responseType,
|
||||
object message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
IAsyncEnumerable<TResponse?> HandleAsyncMany<TMessage, TResponse>(TMessage message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull;
|
||||
|
||||
Task<IList<TResponse?>> HandleMany<TMessage, TResponse>(TMessage message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IMediatorRequired
|
||||
{
|
||||
IMediator Mediator { get; }
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IMessage;
|
||||
@@ -0,0 +1,19 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public static class IMessengerExtensions
|
||||
{
|
||||
public static TResponse Send<TMessage, TResponse>(this IMessenger messenger)
|
||||
where TMessage : class, new()
|
||||
{
|
||||
ResponseEventArgs<TMessage, TResponse> args = messenger.Send(new ResponseEventArgs<TMessage, TResponse> { Message = new TMessage() });
|
||||
return args.Response;
|
||||
}
|
||||
|
||||
public static void Send<TMessage>(this IMessenger messenger, string key)
|
||||
where TMessage : class, new() => messenger.Send(new TMessage(), key);
|
||||
|
||||
public static async Task<TResponse> SendAsync<TMessage, TResponse>(this IMessenger messenger)
|
||||
where TMessage : class, new() => await messenger.Send(new AsyncResponseEventArgs<TMessage, TResponse> { Message = new TMessage() });
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IMessengerRequired
|
||||
{
|
||||
IMessenger Messenger { get; }
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface INotificationHandler<in TMessage> :
|
||||
IHandler
|
||||
{
|
||||
Task Handle(TMessage args);
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
namespace Toolkit.Foundation;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IObservableViewModel :
|
||||
IDisposable
|
||||
{
|
||||
public IDisposer Disposer { get; }
|
||||
|
||||
public IPublisher Publisher { get; }
|
||||
public IMessenger Messenger { get; }
|
||||
|
||||
public IServiceFactory Factory { get; }
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IPipelineBehaviour<TMessage, TResponse>
|
||||
{
|
||||
Task<TResponse> Handle(TMessage message,
|
||||
HandlerDelegate<TMessage, TResponse> next,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public interface IPipelineBehaviour<TMessage>
|
||||
{
|
||||
Task Handle(TMessage message,
|
||||
NotificationHandlerDelegate<TMessage> next,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IPublisher
|
||||
{
|
||||
void Publish<TMessage>(object? key = null)
|
||||
where TMessage : new();
|
||||
|
||||
void Publish<TMessage>(TMessage message)
|
||||
where TMessage : notnull;
|
||||
|
||||
void Publish<TMessage>(TMessage message,
|
||||
object? key = null)
|
||||
where TMessage : notnull;
|
||||
|
||||
void Publish(object message,
|
||||
Func<Func<Task>, Task> marshal,
|
||||
object? key = null);
|
||||
|
||||
void Publish<TMessage>()
|
||||
where TMessage : new();
|
||||
|
||||
void Publish(object message);
|
||||
|
||||
void PublishUI<TMessage>(TMessage message,
|
||||
object? key = null) where TMessage : notnull;
|
||||
|
||||
void PublishUI<TMessage>(object? key = null)
|
||||
where TMessage : new();
|
||||
|
||||
void PublishUI<TMessage>(TMessage message)
|
||||
where TMessage : notnull;
|
||||
|
||||
void PublishUI(object message);
|
||||
|
||||
void PublishUI<TMessage>()
|
||||
where TMessage : new();
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IPublisherRequired
|
||||
{
|
||||
IPublisher Publisher { get; }
|
||||
}
|
||||
@@ -38,61 +38,59 @@ public static class IServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services,
|
||||
string key)
|
||||
where THandler : IHandler
|
||||
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
where THandler : class, IHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
return AddHandler<THandler>(services, ServiceLifetime.Transient, key);
|
||||
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
|
||||
services.AddInitialization<HandlerInitialization<TMessage, TResponse, THandler>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services,
|
||||
public static IServiceCollection AddAsyncHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
|
||||
services.AddInitialization<AsyncHandlerInitialization<TMessage, TResponse, THandler>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAsyncHandler<TMessage, THandler>(this IServiceCollection services,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
where THandler : class, IAsyncHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
|
||||
services.AddInitialization<AsyncHandlerInitialization<TMessage, THandler>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHandler<TMessage, THandler>(this IServiceCollection services,
|
||||
string key)
|
||||
where THandler : class, IHandler<TMessage>
|
||||
where TMessage : class => AddHandler<TMessage, THandler>(services, ServiceLifetime.Transient, key);
|
||||
|
||||
public static IServiceCollection AddHandler<TMessage, THandler>(this IServiceCollection services,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient,
|
||||
string? key = null)
|
||||
where THandler : IHandler
|
||||
where THandler : class, IHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
if (typeof(THandler).GetInterfaces() is Type[] handlerTypes)
|
||||
if (key is { Length: > 0})
|
||||
{
|
||||
foreach (Type handlerType in handlerTypes)
|
||||
{
|
||||
if (handlerType.Name == typeof(INotificationHandler<>).Name &&
|
||||
handlerType.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
|
||||
{
|
||||
Type notificationType = notificationHandlerArguments[0];
|
||||
Type wrapperType = typeof(NotificationHandlerWrapper<>).MakeGenericType(notificationType);
|
||||
|
||||
string preferredKey = $"{(key is not null ? $"{key}:" : "")}{notificationType}";
|
||||
|
||||
services.Add(new ServiceDescriptor(typeof(INotificationHandler<>)
|
||||
.MakeGenericType(notificationType), preferredKey, typeof(THandler), lifetime));
|
||||
|
||||
services.Add(new ServiceDescriptor(wrapperType, preferredKey, (provider, registeredKey) =>
|
||||
provider.GetService<IServiceFactory>()?.Create(wrapperType,
|
||||
provider.GetRequiredKeyedService(typeof(INotificationHandler<>).MakeGenericType(notificationType), registeredKey),
|
||||
provider.GetServices(typeof(IPipelineBehaviour<>)
|
||||
.MakeGenericType(notificationType)))!, lifetime));
|
||||
}
|
||||
|
||||
if (handlerType.Name == typeof(IHandler<,>).Name &&
|
||||
handlerType.GetGenericArguments() is { Length: 2 } handlerArguments)
|
||||
{
|
||||
Type requestType = handlerArguments[0];
|
||||
Type responseType = handlerArguments[1];
|
||||
|
||||
Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType);
|
||||
string preferredKey = $"{(key is not null ? $"{key}:" : "")}{wrapperType}";
|
||||
|
||||
services.Add(new ServiceDescriptor(typeof(THandler), preferredKey,
|
||||
typeof(THandler), lifetime));
|
||||
|
||||
services.Add(new ServiceDescriptor(wrapperType, preferredKey, (provider, actualKey) =>
|
||||
provider.GetService<IServiceFactory>()?.Create(wrapperType,
|
||||
provider.GetRequiredKeyedService<THandler>(preferredKey),
|
||||
provider.GetServices(typeof(IPipelineBehaviour<,>)
|
||||
.MakeGenericType(requestType, responseType)))!, lifetime));
|
||||
}
|
||||
}
|
||||
|
||||
return services;
|
||||
services.Add(new ServiceDescriptor(typeof(THandler), key, typeof(THandler), lifetime));
|
||||
services.AddInitialization<HandlerInitialization<TMessage, THandler>>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
|
||||
services.AddInitialization<HandlerInitialization<TMessage, THandler>>();
|
||||
}
|
||||
|
||||
return services;
|
||||
@@ -106,6 +104,15 @@ public static class IServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddInitialization<TInitialization>(this IServiceCollection services,
|
||||
params object[] parameters)
|
||||
where TInitialization : class,
|
||||
IInitialization
|
||||
{
|
||||
services.AddTransient<IInitialization>(provider => provider.GetRequiredService<IServiceFactory>().Create<TInitialization>(parameters));
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddRange(this IServiceCollection services,
|
||||
IServiceCollection fromServices)
|
||||
{
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface ISubscriber
|
||||
{
|
||||
void Subscribe(object subscriber);
|
||||
|
||||
void Unsubscribe(object subscriber);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface ISubscriberRequired
|
||||
{
|
||||
ISubscriber Subscription { get; }
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class Mediator(IHandlerProvider handlerProvider,
|
||||
IServiceProvider provider) :
|
||||
IMediator
|
||||
{
|
||||
public async Task<TResponse?> Handle<TMessage, TResponse>(TMessage message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull
|
||||
{
|
||||
Type messageType = message.GetType();
|
||||
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(messageType, typeof(TResponse));
|
||||
key = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
|
||||
|
||||
List<object?> handlers = GetHandlers(message, handlerType, key);
|
||||
foreach (object? handler in handlers)
|
||||
{
|
||||
MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [message.GetType(), typeof(CancellationToken)]);
|
||||
if (handleMethod is not null)
|
||||
{
|
||||
return await (Task<TResponse?>)handleMethod.Invoke(handler, [message, cancellationToken])!;
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<object?> Handle(Type responseType,
|
||||
object message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
Type messageType = message.GetType();
|
||||
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType);
|
||||
key = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
|
||||
|
||||
List<object?> handlers = GetHandlers(message, handlerType, key);
|
||||
foreach (object? handler in handlers)
|
||||
{
|
||||
MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle",
|
||||
[messageType, typeof(CancellationToken)]);
|
||||
|
||||
if (handleMethod is not null)
|
||||
{
|
||||
dynamic task = handleMethod.Invoke(handler, [message, cancellationToken])!;
|
||||
await task;
|
||||
|
||||
return task.Result;
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<object?> HandleAsyncMany(Type responseType,
|
||||
object message,
|
||||
object? key = null,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
{
|
||||
Type messageType = message.GetType();
|
||||
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType);
|
||||
key = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
|
||||
|
||||
List<object?> handlers = GetHandlers(message, handlerType, key);
|
||||
foreach (object? handler in handlers)
|
||||
{
|
||||
MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle",
|
||||
[messageType, typeof(CancellationToken)]);
|
||||
|
||||
if (handleMethod is not null)
|
||||
{
|
||||
yield return await (Task<object?>)handleMethod.Invoke(handler, [message, cancellationToken])!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<TResponse?> HandleAsyncMany<TMessage, TResponse>(TMessage message,
|
||||
object? key = null,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull
|
||||
{
|
||||
Type messageType = message.GetType();
|
||||
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(messageType, typeof(TResponse));
|
||||
key = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
|
||||
|
||||
List<object?> handlers = GetHandlers(message, handlerType, key);
|
||||
foreach (object? handler in handlers)
|
||||
{
|
||||
MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [message.GetType(), typeof(CancellationToken)]);
|
||||
if (handleMethod is not null)
|
||||
{
|
||||
yield return await (Task<TResponse?>)handleMethod.Invoke(handler, [message, cancellationToken])!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<object?>> HandleMany(Type responseType,
|
||||
object message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
List<object?> responses = [];
|
||||
await foreach (object? response in HandleAsyncMany(responseType, message, key, cancellationToken))
|
||||
{
|
||||
responses.Add(response);
|
||||
}
|
||||
|
||||
return responses;
|
||||
}
|
||||
|
||||
public async Task<IList<TResponse?>> HandleMany<TMessage, TResponse>(TMessage message,
|
||||
object? key = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TMessage : notnull
|
||||
{
|
||||
List<TResponse?> responses = [];
|
||||
await foreach (TResponse? response in HandleAsyncMany<TMessage, TResponse>(message, key, cancellationToken))
|
||||
{
|
||||
responses.Add(response);
|
||||
}
|
||||
|
||||
return responses;
|
||||
}
|
||||
private List<object?> GetHandlers(object message,
|
||||
Type handlerWrapperType,
|
||||
object? key)
|
||||
{
|
||||
Type messageType = message.GetType();
|
||||
Dictionary<Type, List<object?>> handlers = [];
|
||||
|
||||
void AddHandlers(IEnumerable<object?> newHandlers)
|
||||
{
|
||||
foreach (object? handler in newHandlers)
|
||||
{
|
||||
if (handler == null) continue;
|
||||
|
||||
Type serviceType = handler.GetType();
|
||||
if (!handlers.TryGetValue(serviceType, out List<object?>? handlerList))
|
||||
{
|
||||
handlerList = [];
|
||||
handlers.Add(serviceType, handlerList);
|
||||
}
|
||||
|
||||
handlerList.Add(handler);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<object?> keyedServices = key is not null ? provider.GetKeyedServices(handlerWrapperType, key) :
|
||||
provider.GetServices(handlerWrapperType);
|
||||
AddHandlers(keyedServices);
|
||||
|
||||
if (key is not null)
|
||||
{
|
||||
IEnumerable<object?> additionalHandlers = handlerProvider.Get(key);
|
||||
AddHandlers(additionalHandlers);
|
||||
}
|
||||
|
||||
return handlers.SelectMany(entry => entry.Value).ToList();
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class NavigateBackHandler(IComponentScopeProvider provider) :
|
||||
INotificationHandler<NavigateBackEventArgs>
|
||||
IHandler<NavigateBackEventArgs>
|
||||
{
|
||||
public Task Handle(NavigateBackEventArgs args)
|
||||
public void Handle(NavigateBackEventArgs args)
|
||||
{
|
||||
if (provider.Get(args.Scope ?? "Root")
|
||||
is ComponentScopeDescriptor descriptor)
|
||||
@@ -16,7 +16,5 @@ public class NavigateBackHandler(IComponentScopeProvider provider) :
|
||||
navigationScope.Back(args.Context);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,9 @@ namespace Toolkit.Foundation;
|
||||
|
||||
public class NavigateHandler(NamedComponent scope,
|
||||
IComponentScopeProvider componentScopeProvider) :
|
||||
INotificationHandler<NavigateEventArgs>
|
||||
IHandler<NavigateEventArgs>
|
||||
{
|
||||
public Task Handle(NavigateEventArgs args)
|
||||
public void Handle(NavigateEventArgs args)
|
||||
{
|
||||
INavigation? navigation = null;
|
||||
if (args.Scope is "self" || args.Scope is "new")
|
||||
@@ -34,7 +34,5 @@ public class NavigateHandler(NamedComponent scope,
|
||||
|
||||
navigation?.Navigate(args.Route, args.Sender,
|
||||
args.Region, args.Navigated, args.Parameters);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class Navigation(IServiceProvider provider,
|
||||
INavigationRegionProvider navigationRegionProvider,
|
||||
IContentFactory contentFactory,
|
||||
IPublisher publisher) :
|
||||
IMessenger messenger) :
|
||||
INavigation
|
||||
{
|
||||
public void Navigate(string route,
|
||||
@@ -71,7 +72,7 @@ public class Navigation(IServiceProvider provider,
|
||||
if (Activator.CreateInstance(navigateEventType, [region, template, content, sender, parameters])
|
||||
is object navigateEvent)
|
||||
{
|
||||
publisher.Publish(navigateEvent, navigationType.Name);
|
||||
messenger.Send(navigateEvent, navigationType.Name);
|
||||
if (currentSegmentIndex == segmentCount)
|
||||
{
|
||||
navigated?.Invoke(this, EventArgs.Empty);
|
||||
@@ -97,7 +98,7 @@ public class Navigation(IServiceProvider provider,
|
||||
Type navigateType = typeof(NavigateBackEventArgs<>).MakeGenericType(navigationType);
|
||||
if (Activator.CreateInstance(navigateType, [region]) is object navigate)
|
||||
{
|
||||
publisher.Publish(navigate, navigationType.Name);
|
||||
messenger.Send(navigate, navigationType.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public delegate Task NotificationHandlerDelegate<TMessage>(TMessage message);
|
||||
@@ -1,23 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class NotificationHandlerWrapper<TMessage>(INotificationHandler<TMessage> handler,
|
||||
IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours)
|
||||
{
|
||||
private readonly IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours =
|
||||
pipelineBehaviours.Reverse();
|
||||
|
||||
public async Task Handle(TMessage message)
|
||||
{
|
||||
NotificationHandlerDelegate<TMessage> currentHandler = handler.Handle;
|
||||
foreach (IPipelineBehaviour<TMessage> behaviour in pipelineBehaviours)
|
||||
{
|
||||
NotificationHandlerDelegate<TMessage> previousHandler = currentHandler;
|
||||
currentHandler = async (args) =>
|
||||
{
|
||||
await behaviour.Handle(args, previousHandler);
|
||||
};
|
||||
}
|
||||
|
||||
await currentHandler(message);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +1,30 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public partial class Observable(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer) :
|
||||
ObservableObject,
|
||||
ObservableRecipient,
|
||||
IObservableViewModel,
|
||||
IActivityIndicator,
|
||||
IInitialization,
|
||||
IActivated,
|
||||
IDeactivating,
|
||||
IDeactivated,
|
||||
IDisposable,
|
||||
IServiceProviderRequired,
|
||||
IServiceFactoryRequired,
|
||||
IMediatorRequired,
|
||||
IPublisherRequired,
|
||||
IMessengerRequired,
|
||||
IDisposerRequired
|
||||
{
|
||||
private readonly Dictionary<string, object> trackedProperties = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isActivated;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isActive;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isInitialized;
|
||||
|
||||
public IDisposer Disposer { get; } = disposer;
|
||||
|
||||
public IServiceFactory Factory { get; } = factory;
|
||||
|
||||
public IMediator Mediator { get; } = mediator;
|
||||
|
||||
public IServiceProvider Provider { get; } = provider;
|
||||
|
||||
public IPublisher Publisher { get; } = publisher;
|
||||
|
||||
public ISubscriber Subscriber { get; } = subscriber;
|
||||
|
||||
public virtual Task OnActivated()
|
||||
{
|
||||
IsActivated = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public IMessenger Messenger { get; } = messenger;
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
@@ -59,37 +34,12 @@ public partial class Observable(IServiceProvider provider,
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task OnDeactivated()
|
||||
{
|
||||
IsActivated = false;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task OnDeactivating() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
Disposer.Dispose(this);
|
||||
}
|
||||
|
||||
public virtual void OnInitialize()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Initialize()
|
||||
{
|
||||
if (IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsInitialized = true;
|
||||
Subscriber.Subscribe(this);
|
||||
OnInitialize();
|
||||
}
|
||||
|
||||
public void Revert()
|
||||
{
|
||||
foreach (object trackedProperty in trackedProperties.Values)
|
||||
@@ -116,11 +66,9 @@ public partial class Observable<TValue> :
|
||||
|
||||
public Observable(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
@@ -143,12 +91,10 @@ public partial class Observable<TKey, TValue> :
|
||||
|
||||
public Observable(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
TKey key,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer)
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
@@ -8,12 +8,8 @@ using System.Reactive.Disposables;
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public partial class ObservableCollection<TViewModel> :
|
||||
ObservableObject,
|
||||
ObservableRecipient,
|
||||
IObservableCollectionViewModel<TViewModel>,
|
||||
IInitialization,
|
||||
IActivated,
|
||||
IDeactivating,
|
||||
IDeactivated,
|
||||
IList<TViewModel>,
|
||||
IList,
|
||||
IReadOnlyList<TViewModel>,
|
||||
@@ -21,17 +17,15 @@ public partial class ObservableCollection<TViewModel> :
|
||||
ICollectionSynchronization<TViewModel>,
|
||||
IServiceProviderRequired,
|
||||
IServiceFactoryRequired,
|
||||
IMediatorRequired,
|
||||
IPublisherRequired,
|
||||
IMessengerRequired,
|
||||
IDisposerRequired,
|
||||
INotificationHandler<RemoveEventArgs<TViewModel>>,
|
||||
INotificationHandler<RemoveAtEventArgs<TViewModel>>,
|
||||
INotificationHandler<CreateEventArgs<TViewModel>>,
|
||||
INotificationHandler<InsertEventArgs<TViewModel>>,
|
||||
INotificationHandler<MoveEventArgs<TViewModel>>,
|
||||
INotificationHandler<MoveToEventArgs<TViewModel>>,
|
||||
INotificationHandler<ReplaceEventArgs<TViewModel>>,
|
||||
INotificationHandler<SelectionEventArgs<TViewModel>>
|
||||
IRecipient<RemoveEventArgs<TViewModel>>,
|
||||
IRecipient<RemoveAtEventArgs<TViewModel>>,
|
||||
IRecipient<CreateEventArgs<TViewModel>>,
|
||||
IRecipient<InsertEventArgs<TViewModel>>,
|
||||
IRecipient<MoveEventArgs<TViewModel>>,
|
||||
IRecipient<MoveToEventArgs<TViewModel>>,
|
||||
IRecipient<ReplaceEventArgs<TViewModel>>
|
||||
where TViewModel : notnull,
|
||||
IDisposable
|
||||
{
|
||||
@@ -39,8 +33,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
|
||||
private readonly IDispatcher dispatcher;
|
||||
|
||||
private readonly Queue<object> pendingEvents = [];
|
||||
|
||||
private readonly Dictionary<string, object> trackedProperties = [];
|
||||
|
||||
[ObservableProperty]
|
||||
@@ -49,28 +41,20 @@ public partial class ObservableCollection<TViewModel> :
|
||||
private Func<TViewModel>? defaultSelectionFactory;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isActivated;
|
||||
private bool initialized;
|
||||
|
||||
private bool isClearing;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isInitialized;
|
||||
|
||||
[ObservableProperty]
|
||||
private TViewModel? selectedItem;
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IDisposer disposer)
|
||||
IMessenger messenger,
|
||||
IDisposer disposer) : base(messenger)
|
||||
{
|
||||
Provider = provider;
|
||||
Factory = factory;
|
||||
Mediator = mediator;
|
||||
Publisher = publisher;
|
||||
Subscriber = subscriber;
|
||||
Disposer = disposer;
|
||||
|
||||
dispatcher = Provider.GetRequiredService<IDispatcher>();
|
||||
@@ -79,17 +63,12 @@ public partial class ObservableCollection<TViewModel> :
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
IEnumerable<TViewModel> items)
|
||||
IEnumerable<TViewModel> items) : base(messenger)
|
||||
{
|
||||
Provider = provider;
|
||||
Factory = factory;
|
||||
Mediator = mediator;
|
||||
Publisher = publisher;
|
||||
Subscriber = subscriber;
|
||||
Disposer = disposer;
|
||||
|
||||
dispatcher = Provider.GetRequiredService<IDispatcher>();
|
||||
@@ -105,21 +84,12 @@ public partial class ObservableCollection<TViewModel> :
|
||||
public IServiceFactory Factory { get; private set; }
|
||||
|
||||
bool IList.IsFixedSize => false;
|
||||
|
||||
bool ICollection<TViewModel>.IsReadOnly => false;
|
||||
|
||||
bool IList.IsReadOnly => false;
|
||||
|
||||
bool ICollection.IsSynchronized => false;
|
||||
|
||||
public IMediator Mediator { get; }
|
||||
|
||||
public new IMessenger Messenger { get; private set; }
|
||||
public IServiceProvider Provider { get; private set; }
|
||||
|
||||
public IPublisher Publisher { get; private set; }
|
||||
|
||||
public ISubscriber Subscriber { get; }
|
||||
|
||||
object ICollection.SyncRoot => this;
|
||||
|
||||
public TViewModel this[int index]
|
||||
@@ -147,29 +117,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
}
|
||||
}
|
||||
|
||||
public void Activate(Func<ActivationBuilder> activateDelegate,
|
||||
bool reset = false)
|
||||
{
|
||||
if (reset)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
ActivationBuilder builder = activateDelegate.Invoke();
|
||||
Publisher.Publish(builder.Value, builder.Key);
|
||||
}
|
||||
|
||||
public void Activate(bool reset = false)
|
||||
{
|
||||
if (reset)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
ActivationBuilder builder = ActivationBuilder();
|
||||
Publisher.PublishUI(builder.Value, builder.Key);
|
||||
}
|
||||
|
||||
public TViewModel Add<T>(params object?[] parameters)
|
||||
where T : TViewModel
|
||||
{
|
||||
@@ -289,129 +236,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
((IEnumerable)collection).GetEnumerator();
|
||||
|
||||
public Task Handle(RemoveEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
foreach (TViewModel item in this.ToList())
|
||||
{
|
||||
if (args.Sender is not null && args.Sender.Equals(item))
|
||||
{
|
||||
Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(CreateEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(InsertEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Insert(args.Index, item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(MoveToEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
Move(args.OldIndex, args.NewIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(MoveEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Move(args.Index, item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(ReplaceEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Replace(args.Index, item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(RemoveAtEventArgs<TViewModel> args)
|
||||
{
|
||||
if (IsActivated)
|
||||
{
|
||||
int index = args.Index;
|
||||
if (index >= 0 && index <= Count - 1)
|
||||
{
|
||||
RemoveAt(index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pendingEvents.Enqueue(args);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task Handle(SelectionEventArgs<TViewModel> args) =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public int IndexOf(TViewModel item) =>
|
||||
collection.IndexOf(item);
|
||||
|
||||
@@ -419,22 +243,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
IsCompatibleObject(value) ?
|
||||
IndexOf((TViewModel)value!) : -1;
|
||||
|
||||
[RelayCommand]
|
||||
public virtual void Initialize()
|
||||
{
|
||||
if (IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsInitialized = true;
|
||||
Subscriber.Subscribe(this);
|
||||
|
||||
OnInitialize();
|
||||
|
||||
Activate();
|
||||
}
|
||||
|
||||
public TViewModel Insert<T>(int index = 0,
|
||||
params object?[] parameters)
|
||||
where T :
|
||||
@@ -519,31 +327,67 @@ public partial class ObservableCollection<TViewModel> :
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual Task OnActivated()
|
||||
{
|
||||
IsActivated = true;
|
||||
while (pendingEvents.Count > 0)
|
||||
{
|
||||
object current = pendingEvents.Dequeue();
|
||||
Handle((dynamic)current);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task OnDeactivated()
|
||||
{
|
||||
IsActivated = false;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task OnDeactivating() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual void OnInitialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Receive(RemoveEventArgs<TViewModel> args)
|
||||
{
|
||||
foreach (TViewModel item in this.ToList())
|
||||
{
|
||||
if (args.Sender is not null && args.Sender.Equals(item))
|
||||
{
|
||||
Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Receive(CreateEventArgs<TViewModel> args)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Receive(InsertEventArgs<TViewModel> args)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Insert(args.Index, item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Receive(MoveToEventArgs<TViewModel> args)
|
||||
{
|
||||
Move(args.OldIndex, args.NewIndex);
|
||||
}
|
||||
|
||||
public void Receive(MoveEventArgs<TViewModel> args)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Move(args.Index, item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Receive(ReplaceEventArgs<TViewModel> args)
|
||||
{
|
||||
if (args.Sender is TViewModel item)
|
||||
{
|
||||
Replace(args.Index, item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Receive(RemoveAtEventArgs<TViewModel> args)
|
||||
{
|
||||
int index = args.Index;
|
||||
if (index >= 0 && index <= Count - 1)
|
||||
{
|
||||
RemoveAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(TViewModel item)
|
||||
{
|
||||
int index = collection.IndexOf(item);
|
||||
@@ -637,7 +481,7 @@ public partial class ObservableCollection<TViewModel> :
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSource(IList<TViewModel> source, Func<TViewModel>? defaultSelectionFactory)
|
||||
public void SetSource(IList<TViewModel> source, Func<TViewModel>? defaultSelectionFactory)
|
||||
{
|
||||
foreach (TViewModel item in source)
|
||||
{
|
||||
@@ -666,9 +510,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual ActivationBuilder ActivationBuilder() =>
|
||||
new(new ActivationEventArgs<TViewModel>());
|
||||
|
||||
protected virtual void ClearItems() =>
|
||||
collection.Clear();
|
||||
|
||||
@@ -692,10 +533,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
collection.Insert(index > Count ? Count : index, item);
|
||||
}
|
||||
|
||||
protected virtual void OnSelectedItemChanged()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void RemoveItem(int index) =>
|
||||
collection.RemoveAt(index);
|
||||
|
||||
@@ -711,18 +548,6 @@ public partial class ObservableCollection<TViewModel> :
|
||||
CollectionChanged?.Invoke(this, args);
|
||||
}
|
||||
|
||||
partial void OnIsActivatedChanged(bool value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
while (pendingEvents.Count > 0)
|
||||
{
|
||||
object current = pendingEvents.Dequeue();
|
||||
Handle((dynamic)current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnSelectedItemChanged(TViewModel? oldValue, TViewModel? newValue)
|
||||
{
|
||||
if (oldValue is ISelectable oldSelection)
|
||||
@@ -735,8 +560,7 @@ public partial class ObservableCollection<TViewModel> :
|
||||
newSelection.IsSelected = true;
|
||||
}
|
||||
|
||||
Publisher.Publish(Selection.As(SelectedItem));
|
||||
OnSelectedItemChanged();
|
||||
Messenger.Send(Selection.As(SelectedItem));
|
||||
}
|
||||
|
||||
private void SourceCollectionChanged(object? sender,
|
||||
@@ -785,6 +609,7 @@ public partial class ObservableCollection<TViewModel> :
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSelection(TViewModel item)
|
||||
{
|
||||
if (item is ISelectable newSelection)
|
||||
@@ -809,32 +634,27 @@ public partial class ObservableCollection<TValue, TViewModel> :
|
||||
[ObservableProperty]
|
||||
private TValue? value;
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IDisposer disposer,
|
||||
IServiceFactory factory,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
IEnumerable<TViewModel> items,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer, items)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer, items)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
protected virtual void OnChanged(TValue? value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
partial void OnValueChanged(TValue? value) => OnChanged(value);
|
||||
@@ -852,12 +672,10 @@ public partial class ObservableCollection<TViewModel, TKey, TValue> :
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
TKey key,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer)
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
@@ -865,13 +683,11 @@ public partial class ObservableCollection<TViewModel, TKey, TValue> :
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
IEnumerable<TViewModel> items,
|
||||
TKey key,
|
||||
TValue? value = default) : base(provider, factory, mediator, publisher, subscriber, disposer, items)
|
||||
TValue? value = default) : base(provider, factory, messenger, disposer, items)
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
@@ -883,20 +699,18 @@ public class ObservableCollection :
|
||||
{
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IDisposer disposer) : base(provider, factory, mediator, publisher, subscriber, disposer)
|
||||
IMessenger messenger,
|
||||
IDisposer disposer) : base(provider, factory, messenger, disposer)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ObservableCollection(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
IMessenger messenger,
|
||||
IDisposer disposer,
|
||||
IEnumerable<IDisposable> items) : base(provider, factory, mediator, publisher, subscriber, disposer, items)
|
||||
IEnumerable<IDisposable> items) : base(provider, factory, messenger, disposer, items)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class Publisher(IHandlerProvider handlerProvider,
|
||||
IServiceFactory serviceFactory,
|
||||
IServiceProvider serviceProvider,
|
||||
IDispatcher dispatcher) :
|
||||
IPublisher
|
||||
{
|
||||
public void Publish<TMessage>(object? key = null)
|
||||
where TMessage : new() =>
|
||||
Publish(serviceFactory.Create<TMessage>() ?? new TMessage(), async args => await args(), key);
|
||||
|
||||
public void Publish<TMessage>(TMessage message)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, async args => await args(), null);
|
||||
|
||||
public void Publish<TMessage>(TMessage message,
|
||||
object? key = null)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, async args => await args(), key);
|
||||
|
||||
public void Publish(object message,
|
||||
Func<Func<Task>, Task> marshal,
|
||||
object? key = null)
|
||||
{
|
||||
Type notificationType = message.GetType();
|
||||
Type handlerType = typeof(NotificationHandlerWrapper<>)
|
||||
.MakeGenericType(notificationType);
|
||||
|
||||
key = $"{(key is not null ? $"{key}:" : "")}{notificationType}";
|
||||
|
||||
List<object?> handlers = [];
|
||||
foreach (object? handler in handlerProvider.Get(key))
|
||||
{
|
||||
handlers.Add(handler);
|
||||
}
|
||||
|
||||
foreach (object? handler in serviceProvider.GetKeyedServices(handlerType, key))
|
||||
{
|
||||
handlers.Add(handler);
|
||||
}
|
||||
|
||||
foreach (object? handler in handlers)
|
||||
{
|
||||
if (handler is not null)
|
||||
{
|
||||
MethodInfo? handleMethod = handler.GetType().GetMethod("Handle",
|
||||
[notificationType]);
|
||||
|
||||
if (handleMethod is not null)
|
||||
{
|
||||
marshal(() => (Task)handleMethod.Invoke(handler, new object[]
|
||||
{ message })!);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Publish(object message) => Publish(message,
|
||||
async args => await args(), null);
|
||||
|
||||
public void Publish<TMessage>()
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), async args => await args(), null);
|
||||
|
||||
public void PublishUI<TMessage>(object? key = null)
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), key);
|
||||
|
||||
public void PublishUI<TMessage>(TMessage message)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, args => dispatcher.Invoke(async () => await args()), null);
|
||||
|
||||
public void PublishUI<TMessage>(TMessage message,
|
||||
object? key = null)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, args => dispatcher.Invoke(async () => await args()), key);
|
||||
|
||||
public void PublishUI<TMessage>()
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), null);
|
||||
|
||||
public void PublishUI(object message) => Publish(message, args =>
|
||||
dispatcher.Invoke(async () => await args()), null);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ResponseEventArgs<TMessage, TResponse> :
|
||||
RequestMessage<TResponse>
|
||||
{
|
||||
public TMessage? Message { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class SelectFilesHandler(IFileProvider fileProvider) :
|
||||
IHandler<SelectionEventArgs<FileFilter>, IReadOnlyCollection<string>?>
|
||||
IAsyncHandler<SelectionEventArgs<FileFilter>, IReadOnlyCollection<string>?>
|
||||
{
|
||||
public async Task<IReadOnlyCollection<string>?> Handle(SelectionEventArgs<FileFilter> args,
|
||||
CancellationToken cancellationToken)
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
|
||||
public class SelectFoldersHandler(IFolderProvider folderProvider) :
|
||||
IHandler<SelectionEventArgs<FolderFilter>, IReadOnlyCollection<string>?>
|
||||
IAsyncHandler<SelectionEventArgs<FolderFilter>, IReadOnlyCollection<string>?>
|
||||
{
|
||||
public async Task<IReadOnlyCollection<string>?> Handle(SelectionEventArgs<FolderFilter> args,
|
||||
CancellationToken cancellationToken)
|
||||
public async Task<IReadOnlyCollection<string>?> Handle(SelectionEventArgs<FolderFilter> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (args.Sender is FolderFilter filter)
|
||||
{
|
||||
|
||||
@@ -1,165 +0,0 @@
|
||||
using System.Reactive.Disposables;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class Subscriber(SubscriptionCollection subscriptions,
|
||||
IDisposer disposer) :
|
||||
ISubscriber
|
||||
{
|
||||
public void Subscribe(object subscriber)
|
||||
{
|
||||
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
|
||||
|
||||
foreach (Type handlerType in GetHandlerInterfaces(subscriber.GetType()))
|
||||
{
|
||||
if (handlerType.Name == typeof(INotificationHandler<>).Name &&
|
||||
handlerType.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
|
||||
{
|
||||
Type notificationType = notificationHandlerArguments[0];
|
||||
AddSubscriptions(subscriber, subscribers, notificationType);
|
||||
}
|
||||
|
||||
if (handlerType.Name == typeof(IHandler<,>).Name &&
|
||||
handlerType.GetGenericArguments() is { Length: 2 } handlerArguments)
|
||||
{
|
||||
Type requestType = handlerArguments[0];
|
||||
Type responseType = handlerArguments[1];
|
||||
Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType);
|
||||
|
||||
AddSubscriptions(subscriber, subscribers, wrapperType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Unsubscribe(object subscriber)
|
||||
{
|
||||
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
|
||||
|
||||
foreach (Type handlerType in GetHandlerInterfaces(subscriber.GetType()))
|
||||
{
|
||||
if (handlerType.Name == typeof(INotificationHandler<>).Name &&
|
||||
handlerType.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
|
||||
{
|
||||
Type notificationType = notificationHandlerArguments[0];
|
||||
RemoveSubscriptions(subscriber, subscribers, notificationType);
|
||||
}
|
||||
|
||||
if (handlerType.Name == typeof(IHandler<,>).Name &&
|
||||
handlerType.GetGenericArguments() is { Length: 2 } handlerArguments)
|
||||
{
|
||||
Type requestType = handlerArguments[0];
|
||||
Type responseType = handlerArguments[1];
|
||||
Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType);
|
||||
|
||||
RemoveSubscriptions(subscriber, subscribers, wrapperType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddOrUpdateSubscription(object subscriber,
|
||||
string preferredKey)
|
||||
{
|
||||
subscriptions.AddOrUpdate(preferredKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||
{
|
||||
collection.Add(new WeakReference(subscriber));
|
||||
return collection;
|
||||
});
|
||||
|
||||
disposer.Add(subscriber, Disposable.Create(() => {
|
||||
|
||||
RemoveSubscription(subscriber, preferredKey);
|
||||
|
||||
}));
|
||||
}
|
||||
|
||||
private void AddSubscriptions(object subscriber,
|
||||
IDictionary<Type, List<object>> subscribers,
|
||||
Type handlerType)
|
||||
{
|
||||
if (subscribers.TryGetValue(handlerType, out List<object>? keys))
|
||||
{
|
||||
foreach (object key in keys)
|
||||
{
|
||||
string preferredKey = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
|
||||
AddOrUpdateSubscription(subscriber, preferredKey);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string preferredKey = $"{handlerType}";
|
||||
AddOrUpdateSubscription(subscriber, preferredKey);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
|
||||
handlerType.GetInterfaces().Where(interfaceType =>
|
||||
{
|
||||
Type? definition = interfaceType.IsGenericType ? interfaceType.GetGenericTypeDefinition() : null;
|
||||
return definition == typeof(INotificationHandler<>) ||
|
||||
definition == typeof(IHandler<>) ||
|
||||
definition == typeof(IHandler<,>);
|
||||
});
|
||||
|
||||
private IDictionary<Type, List<object>> GetSubscriptionKeys(object subscriber)
|
||||
{
|
||||
Dictionary<Type, List<object>> keys = [];
|
||||
foreach (NotificationAttribute attribute in subscriber.GetAttributes<NotificationAttribute>())
|
||||
{
|
||||
if (!keys.TryGetValue(attribute.Type, out List<object>? value))
|
||||
{
|
||||
value = ([]);
|
||||
keys[attribute.Type] = value;
|
||||
}
|
||||
|
||||
if (subscriber.GetPropertyValue(() => attribute.Key) is object key)
|
||||
{
|
||||
value.Add(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
value.Add(attribute.Key);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
private void RemoveSubscription(object subscriber,
|
||||
string key)
|
||||
{
|
||||
if (subscriptions.TryGetValue(key, out List<WeakReference>? subscribers))
|
||||
{
|
||||
for (int i = subscribers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (subscribers[i].Target == subscriber)
|
||||
{
|
||||
subscribers.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (subscribers.Count == 0)
|
||||
{
|
||||
subscriptions.TryRemove(key, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveSubscriptions(object subscriber,
|
||||
IDictionary<Type, List<object>> subscribers,
|
||||
Type handlerType)
|
||||
{
|
||||
if (subscribers.TryGetValue(handlerType, out List<object>? keys))
|
||||
{
|
||||
foreach (object key in keys)
|
||||
{
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
|
||||
RemoveSubscription(subscriber, subscriptionKey);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string subscriptionKey = $"{handlerType}";
|
||||
RemoveSubscription(subscriber, subscriptionKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class SubscriptionCollection :
|
||||
ConcurrentDictionary<object, List<WeakReference>>;
|
||||
@@ -1,9 +1,10 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class WriteClipboardHandler(IClipboardWriter clipboardWriter) :
|
||||
INotificationHandler<WriteEventArgs<Clipboard<object>>>
|
||||
IAsyncHandler<WriteEventArgs<Clipboard<object>>>
|
||||
{
|
||||
public async Task Handle(WriteEventArgs<Clipboard<object>> args)
|
||||
public async Task Handle(WriteEventArgs<Clipboard<object>> args,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (args.Sender is Clipboard<object> clipboard)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user