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