-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventBuffer.cs
35 lines (31 loc) · 914 Bytes
/
EventBuffer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
namespace PanzerBlitz
{
public class EventBuffer<T> where T : EventArgs
{
readonly Queue<Tuple<Action<object, T>, Tuple<object, T>>> _Invocations =
new Queue<Tuple<Action<object, T>, Tuple<object, T>>>();
public void QueueEvent(Action<object, T> Handler, object Sender, T E)
{
lock (_Invocations)
{
_Invocations.Enqueue(
new Tuple<Action<object, T>, Tuple<object, T>>(Handler, new Tuple<object, T>(Sender, E)));
}
}
public Action<object, T> Hook<K>(Action<object, K> Handler) where K : T
{
return (Sender, E) => QueueEvent((s, e) => Handler(s, (K)e), Sender, E);
}
public void DispatchEvents()
{
lock (_Invocations)
{
foreach (var invocation in _Invocations)
invocation.Item1(invocation.Item2.Item1, invocation.Item2.Item2);
_Invocations.Clear();
}
}
}
}