Added + operator to HandlerList<T>. Now it works just like events meaning you don't have to initialize it. Note that means you also cannot call 'Register' unless you create an instance first.
You can do 'TileEdit += HandlerList<TileEditEventArgs>.Create(test, priority, true);'. Which 'Create' takes the same parameters as Register. Or if you don't need the parameters you can do 'TileEdit += test;'.
This commit is contained in:
parent
6437d2a977
commit
5816b5badd
2 changed files with 36 additions and 7 deletions
|
|
@ -11,7 +11,7 @@ namespace TShockAPI
|
|||
}
|
||||
public class HandlerList<T> where T : EventArgs
|
||||
{
|
||||
protected class HandlerObject
|
||||
public class HandlerItem
|
||||
{
|
||||
public EventHandler<T> Handler { get; set; }
|
||||
public HandlerPriority Priority { get; set; }
|
||||
|
|
@ -19,10 +19,10 @@ namespace TShockAPI
|
|||
}
|
||||
|
||||
protected object HandlerLock = new object();
|
||||
protected List<HandlerObject> Handlers { get; set; }
|
||||
protected List<HandlerItem> Handlers { get; set; }
|
||||
public HandlerList()
|
||||
{
|
||||
Handlers = new List<HandlerObject>();
|
||||
Handlers = new List<HandlerItem>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -32,10 +32,15 @@ namespace TShockAPI
|
|||
/// <param name="priority">Priority of the delegate</param>
|
||||
/// <param name="gethandled">Should the handler receive a call even if it has been handled</param>
|
||||
public void Register(EventHandler<T> handler, HandlerPriority priority = HandlerPriority.Normal, bool gethandled = false)
|
||||
{
|
||||
Register(Create(handler, priority, gethandled));
|
||||
}
|
||||
|
||||
public void Register(HandlerItem obj)
|
||||
{
|
||||
lock (HandlerLock)
|
||||
{
|
||||
Handlers.Add(new HandlerObject { Handler = handler, Priority = priority, GetHandled = gethandled });
|
||||
Handlers.Add(obj);
|
||||
Handlers = Handlers.OrderBy(h => (int)h.Priority).ToList();
|
||||
}
|
||||
}
|
||||
|
|
@ -50,11 +55,11 @@ namespace TShockAPI
|
|||
|
||||
public void Invoke(object sender, T e)
|
||||
{
|
||||
List<HandlerObject> handlers;
|
||||
List<HandlerItem> handlers;
|
||||
lock (HandlerLock)
|
||||
{
|
||||
//Copy the list for invoking as to not keep it locked during the invokes
|
||||
handlers = new List<HandlerObject>(Handlers);
|
||||
handlers = new List<HandlerItem>(Handlers);
|
||||
}
|
||||
|
||||
var hargs = e as HandledEventArgs;
|
||||
|
|
@ -66,6 +71,27 @@ namespace TShockAPI
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static HandlerItem Create(EventHandler<T> handler, HandlerPriority priority = HandlerPriority.Normal, bool gethandled = false)
|
||||
{
|
||||
return new HandlerItem { Handler = handler, Priority = priority, GetHandled = gethandled };
|
||||
}
|
||||
public static HandlerList<T> operator +(HandlerList<T> hand, HandlerItem obj)
|
||||
{
|
||||
if (hand == null)
|
||||
hand = new HandlerList<T>();
|
||||
|
||||
hand.Register(obj);
|
||||
return hand;
|
||||
}
|
||||
public static HandlerList<T> operator +(HandlerList<T> hand, EventHandler<T> handler)
|
||||
{
|
||||
if (hand == null)
|
||||
hand = new HandlerList<T>();
|
||||
|
||||
hand.Register(Create(handler));
|
||||
return hand;
|
||||
}
|
||||
}
|
||||
|
||||
public enum HandlerPriority
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue