Get disposing working for subscription manager
This commit is contained in:
@@ -43,6 +43,11 @@ public class ContentControlHandler :
|
||||
{
|
||||
await deactivated.OnDeactivated();
|
||||
}
|
||||
|
||||
if (content is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,14 +49,16 @@ public class ContentTemplate :
|
||||
{
|
||||
await deactivated.OnDeactivated();
|
||||
}
|
||||
|
||||
if (content is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
control.Loaded += HandleLoaded;
|
||||
control.Unloaded += HandleUnloaded;
|
||||
|
||||
//viewModelContentBinder?.Register(control);
|
||||
|
||||
return control;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
namespace Toolkit.Foundation;
|
||||
using System.Reactive.Disposables;
|
||||
|
||||
public class SubscriptionManager(SubscriptionCollection subscriptions) :
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class SubscriptionManager(SubscriptionCollection subscriptions,
|
||||
IDisposer disposer) :
|
||||
ISubscriptionManager
|
||||
{
|
||||
public IEnumerable<object?> GetHandlers(Type notificationType, object key)
|
||||
{
|
||||
var d = subscriptions;
|
||||
if (subscriptions.TryGetValue($"{(key is not null ? $"{key}:" : "")}{notificationType}",
|
||||
out List<WeakReference>? subscribers))
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{notificationType}";
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||
{
|
||||
foreach (WeakReference weakRef in subscribers.ToArray())
|
||||
{
|
||||
@@ -32,7 +34,8 @@ public class SubscriptionManager(SubscriptionCollection subscriptions) :
|
||||
{
|
||||
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
||||
{
|
||||
if (subscriptions.TryGetValue($"{(key is not null ? $"{key}:" : "")}{argumentType}", out List<WeakReference>? subscribers))
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||
{
|
||||
for (int i = subscribers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
@@ -54,34 +57,43 @@ public class SubscriptionManager(SubscriptionCollection subscriptions) :
|
||||
{
|
||||
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
||||
{
|
||||
if (key is not null)
|
||||
{
|
||||
subscriptions.AddOrUpdate($"{key}:{argumentType}", _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||
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, argumentType)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
subscriptions.AddOrUpdate($"{argumentType}", _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||
private void RemoveSubscriber(object subscriber, Type argumentType)
|
||||
{
|
||||
collection.Add(new WeakReference(subscriber));
|
||||
return collection;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static object? GetKeyFromHandler(object handler)
|
||||
string subscriptionKey = $"{argumentType}";
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||
{
|
||||
return handler.GetAttribute<NotificationAttribute>()
|
||||
is NotificationAttribute attribute
|
||||
? handler.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key
|
||||
: null;
|
||||
for (int i = subscribers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (subscribers[i].Target == subscriber)
|
||||
{
|
||||
subscribers.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
|
||||
if (subscribers.Count == 0)
|
||||
{
|
||||
subscriptions.TryRemove(subscriptionKey, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private object? GetKeyFromHandler(object handler) =>
|
||||
handler.GetAttribute<NotificationAttribute>() is NotificationAttribute attribute
|
||||
? handler.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key : null;
|
||||
|
||||
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
|
||||
handlerType.GetInterfaces().Where(interfaceType => interfaceType.IsGenericType &&
|
||||
interfaceType.GetGenericTypeDefinition() == typeof(INotificationHandler<>));
|
||||
}
|
||||
Reference in New Issue
Block a user