Improve Publisher

This commit is contained in:
TheXamlGuy
2024-05-14 19:24:07 +01:00
parent adb5ffe57a
commit 4ba58a9863
22 changed files with 93 additions and 140 deletions
@@ -8,8 +8,7 @@ namespace Toolkit.Avalonia;
public class ClassicDesktopStyleApplicationHandler :
INavigateHandler<IClassicDesktopStyleApplicationLifetime>
{
public Task Handle(NavigateEventArgs<IClassicDesktopStyleApplicationLifetime> args,
CancellationToken cancellationToken = default)
public Task Handle(NavigateEventArgs<IClassicDesktopStyleApplicationLifetime> args)
{
if (Application.Current?.ApplicationLifetime is
IClassicDesktopStyleApplicationLifetime lifeTime)
+1 -2
View File
@@ -7,8 +7,7 @@ namespace Toolkit.Avalonia;
public class ContentControlHandler :
INavigateHandler<ContentControl>
{
public async Task Handle(NavigateEventArgs<ContentControl> args,
CancellationToken cancellationToken)
public async Task Handle(NavigateEventArgs<ContentControl> args)
{
if (args.Context is ContentControl contentControl)
{
+2 -3
View File
@@ -6,8 +6,7 @@ namespace Toolkit.Avalonia;
public class ContentDialogHandler(IDispatcher dispatcher) :
INavigateHandler<ContentDialog>
{
public async Task Handle(NavigateEventArgs<ContentDialog> args,
CancellationToken cancellationToken)
public async Task Handle(NavigateEventArgs<ContentDialog> args)
{
if (args.Context is ContentDialog contentDialog)
{
@@ -86,7 +85,7 @@ public class ContentDialogHandler(IDispatcher dispatcher) :
}
// A hack to wait for the dialog to finish loading up to make it appear more responsive
await Task.Delay(250, cancellationToken);
await Task.Delay(250);
if (content is IInitializer initializer)
{
await initializer.Initialize();
+2 -4
View File
@@ -12,8 +12,7 @@ public class FrameHandler :
INavigateHandler<Frame>,
INavigateBackHandler<Frame>
{
public Task Handle(NavigateEventArgs<Frame> args,
CancellationToken cancellationToken)
public Task Handle(NavigateEventArgs<Frame> args)
{
if (args.Context is Frame frame)
{
@@ -136,8 +135,7 @@ public class FrameHandler :
return Task.CompletedTask;
}
public Task Handle(NavigateBackEventArgs<Frame> args,
CancellationToken cancellationToken = default)
public Task Handle(NavigateBackEventArgs<Frame> args)
{
if (args.Context is Frame frame)
{
@@ -8,8 +8,7 @@ namespace Toolkit.Avalonia;
public class SingleViewApplicationHandler :
INavigateHandler<ISingleViewApplicationLifetime>
{
public Task Handle(NavigateEventArgs<ISingleViewApplicationLifetime> args,
CancellationToken cancellationToken = default)
public Task Handle(NavigateEventArgs<ISingleViewApplicationLifetime> args)
{
if (Application.Current?.ApplicationLifetime is
ISingleViewApplicationLifetime lifeTime)
+1 -1
View File
@@ -13,7 +13,7 @@ public class AppService(IEnumerable<IInitializer> initializers,
await initializer.Initialize();
}
await publisher.Publish<StartedEventArgs>(cancellationToken);
publisher.Publish<StartedEventArgs>(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
@@ -20,8 +20,7 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, THe
{
}
public Task Handle(ChangedEventArgs<TConfiguration> args,
CancellationToken cancellationToken = default)
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
throw new NotImplementedException();
}
@@ -58,8 +57,7 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAc
return base.OnActivated();
}
public Task Handle(ChangedEventArgs<TConfiguration> args,
CancellationToken cancellationToken = default)
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
if (args.Value is TConfiguration configuration)
{
@@ -101,8 +99,7 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TDe
return base.OnActivated();
}
public Task Handle(ChangedEventArgs<TConfiguration> args,
CancellationToken cancellationToken = default)
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
if (args.Value is TConfiguration configuration)
{
@@ -5,8 +5,7 @@ public class ConfigurationChangedHandler<TConfiguration, TValue>(ConfigurationVa
where TValue :
class, new()
{
public Task Handle(ChangedEventArgs<TConfiguration> args,
CancellationToken cancellationToken = default)
public Task Handle(ChangedEventArgs<TConfiguration> args)
{
if (args.Value is TConfiguration configuration)
{
@@ -9,7 +9,7 @@ public class ConfigurationInitializer<TConfiguration>(IConfigurationReader<TConf
where TConfiguration :
class
{
public async Task Initialize()
public Task Initialize()
{
if (!reader.TryRead(out TConfiguration? configuration))
{
@@ -20,6 +20,7 @@ public class ConfigurationInitializer<TConfiguration>(IConfigurationReader<TConf
}
}
await publisher.PublishUI(new ActivatedEventArgs<TConfiguration>(configuration));
publisher.PublishUI(new ActivatedEventArgs<TConfiguration>(configuration));
return Task.CompletedTask;
}
}
+2 -2
View File
@@ -11,12 +11,12 @@ public class ConfigurationMonitor<TConfiguration>(IConfigurationFile<TConfigurat
public Task StartAsync(CancellationToken cancellationToken)
{
async void ChangedHandler(object sender,
void ChangedHandler(object sender,
FileSystemEventArgs args)
{
if (reader.Read() is { } configuration)
{
await publisher.PublishUI(new ChangedEventArgs<TConfiguration>(configuration));
publisher.PublishUI(new ChangedEventArgs<TConfiguration>(configuration));
}
}
+3 -3
View File
@@ -2,8 +2,8 @@
public interface INavigationScope
{
Task NavigateAsync(string route, object? sender = null, object? context = null,
EventHandler? navigated = null, object[]? parameters = null, CancellationToken cancellationToken = default);
void Navigate(string route, object? sender = null, object? context = null,
EventHandler? navigated = null, object[]? parameters = null);
Task NavigateBackAsync(object? context, CancellationToken cancellationToken = default);
void Back(object? context);
}
+1 -2
View File
@@ -3,6 +3,5 @@
public interface INotificationHandler<in TMessage> :
IHandler
{
Task Handle(TMessage args,
CancellationToken cancellationToken = default);
Task Handle(TMessage args);
}
+14 -22
View File
@@ -2,45 +2,37 @@
public interface IPublisher
{
public Task Publish<TMessage>(object key,
CancellationToken cancellationToken = default)
void Publish<TMessage>(object key)
where TMessage : new();
public Task Publish<TMessage>(TMessage message,
CancellationToken cancellationToken = default)
void Publish<TMessage>(TMessage message)
where TMessage : notnull;
public Task Publish<TMessage>(TMessage message,
object key,
CancellationToken cancellationToken = default)
void Publish<TMessage>(TMessage message,
object key)
where TMessage : notnull;
Task PublishUI<TMessage>(TMessage message,
object key,
CancellationToken cancellationToken = default)
void PublishUI<TMessage>(TMessage message,
object key)
where TMessage : notnull;
Task PublishUI<TMessage>(object key,
CancellationToken cancellationToken = default)
void PublishUI<TMessage>(object key)
where TMessage : new();
Task PublishUI<TMessage>(TMessage message,
CancellationToken cancellationToken = default)
void PublishUI<TMessage>(TMessage message)
where TMessage : notnull;
Task PublishUI(object message,
CancellationToken cancellationToken = default);
void PublishUI(object message);
Task Publish(object message,
void Publish(object message,
Func<Func<Task>, Task> marshal,
object? key = null,
CancellationToken cancellationToken = default);
object? key = null);
Task PublishUI<TMessage>(CancellationToken cancellationToken = default)
void PublishUI<TMessage>()
where TMessage : new();
Task Publish<TMessage>(CancellationToken cancellationToken = default)
void Publish<TMessage>()
where TMessage : new();
public Task Publish(object message, CancellationToken cancellationToken = default);
void Publish(object message);
}
+4 -3
View File
@@ -5,16 +5,17 @@ namespace Toolkit.Foundation;
public class NavigateBackHandler(IComponentScopeProvider provider) :
INotificationHandler<NavigateBackEventArgs>
{
public async Task Handle(NavigateBackEventArgs args,
CancellationToken cancellationToken)
public Task Handle(NavigateBackEventArgs args)
{
if (provider.Get(args.Scope ?? "Root")
is ComponentScopeDescriptor descriptor)
{
if (descriptor?.Services?.GetService<INavigationScope>() is INavigationScope navigationScope)
{
await navigationScope.NavigateBackAsync(args.Context, cancellationToken);
navigationScope.Back(args.Context);
}
}
return Task.CompletedTask;
}
}
+5 -7
View File
@@ -7,8 +7,7 @@ public class NavigateHandler(NamedComponent scope,
IServiceProvider provider) :
INotificationHandler<NavigateEventArgs>
{
public async Task Handle(NavigateEventArgs args,
CancellationToken cancellationToken)
public Task Handle(NavigateEventArgs args)
{
INavigationScope? navigationScope;
if (args.Scope == "self")
@@ -21,10 +20,9 @@ public class NavigateHandler(NamedComponent scope,
navigationScope = descriptor?.Services?.GetRequiredService<INavigationScope>();
}
if (navigationScope is not null)
{
await navigationScope.NavigateAsync(args.Route, args.Sender,
args.Context, args.Navigated, args.Parameters, cancellationToken);
}
navigationScope?.Navigate(args.Route, args.Sender,
args.Context, args.Navigated, args.Parameters);
return Task.CompletedTask;
}
}
+5 -6
View File
@@ -10,8 +10,8 @@ public class NavigationScope(IPublisher publisher,
IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) :
INavigationScope
{
public async Task NavigateAsync(string route, object? sender = null, object? context = null,
EventHandler? navigated = null, object[]? parameters = null, CancellationToken cancellationToken = default)
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;
@@ -65,7 +65,7 @@ public class NavigationScope(IPublisher publisher,
Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type);
if (Activator.CreateInstance(navigateType, [context, view, viewModel, sender, parameters]) is object navigate)
{
await publisher.Publish(navigate, cancellationToken);
publisher.Publish(navigate);
if (currentSegmentIndex == segmentCount)
{
navigated?.Invoke(this, EventArgs.Empty);
@@ -79,8 +79,7 @@ public class NavigationScope(IPublisher publisher,
}
}
public async Task NavigateBackAsync(object? context,
CancellationToken cancellationToken = default)
public void Back(object? context)
{
if (context is not null)
{
@@ -95,7 +94,7 @@ public class NavigationScope(IPublisher publisher,
Type navigateType = typeof(NavigateBackEventArgs<>).MakeGenericType(navigation.Type);
if (Activator.CreateInstance(navigateType, [context]) is object navigate)
{
await publisher.Publish(navigate, cancellationToken);
publisher.Publish(navigate);
}
}
}
@@ -1,4 +1,3 @@
namespace Toolkit.Foundation;
public delegate Task NotificationHandlerDelegate<TMessage>(TMessage message,
CancellationToken cancellationToken);
public delegate Task NotificationHandlerDelegate<TMessage>(TMessage message);
@@ -6,19 +6,18 @@ public class NotificationHandlerWrapper<TMessage>(INotificationHandler<TMessage>
private readonly IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours =
pipelineBehaviours.Reverse();
public async Task Handle(TMessage message,
CancellationToken cancellationToken)
public async Task Handle(TMessage message)
{
NotificationHandlerDelegate<TMessage> currentHandler = handler.Handle;
foreach (IPipelineBehaviour<TMessage> behaviour in pipelineBehaviours)
{
NotificationHandlerDelegate<TMessage> previousHandler = currentHandler;
currentHandler = async (args, token) =>
currentHandler = async (args) =>
{
await behaviour.Handle(args, previousHandler, token);
await behaviour.Handle(args, previousHandler);
};
}
await currentHandler(message, cancellationToken);
await currentHandler(message);
}
}
@@ -235,7 +235,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
Disposer.Dispose(this);
}
public async Task Enumerate()
public void Enumerate()
{
if (this.GetAttribute<EnumerateAttribute>() is EnumerateAttribute attribute)
{
@@ -245,7 +245,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
}
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
await Publisher.PublishUI(PrepareEnumeration(key));
Publisher.PublishUI(PrepareEnumeration(key));
}
}
@@ -255,8 +255,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
IEnumerator IEnumerable.GetEnumerator() =>
((IEnumerable)collection).GetEnumerator();
public Task Handle(RemoveEventArgs<TViewModel> args,
CancellationToken cancellationToken)
public Task Handle(RemoveEventArgs<TViewModel> args)
{
foreach (TViewModel item in this.ToList())
{
@@ -269,8 +268,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
return Task.CompletedTask;
}
public Task Handle(CreateEventArgs<TViewModel> args,
CancellationToken cancellationToken)
public Task Handle(CreateEventArgs<TViewModel> args)
{
if (args.Value is TViewModel item)
{
@@ -280,8 +278,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
return Task.CompletedTask;
}
public Task Handle(InsertEventArgs<TViewModel> args,
CancellationToken cancellationToken)
public Task Handle(InsertEventArgs<TViewModel> args)
{
if (args.Value is TViewModel item)
{
@@ -291,8 +288,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
return Task.CompletedTask;
}
public Task Handle(MoveEventArgs<TViewModel> args,
CancellationToken cancellationToken)
public Task Handle(MoveEventArgs<TViewModel> args)
{
if (args.Value is TViewModel item)
{
@@ -302,8 +298,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
return Task.CompletedTask;
}
public Task Handle(ReplaceEventArgs<TViewModel> args,
CancellationToken cancellationToken)
public Task Handle(ReplaceEventArgs<TViewModel> args)
{
if (args.Value is TViewModel item)
{
@@ -328,7 +323,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
}
Initialized = true;
await Enumerate();
Enumerate();
}
public void Insert(int index, TViewModel item) =>
+26 -46
View File
@@ -8,29 +8,21 @@ public class Publisher(ISubscriptionManager subscriptionManager,
IDispatcher dispatcher) :
IPublisher
{
public Task Publish<TMessage>(object key,
CancellationToken cancellationToken = default)
public void Publish<TMessage>(object key)
where TMessage : new() =>
Publish(new TMessage(), async args => await args(),
key, cancellationToken);
Publish(new TMessage(), async args => await args(), key);
public Task Publish<TMessage>(TMessage message,
CancellationToken cancellationToken = default)
public void Publish<TMessage>(TMessage message)
where TMessage : notnull =>
Publish(message, async args => await args(),
null, cancellationToken);
Publish(message, async args => await args(), null);
public Task Publish<TMessage>(TMessage message,
object key,
CancellationToken cancellationToken = default)
public void Publish<TMessage>(TMessage message, object key)
where TMessage : notnull =>
Publish(message, async args => await args(),
key, cancellationToken);
Publish(message, async args => await args(), key);
public async Task Publish(object message,
public void Publish(object message,
Func<Func<Task>, Task> marshal,
object? key = null,
CancellationToken cancellationToken = default)
object? key = null)
{
Type notificationType = message.GetType();
@@ -49,53 +41,41 @@ public class Publisher(ISubscriptionManager subscriptionManager,
{
Type? handlerType = handler.GetType();
MethodInfo? handleMethod = handlerType.GetMethod("Handle",
[notificationType, typeof(CancellationToken)]);
[notificationType]);
if (handleMethod is not null)
{
await marshal(() => (Task)handleMethod.Invoke(handler, new object[]
{ message, cancellationToken })!);
marshal(() => (Task)handleMethod.Invoke(handler, new object[]
{ message })!);
}
}
}
}
public Task Publish(object message,
CancellationToken cancellationToken = default) => Publish(message,
async args => await args(),
null, cancellationToken);
public void Publish(object message) => Publish(message,
async args => await args(), null);
public Task Publish<TMessage>(CancellationToken cancellationToken = default)
public void Publish<TMessage>()
where TMessage : new() =>
Publish(new TMessage(), async args => await args(),
null, cancellationToken);
Publish(new TMessage(), async args => await args(), null);
public Task PublishUI<TMessage>(object key,
CancellationToken cancellationToken = default)
public void PublishUI<TMessage>(object key)
where TMessage : new() =>
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()),
key, cancellationToken);
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), key);
public Task PublishUI<TMessage>(TMessage message,
CancellationToken cancellationToken = default)
public void PublishUI<TMessage>(TMessage message)
where TMessage : notnull =>
Publish(message, args => dispatcher.Invoke(async () => await args()),
null, cancellationToken);
Publish(message, args => dispatcher.Invoke(async () => await args()), null);
public Task PublishUI<TMessage>(TMessage message,
object key,
CancellationToken cancellationToken = default)
public void PublishUI<TMessage>(TMessage message,
object key)
where TMessage : notnull =>
Publish(message, args => dispatcher.Invoke(async () => await args()),
key, cancellationToken);
Publish(message, args => dispatcher.Invoke(async () => await args()), key);
public Task PublishUI<TMessage>(CancellationToken cancellationToken = default)
public void PublishUI<TMessage>()
where TMessage : new() =>
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()),
null, cancellationToken);
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), null);
public Task PublishUI(object message,
CancellationToken cancellationToken = default) => Publish(message, args =>
dispatcher.Invoke(async () => await args()),
null, cancellationToken);
public void PublishUI(object message) => Publish(message, args =>
dispatcher.Invoke(async () => await args()), null);
}
+1 -1
View File
@@ -75,7 +75,7 @@ public class NavigateAction :
Enumerable.Empty<KeyValuePair<string, object>>()];
observableViewModel.Publisher.Publish(new NavigateEventArgs(Route, Context == this ? control : Context, Scope ?? null,
control.DataContext, Navigated, parameters)).ConfigureAwait(false);
control.DataContext, Navigated, parameters));
}
}
+1 -1
View File
@@ -35,7 +35,7 @@ public class NavigateBackAction :
if (control.DataContext is IObservableViewModel observableViewModel)
{
observableViewModel.Publisher.Publish(new NavigateBackEventArgs(Context
?? null, Scope ?? null)).ConfigureAwait(false);
?? null, Scope ?? null));
}
}