@@ -12,7 +12,7 @@ namespace MasterDevs.ChromeDevTools
12
12
public class ChromeSession : IChromeSession
13
13
{
14
14
private readonly string _endpoint ;
15
- private readonly ConcurrentDictionary < string , ConcurrentBag < EventHandler > > _handlers = new ConcurrentDictionary < string , ConcurrentBag < EventHandler > > ( ) ;
15
+ private readonly ConcurrentDictionary < string , ConcurrentBag < Action < object > > > _handlers = new ConcurrentDictionary < string , ConcurrentBag < Action < object > > > ( ) ;
16
16
private ICommandFactory _commandFactory ;
17
17
private IEventFactory _eventFactory ;
18
18
private ManualResetEvent _openEvent = new ManualResetEvent ( false ) ;
@@ -85,14 +85,15 @@ public Task<ICommandResponse> SendAsync<T>(T parameter)
85
85
return SendCommand ( command ) ;
86
86
}
87
87
88
- public void Subscribe < T > ( EventHandler handler )
88
+ public void Subscribe < T > ( Action < T > handler ) where T : class
89
89
{
90
90
var handlerType = typeof ( T ) ;
91
+ var handlerForBag = new Action < object > ( obj => handler ( ( T ) obj ) ) ;
91
92
_handlers . AddOrUpdate ( handlerType . FullName ,
92
- ( m ) => new ConcurrentBag < EventHandler > ( new [ ] { handler } ) ,
93
+ ( m ) => new ConcurrentBag < Action < object > > ( new [ ] { handlerForBag } ) ,
93
94
( m , currentBag ) =>
94
95
{
95
- currentBag . Add ( handler ) ;
96
+ currentBag . Add ( handlerForBag ) ;
96
97
return currentBag ;
97
98
} ) ;
98
99
}
@@ -110,17 +111,28 @@ private void HandleEvent(IEvent evnt)
110
111
return ;
111
112
}
112
113
var handlerKey = type . FullName ;
113
- ConcurrentBag < EventHandler > handlers = null ;
114
+ ConcurrentBag < Action < object > > handlers = null ;
114
115
if ( _handlers . TryGetValue ( handlerKey , out handlers ) )
115
116
{
116
117
var localHandlers = handlers . ToArray ( ) ;
117
118
foreach ( var handler in localHandlers )
118
119
{
119
- handler ( this , evnt ) ;
120
+ ExecuteHandler ( handler , evnt ) ;
120
121
}
121
122
}
122
123
}
123
124
125
+ private void ExecuteHandler ( Action < object > handler , dynamic evnt )
126
+ {
127
+ if ( evnt . GetType ( ) . GetGenericTypeDefinition ( ) == typeof ( Event < > ) )
128
+ {
129
+ handler ( evnt . Params ) ;
130
+ } else
131
+ {
132
+ handler ( evnt ) ;
133
+ }
134
+ }
135
+
124
136
private void HandleResponse ( ICommandResponse response )
125
137
{
126
138
if ( null == response ) return ;
0 commit comments