11using System ;
2- using System . Threading . Tasks ;
3- using RabbitMQ . Client . Events ;
2+ using System . Linq ;
43
54namespace RabbitMQ . Client . Impl
65{
@@ -12,7 +11,7 @@ internal struct EventingWrapper<T>
1211 private string ? _context ;
1312 private Action < Exception , string > ? _onExceptionAction ;
1413
15- public bool IsEmpty => _event is null ;
14+ public readonly bool IsEmpty => _event is null ;
1615
1716 public EventingWrapper ( string context , Action < Exception , string > onExceptionAction )
1817 {
@@ -42,7 +41,7 @@ public void ClearHandlers()
4241
4342 public void Invoke ( object sender , T parameter )
4443 {
45- var handlers = _handlers ;
44+ Delegate [ ] ? handlers = _handlers ;
4645 if ( handlers is null )
4746 {
4847 handlers = _event ? . GetInvocationList ( ) ;
@@ -53,15 +52,16 @@ public void Invoke(object sender, T parameter)
5352
5453 _handlers = handlers ;
5554 }
56- foreach ( EventHandler < T > action in handlers )
55+
56+ foreach ( EventHandler < T > action in handlers . Cast < EventHandler < T > > ( ) )
5757 {
5858 try
5959 {
6060 action ( sender , parameter ) ;
6161 }
6262 catch ( Exception exception )
6363 {
64- var onException = _onExceptionAction ;
64+ Action < Exception , string > ? onException = _onExceptionAction ;
6565 if ( onException != null )
6666 {
6767 onException ( exception , _context ! ) ;
@@ -82,61 +82,4 @@ public void Takeover(in EventingWrapper<T> other)
8282 _onExceptionAction = other . _onExceptionAction ;
8383 }
8484 }
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- }
14285}
0 commit comments