Fix more edge cases

This commit is contained in:
TheXamlGuy
2024-05-31 22:50:52 +01:00
parent 8f1a3252c6
commit c24538f545
16 changed files with 212 additions and 118 deletions
+1 -5
View File
@@ -1,7 +1,3 @@
namespace Toolkit.Foundation;
public class AggerateAttribute(Type type, object key,
AggerateMode mode = AggerateMode.Reset) : NotificationAttribute(type, key)
{
public AggerateMode Mode => mode;
}
public class AggerateAttribute(Type type, object key) : NotificationAttribute(type, key);
-31
View File
@@ -1,31 +0,0 @@
namespace Toolkit.Foundation;
public record Aggerate
{
public static AggerateEventArgs<TValue, TOptions> With<TValue, TOptions>(TOptions options) where TOptions : class
{
return new AggerateEventArgs<TValue, TOptions>(options);
}
public static AggerateEventArgs<TValue> With<TValue>()
{
return new AggerateEventArgs<TValue>();
}
}
public record AggerateEventArgs<TValue, TOptions>(TOptions? Options = null) :
IAggerate
where TOptions : class
{
public object? Key { get; init; }
public AggerateMode Mode { get; init; }
}
public record AggerateEventArgs<TValue> :
IAggerate
{
public object? Key { get; init; }
public AggerateMode Mode { get; init; }
}
-7
View File
@@ -1,7 +0,0 @@
namespace Toolkit.Foundation;
public enum AggerateMode
{
Append,
Reset
}
+10
View File
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public record Aggregate
{
public static AggregateEventArgs<TValue, TOptions> As<TValue, TOptions>(TOptions options)
where TOptions : class => new(options);
public static AggerateEventArgs<TValue> As<TValue>() =>
new AggerateEventArgs<TValue>();
}
+8
View File
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public record AggregateEventArgs<TValue, TOptions>(TOptions? Options = null) :
IAggregate
where TOptions : class;
public record AggerateEventArgs<TValue> :
IAggregate;
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record AggregateExpression(IAggregate Value, object? Key = null);
+1
View File
@@ -6,6 +6,7 @@ public class HandlerProvider(SubscriptionCollection subscriptions) :
public IEnumerable<object?> Get(Type type,
object? key = null)
{
var d = subscriptions;
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
{
-6
View File
@@ -1,6 +0,0 @@
namespace Toolkit.Foundation;
public interface IAggerate
{
object? Key { get; init; }
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public interface IAggregate;
+4 -4
View File
@@ -2,14 +2,14 @@
public interface IPublisher
{
void Publish<TMessage>(object key)
void Publish<TMessage>(object? key = null)
where TMessage : new();
void Publish<TMessage>(TMessage message)
where TMessage : notnull;
void Publish<TMessage>(TMessage message,
object key)
object? key = null)
where TMessage : notnull;
void Publish(object message,
@@ -22,9 +22,9 @@ public interface IPublisher
void Publish(object message);
void PublishUI<TMessage>(TMessage message,
object key) where TMessage : notnull;
object? key = null) where TMessage : notnull;
void PublishUI<TMessage>(object key)
void PublishUI<TMessage>(object? key = null)
where TMessage : new();
void PublishUI<TMessage>(TMessage message)
+15 -12
View File
@@ -192,20 +192,20 @@ public partial class ObservableCollection<TItem> :
}
}
public void BeginAggregation()
public void Fetch(bool reset = false)
{
if (this.GetAttribute<AggerateAttribute>() is AggerateAttribute attribute)
if (reset)
{
if (attribute.Mode == AggerateMode.Reset)
{
Clear();
}
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
Publisher.PublishUI(OnAggerate(key));
Clear();
}
AggregateExpression expression = CreateAggregateExpression();
Publisher.PublishUI(expression.Value, expression.Key);
}
protected virtual IAggregate OnAggerate() =>
new AggerateEventArgs<TItem>();
public void Clear()
{
clearing = true;
@@ -389,7 +389,7 @@ public partial class ObservableCollection<TItem> :
}
Initialized = true;
BeginAggregation();
Fetch();
return Task.CompletedTask;
}
@@ -565,8 +565,11 @@ public partial class ObservableCollection<TItem> :
collection.Insert(index > Count ? Count : index, item);
}
protected virtual IAggerate OnAggerate(object? key) =>
new AggerateEventArgs<TItem>() with { Key = key };
protected virtual AggregateExpression CreateAggregateExpression() =>
new AggregateExpression(new AggerateEventArgs<TItem>());
protected virtual object? CreateAggregationKey() =>
default;
protected virtual void RemoveItem(int index) =>
collection.RemoveAt(index);
+5 -4
View File
@@ -9,7 +9,7 @@ public class Publisher(IHandlerProvider handlerProvider,
IDispatcher dispatcher) :
IPublisher
{
public void Publish<TMessage>(object key)
public void Publish<TMessage>(object? key = null)
where TMessage : new() =>
Publish(serviceFactory.Create<TMessage>() ?? new TMessage(), async args => await args(), key);
@@ -17,7 +17,8 @@ public class Publisher(IHandlerProvider handlerProvider,
where TMessage : notnull =>
Publish(message, async args => await args(), null);
public void Publish<TMessage>(TMessage message, object key)
public void Publish<TMessage>(TMessage message,
object? key = null)
where TMessage : notnull =>
Publish(message, async args => await args(), key);
@@ -60,7 +61,7 @@ public class Publisher(IHandlerProvider handlerProvider,
where TMessage : new() =>
Publish(new TMessage(), async args => await args(), null);
public void PublishUI<TMessage>(object key)
public void PublishUI<TMessage>(object? key = null)
where TMessage : new() =>
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), key);
@@ -69,7 +70,7 @@ public class Publisher(IHandlerProvider handlerProvider,
Publish(message, args => dispatcher.Invoke(async () => await args()), null);
public void PublishUI<TMessage>(TMessage message,
object key)
object? key = null)
where TMessage : notnull =>
Publish(message, args => dispatcher.Invoke(async () => await args()), key);
+62 -27
View File
@@ -10,21 +10,37 @@ public class Subscription(SubscriptionCollection subscriptions,
{
Type handlerType = subscriber.GetType();
IDictionary<Type, object> keys = GetKeysFromHandler(subscriber);
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
{
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
{
keys.TryGetValue(argumentType, out object? key);
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
subscribers.TryGetValue(argumentType, out List<object>? keys);
if (keys is not null)
{
collection.Add(new WeakReference(subscriber));
return collection;
});
foreach (object key in keys)
{
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
{
collection.Add(new WeakReference(subscriber));
return collection;
});
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
}
}
else
{
string subscriptionKey = $"{argumentType}";
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
{
collection.Add(new WeakReference(subscriber));
return collection;
});
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
}
}
}
}
@@ -32,21 +48,40 @@ public class Subscription(SubscriptionCollection subscriptions,
public void Remove(object subscriber)
{
Type handlerType = subscriber.GetType();
IDictionary<Type, object> keys = GetKeysFromHandler(subscriber);
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
{
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
{
keys.TryGetValue(argumentType, out object? key);
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
subscribers.TryGetValue(argumentType, out List<object>? keys);
if (keys is not null)
{
for (int i = subscribers.Count - 1; i >= 0; i--)
foreach (object key in keys)
{
if (!subscribers[i].IsAlive || subscribers[i].Target == subscriber)
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
{
subscribers.RemoveAt(i);
for (int i = existing.Count - 1; i >= 0; i--)
{
if (!existing[i].IsAlive || existing[i].Target == subscriber)
{
existing.RemoveAt(i);
}
}
}
}
}
else
{
string subscriptionKey = $"{argumentType}";
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
{
for (int i = existing.Count - 1; i >= 0; i--)
{
if (!existing[i].IsAlive || existing[i].Target == subscriber)
{
existing.RemoveAt(i);
}
}
}
}
@@ -74,23 +109,23 @@ public class Subscription(SubscriptionCollection subscriptions,
}
}
//private object? GetKeyFromHandler(object handler) =>
// handler.GetAttribute<NotificationAttribute>() is NotificationAttribute attribute
// ? handler.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key : null;
private IDictionary<Type, object> GetKeysFromHandler(object handler)
private IDictionary<Type, List<object>> GetSubscriptionKeys(object subscriber)
{
Dictionary<Type, object> keys = [];
foreach (NotificationAttribute attribute in handler.GetAttributes<NotificationAttribute>())
Dictionary<Type, List<object>> keys = [];
foreach (NotificationAttribute attribute in subscriber.GetAttributes<NotificationAttribute>())
{
keys.Add(attribute.Type, attribute.Key);
if (!keys.TryGetValue(attribute.Type, out List<object>? value))
{
value = ([]);
keys[attribute.Type] = value;
}
value.Add(attribute.Key);
}
return keys;
}
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
handlerType.GetInterfaces().Where(interfaceType =>
{