-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsynchronousEvent.cs
More file actions
119 lines (105 loc) · 3.65 KB
/
AsynchronousEvent.cs
File metadata and controls
119 lines (105 loc) · 3.65 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Threading.Tasks;
namespace Ultz.Extensions.Events
{
/// <summary>
/// Represents an asynchronous event handler caller.
/// </summary>
/// <typeparam name="T"> The <see cref="Type" /> of <see cref="EventArgs" /> used by this event. </typeparam>
public sealed class AsynchronousEvent<T> where T : EventArgs
{
private readonly Func<Exception, Task> _errorHandler;
private readonly object _lock = new object();
/// <summary>
/// Initialises a new <see cref="AsynchronousEvent{T}" />.
/// </summary>
public AsynchronousEvent()
{
}
/// <summary>
/// Initialises a new <see cref="AsynchronousEvent{T}" /> with the specified <see cref="Func{T, TResult}" /> error handler.
/// </summary>
/// <param name="errorHandler"> The error handler for exceptions occurring in event handlers. </param>
public AsynchronousEvent(Func<Exception, Task> errorHandler)
{
_errorHandler = errorHandler;
}
private event AsynchronousEventHandler<T> Delegate;
/// <summary>
/// Hooks an <see cref="AsynchronousEventHandler{T}" /> up to this <see cref="AsynchronousEvent{T}" />.
/// </summary>
/// <param name="handler"> The <see cref="AsynchronousEventHandler{T}" /> to hook up. </param>
public void Hook(AsynchronousEventHandler<T> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
lock (_lock)
{
Delegate += handler;
}
}
/// <summary>
/// Unhooks an <see cref="AsynchronousEventHandler{T}" /> from this <see cref="AsynchronousEvent{T}" />.
/// </summary>
/// <param name="handler"> The <see cref="AsynchronousEventHandler{T}" /> to unhook. </param>
public void Unhook(AsynchronousEventHandler<T> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
lock (_lock)
{
Delegate -= handler;
}
}
/// <summary>
/// Unhooks all <see cref="AsynchronousEventHandler{T}" />s from this <see cref="AsynchronousEvent{T}" />.
/// </summary>
public void UnhookAll()
{
lock (_lock)
{
Delegate = null;
}
}
/// <summary>
/// Invokes this <see cref="AsynchronousEventHandler{T}" />, sequentially invoking each hooked up
/// <see cref="AsynchronousEventHandler{T}" />.
/// </summary>
/// <param name="e"> The <see cref="EventArgs" /> data for this invocation. </param>
public async Task InvokeAsync(T e)
{
Delegate[] list;
lock (_lock)
{
list = Delegate?.GetInvocationList();
}
if (list == null)
{
return;
}
for (var i = 0; i < list.Length; i++)
{
var task = ((AsynchronousEventHandler<T>) list[i])(e);
if (task == null)
{
continue;
}
try
{
await task.ConfigureAwait(false);
}
catch (Exception ex)
{
if (_errorHandler != null)
{
await _errorHandler(ex).ConfigureAwait(false);
}
}
}
}
}
}