1
1
using System ;
2
- using System . Threading . Tasks ;
3
- using RabbitMQ . Client . Events ;
2
+ using System . Linq ;
4
3
5
4
namespace RabbitMQ . Client . Impl
6
5
{
@@ -12,7 +11,7 @@ internal struct EventingWrapper<T>
12
11
private string ? _context ;
13
12
private Action < Exception , string > ? _onExceptionAction ;
14
13
15
- public bool IsEmpty => _event is null ;
14
+ public readonly bool IsEmpty => _event is null ;
16
15
17
16
public EventingWrapper ( string context , Action < Exception , string > onExceptionAction )
18
17
{
@@ -42,7 +41,7 @@ public void ClearHandlers()
42
41
43
42
public void Invoke ( object sender , T parameter )
44
43
{
45
- var handlers = _handlers ;
44
+ Delegate [ ] ? handlers = _handlers ;
46
45
if ( handlers is null )
47
46
{
48
47
handlers = _event ? . GetInvocationList ( ) ;
@@ -53,15 +52,16 @@ public void Invoke(object sender, T parameter)
53
52
54
53
_handlers = handlers ;
55
54
}
56
- foreach ( EventHandler < T > action in handlers )
55
+
56
+ foreach ( EventHandler < T > action in handlers . Cast < EventHandler < T > > ( ) )
57
57
{
58
58
try
59
59
{
60
60
action ( sender , parameter ) ;
61
61
}
62
62
catch ( Exception exception )
63
63
{
64
- var onException = _onExceptionAction ;
64
+ Action < Exception , string > ? onException = _onExceptionAction ;
65
65
if ( onException != null )
66
66
{
67
67
onException ( exception , _context ! ) ;
@@ -82,61 +82,4 @@ public void Takeover(in EventingWrapper<T> other)
82
82
_onExceptionAction = other . _onExceptionAction ;
83
83
}
84
84
}
85
-
86
- internal struct AsyncEventingWrapper < T >
87
- {
88
- private event AsyncEventHandler < T > ? _event ;
89
- private Delegate [ ] ? _handlers ;
90
-
91
- public bool IsEmpty => _event is null ;
92
-
93
- public void AddHandler ( AsyncEventHandler < T > ? handler )
94
- {
95
- _event += handler ;
96
- _handlers = null ;
97
- }
98
-
99
- public void RemoveHandler ( AsyncEventHandler < T > ? handler )
100
- {
101
- _event -= handler ;
102
- _handlers = null ;
103
- }
104
-
105
- // Do not make this function async! (This type is a struct that gets copied at the start of an async method => empty _handlers is copied)
106
- public Task InvokeAsync ( object sender , T parameter )
107
- {
108
- var handlers = _handlers ;
109
- if ( handlers is null )
110
- {
111
- handlers = _event ? . GetInvocationList ( ) ;
112
- if ( handlers is null )
113
- {
114
- return Task . CompletedTask ;
115
- }
116
-
117
- _handlers = handlers ;
118
- }
119
-
120
- if ( handlers . Length == 1 )
121
- {
122
- return ( ( AsyncEventHandler < T > ) handlers [ 0 ] ) ( sender , parameter ) ;
123
- }
124
- return InternalInvoke ( handlers , sender , parameter ) ;
125
- }
126
-
127
- private static async Task InternalInvoke ( Delegate [ ] handlers , object sender , T parameter )
128
- {
129
- foreach ( AsyncEventHandler < T > action in handlers )
130
- {
131
- await action ( sender , parameter )
132
- . ConfigureAwait ( false ) ;
133
- }
134
- }
135
-
136
- public void Takeover ( in AsyncEventingWrapper < T > other )
137
- {
138
- _event = other . _event ;
139
- _handlers = other . _handlers ;
140
- }
141
- }
142
85
}
0 commit comments