2016-08-02 23:54:57 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection;
|
2018-12-19 16:23:44 +03:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
2016-08-10 14:51:33 +03:00
|
|
|
using static System.String;
|
2016-08-02 23:54:57 +03:00
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
{
|
2018-12-19 16:23:44 +03:00
|
|
|
class WeakEventManager
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2018-12-19 16:23:44 +03:00
|
|
|
readonly Dictionary<string, List<Subscription>> _eventHandlers = new Dictionary<string, List<Subscription>>();
|
2016-08-02 23:54:57 +03:00
|
|
|
|
2018-12-19 16:23:44 +03:00
|
|
|
public void AddEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName]string eventName = null)
|
2016-08-02 23:54:57 +03:00
|
|
|
where TEventArgs : EventArgs
|
|
|
|
{
|
2016-08-10 14:51:33 +03:00
|
|
|
if (IsNullOrEmpty(eventName))
|
2016-08-02 23:54:57 +03:00
|
|
|
throw new ArgumentNullException(nameof(eventName));
|
|
|
|
|
|
|
|
if (handler == null)
|
|
|
|
throw new ArgumentNullException(nameof(handler));
|
|
|
|
|
|
|
|
AddEventHandler(eventName, handler.Target, handler.GetMethodInfo());
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:23:44 +03:00
|
|
|
public void AddEventHandler(EventHandler handler, [CallerMemberName]string eventName = null)
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2016-08-10 14:51:33 +03:00
|
|
|
if (IsNullOrEmpty(eventName))
|
2016-08-02 23:54:57 +03:00
|
|
|
throw new ArgumentNullException(nameof(eventName));
|
|
|
|
|
|
|
|
if (handler == null)
|
|
|
|
throw new ArgumentNullException(nameof(handler));
|
|
|
|
|
|
|
|
AddEventHandler(eventName, handler.Target, handler.GetMethodInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleEvent(object sender, object args, string eventName)
|
|
|
|
{
|
2018-12-19 16:23:44 +03:00
|
|
|
var toRaise = new List<(object subscriber, MethodInfo handler)>();
|
2016-08-10 14:51:33 +03:00
|
|
|
var toRemove = new List<Subscription>();
|
2016-08-02 23:54:57 +03:00
|
|
|
|
2018-11-30 07:22:51 +03:00
|
|
|
if (_eventHandlers.TryGetValue(eventName, out List<Subscription> target))
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2018-11-30 07:22:51 +03:00
|
|
|
for (int i = 0; i < target.Count; i++)
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2018-11-30 07:22:51 +03:00
|
|
|
Subscription subscription = target[i];
|
2016-08-10 14:51:33 +03:00
|
|
|
bool isStatic = subscription.Subscriber == null;
|
|
|
|
if (isStatic)
|
|
|
|
{
|
|
|
|
// For a static method, we'll just pass null as the first parameter of MethodInfo.Invoke
|
2018-12-19 16:23:44 +03:00
|
|
|
toRaise.Add((null, subscription.Handler));
|
2016-08-10 14:51:33 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-02 23:54:57 +03:00
|
|
|
object subscriber = subscription.Subscriber.Target;
|
|
|
|
|
|
|
|
if (subscriber == null)
|
|
|
|
// The subscriber was collected, so there's no need to keep this subscription around
|
2016-08-10 14:51:33 +03:00
|
|
|
toRemove.Add(subscription);
|
2016-08-02 23:54:57 +03:00
|
|
|
else
|
2018-12-19 16:23:44 +03:00
|
|
|
toRaise.Add((subscriber, subscription.Handler));
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
2016-08-10 14:51:33 +03:00
|
|
|
|
2018-11-30 07:22:51 +03:00
|
|
|
for (int i = 0; i < toRemove.Count; i++)
|
2016-08-10 14:51:33 +03:00
|
|
|
{
|
2018-11-30 07:22:51 +03:00
|
|
|
Subscription subscription = toRemove[i];
|
2016-08-10 14:51:33 +03:00
|
|
|
target.Remove(subscription);
|
|
|
|
}
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 07:22:51 +03:00
|
|
|
for (int i = 0; i < toRaise.Count; i++)
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2018-12-19 16:23:44 +03:00
|
|
|
(var subscriber, var handler) = toRaise[i];
|
|
|
|
handler.Invoke(subscriber, new[] { sender, args });
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:23:44 +03:00
|
|
|
public void RemoveEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName]string eventName = null)
|
2016-08-02 23:54:57 +03:00
|
|
|
where TEventArgs : EventArgs
|
|
|
|
{
|
2016-08-10 14:51:33 +03:00
|
|
|
if (IsNullOrEmpty(eventName))
|
2016-08-02 23:54:57 +03:00
|
|
|
throw new ArgumentNullException(nameof(eventName));
|
|
|
|
|
|
|
|
if (handler == null)
|
|
|
|
throw new ArgumentNullException(nameof(handler));
|
|
|
|
|
|
|
|
RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:23:44 +03:00
|
|
|
public void RemoveEventHandler(EventHandler handler, [CallerMemberName]string eventName = null)
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2016-08-10 14:51:33 +03:00
|
|
|
if (IsNullOrEmpty(eventName))
|
2016-08-02 23:54:57 +03:00
|
|
|
throw new ArgumentNullException(nameof(eventName));
|
|
|
|
|
|
|
|
if (handler == null)
|
|
|
|
throw new ArgumentNullException(nameof(handler));
|
|
|
|
|
|
|
|
RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddEventHandler(string eventName, object handlerTarget, MethodInfo methodInfo)
|
|
|
|
{
|
2018-12-19 16:23:44 +03:00
|
|
|
if (!_eventHandlers.TryGetValue(eventName, out List<Subscription> targets))
|
2016-08-02 23:54:57 +03:00
|
|
|
{
|
2016-08-10 14:51:33 +03:00
|
|
|
targets = new List<Subscription>();
|
|
|
|
_eventHandlers.Add(eventName, targets);
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
|
|
|
|
2016-08-10 14:51:33 +03:00
|
|
|
if (handlerTarget == null)
|
|
|
|
{
|
|
|
|
// This event handler is a static method
|
|
|
|
targets.Add(new Subscription(null, methodInfo));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
targets.Add(new Subscription(new WeakReference(handlerTarget), methodInfo));
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveEventHandler(string eventName, object handlerTarget, MemberInfo methodInfo)
|
|
|
|
{
|
2018-12-19 16:23:44 +03:00
|
|
|
if (!_eventHandlers.TryGetValue(eventName, out List<Subscription> subscriptions))
|
2016-08-02 23:54:57 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (int n = subscriptions.Count; n > 0; n--)
|
|
|
|
{
|
|
|
|
Subscription current = subscriptions[n - 1];
|
|
|
|
|
2018-12-19 16:23:44 +03:00
|
|
|
if (current.Subscriber?.Target != handlerTarget|| current.Handler.Name != methodInfo.Name)
|
2016-08-02 23:54:57 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
subscriptions.Remove(current);
|
2018-11-29 15:58:42 +03:00
|
|
|
break;
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Subscription
|
|
|
|
{
|
|
|
|
public Subscription(WeakReference subscriber, MethodInfo handler)
|
|
|
|
{
|
|
|
|
Subscriber = subscriber;
|
2018-12-19 16:23:44 +03:00
|
|
|
Handler = handler ?? throw new ArgumentNullException(nameof(handler));
|
2016-08-02 23:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public readonly WeakReference Subscriber;
|
|
|
|
public readonly MethodInfo Handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|