Files
TheXamlGuy.TaskbarGroup/TheXamlGuy.TaskbarGroup.Core/IObservableExtensions.cs
T
dan_clark@outlook.com 2ac0e3ed26 project
2022-03-23 15:44:32 +00:00

32 lines
903 B
C#

using System.Reactive.Linq;
namespace TheXamlGuy.TaskbarGroup.Core
{
public record FileDropped();
public static class IObservableExtensions
{
public static IDisposable WeakSubscribe<TMessage>(this IObservable<TMessage> observable, IEventAggregatorInvoker invoker, Action<TMessage> actionDelegate)
{
var methodInfo = actionDelegate.Method;
var weakReference = new WeakReference(actionDelegate.Target);
IDisposable? subscription = null;
subscription = observable.Subscribe(item =>
{
if (weakReference.Target is object target)
{
invoker.Invoke(target, item, methodInfo);
}
else
{
subscription?.Dispose();
}
});
return subscription;
}
}
}