Fixed more edge cases
This commit is contained in:
@@ -32,9 +32,8 @@ public class ComponentBuilder :
|
||||
services.AddScoped<SubscriptionCollection>();
|
||||
|
||||
services.AddTransient<IHandlerProvider, HandlerProvider>();
|
||||
services.AddTransient<ISubscription, Subscription>();
|
||||
services.AddScoped<ISubscription, Subscription>();
|
||||
|
||||
services.AddTransient<ISubscription, Subscription>();
|
||||
services.AddTransient<IPublisher, Publisher>();
|
||||
|
||||
services.AddTransient<IMediator, Mediator>();
|
||||
|
||||
@@ -7,6 +7,7 @@ public class HandlerProvider(SubscriptionCollection subscriptions) :
|
||||
object? key = null)
|
||||
{
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
|
||||
var d = subscriptions;
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||
{
|
||||
foreach (WeakReference weakRef in subscribers.ToArray())
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IDisposerRequired
|
||||
{
|
||||
IDisposer Disposer { get; }
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IHandlerProvider
|
||||
{
|
||||
public interface IHandlerProvider
|
||||
{
|
||||
IEnumerable<object?> Get(Type type,
|
||||
object? key = null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IMediatorRequired
|
||||
{
|
||||
IMediator Mediator { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IPublisherRequired
|
||||
{
|
||||
IPublisher Publisher { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IServiceFactoryRequired
|
||||
{
|
||||
IServiceFactory Factory { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IServiceProviderRequired
|
||||
{
|
||||
IServiceProvider Provider { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface ISubscriptionRequired
|
||||
{
|
||||
ISubscription Subscription { get; }
|
||||
}
|
||||
@@ -40,6 +40,7 @@ public class Mediator(IHandlerProvider handlerProvider,
|
||||
handlerList = [];
|
||||
handlers.Add(handlerType, handlerList);
|
||||
}
|
||||
|
||||
handlerList.Add(handler);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +49,7 @@ public class Mediator(IHandlerProvider handlerProvider,
|
||||
{
|
||||
foreach (object? handler in handlerEntry.Value)
|
||||
{
|
||||
if (handler?.GetType().GetMethod("Handle") is MethodInfo handleMethod)
|
||||
if (handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]) is MethodInfo handleMethod)
|
||||
{
|
||||
return await (Task<TResponse?>)handleMethod.Invoke(handler,
|
||||
new object[] { message, cancellationToken })!;
|
||||
|
||||
@@ -3,16 +3,15 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class NavigateHandler(NamedComponent scope,
|
||||
IComponentScopeProvider componentScopeProvider,
|
||||
IServiceProvider provider) :
|
||||
IComponentScopeProvider componentScopeProvider) :
|
||||
INotificationHandler<NavigateEventArgs>
|
||||
{
|
||||
public Task Handle(NavigateEventArgs args)
|
||||
{
|
||||
INavigationScope? navigationScope;
|
||||
if (args.Scope == "self")
|
||||
if (args.Scope == "self" && args.Sender is IServiceProviderRequired requireServiceProvider)
|
||||
{
|
||||
navigationScope = provider.GetRequiredService<INavigationScope>();
|
||||
navigationScope = requireServiceProvider.Provider.GetRequiredService<INavigationScope>();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -10,8 +10,11 @@ public class NavigationScope(IPublisher publisher,
|
||||
IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) :
|
||||
INavigationScope
|
||||
{
|
||||
public void Navigate(string route, object? sender = null, object? context = null,
|
||||
EventHandler? navigated = null, object[]? parameters = null)
|
||||
public void Navigate(string route,
|
||||
object? sender = null,
|
||||
object? context = null,
|
||||
EventHandler? navigated = null,
|
||||
object[]? parameters = null)
|
||||
{
|
||||
string[] segments = route.Split('/');
|
||||
int segmentCount = segments.Length;
|
||||
@@ -40,10 +43,6 @@ public class NavigationScope(IPublisher publisher,
|
||||
.. mappedParameters ?? Enumerable.Empty<object?>()];
|
||||
|
||||
if (provider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view)
|
||||
{
|
||||
if ((parameters is { Length: > 0 }
|
||||
? factory.Create(descriptor.ContentType, parameters)
|
||||
: provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel)
|
||||
{
|
||||
if (context is not null)
|
||||
{
|
||||
@@ -59,8 +58,11 @@ public class NavigationScope(IPublisher publisher,
|
||||
|
||||
if (context is not null)
|
||||
{
|
||||
if (navigationProvider.Get(context is Type type ? type : context.GetType())
|
||||
is INavigation navigation)
|
||||
if ((parameters is { Length: > 0 }
|
||||
? factory.Create(descriptor.ContentType, parameters)
|
||||
: provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel)
|
||||
{
|
||||
if (navigationProvider.Get(context is Type type ? type : context.GetType()) is INavigation navigation)
|
||||
{
|
||||
Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type);
|
||||
if (Activator.CreateInstance(navigateType, [context, view, viewModel, sender, parameters]) is object navigate)
|
||||
|
||||
@@ -19,6 +19,11 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
IList,
|
||||
IReadOnlyList<TViewModel>,
|
||||
INotifyCollectionChanged,
|
||||
IServiceProviderRequired,
|
||||
IServiceFactoryRequired,
|
||||
IMediatorRequired,
|
||||
IPublisherRequired,
|
||||
IDisposerRequired,
|
||||
INotificationHandler<RemoveEventArgs<TViewModel>>,
|
||||
INotificationHandler<CreateEventArgs<TViewModel>>,
|
||||
INotificationHandler<InsertEventArgs<TViewModel>>,
|
||||
|
||||
@@ -10,7 +10,12 @@ public partial class ObservableViewModel :
|
||||
IDeactivating,
|
||||
IDeactivated,
|
||||
IDeactivatable,
|
||||
IDisposable
|
||||
IDisposable,
|
||||
IServiceProviderRequired,
|
||||
IServiceFactoryRequired,
|
||||
IMediatorRequired,
|
||||
IPublisherRequired,
|
||||
IDisposerRequired
|
||||
{
|
||||
[ObservableProperty]
|
||||
private bool isInitialized;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Validation
|
||||
{
|
||||
public static ValidationEventArgs<TValue> As<TValue>(TValue value) =>
|
||||
new(value);
|
||||
|
||||
public static ValidationEventArgs<TValue> As<TValue>() where TValue : new() =>
|
||||
new(new TValue());
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record ValidationEventArgs<TValue>(TValue Value);
|
||||
Reference in New Issue
Block a user