Skip to content

Commit 238d66e

Browse files
committed
Making the event/subscription model a little easier to use. Incrementing
the version number.
1 parent 1e08d83 commit 238d66e

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

source/ChromeDevTools/ChromeSession.cs

+18-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace MasterDevs.ChromeDevTools
1212
public class ChromeSession : IChromeSession
1313
{
1414
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>>>();
1616
private ICommandFactory _commandFactory;
1717
private IEventFactory _eventFactory;
1818
private ManualResetEvent _openEvent = new ManualResetEvent(false);
@@ -85,14 +85,15 @@ public Task<ICommandResponse> SendAsync<T>(T parameter)
8585
return SendCommand(command);
8686
}
8787

88-
public void Subscribe<T>(EventHandler handler)
88+
public void Subscribe<T>(Action<T> handler) where T : class
8989
{
9090
var handlerType = typeof(T);
91+
var handlerForBag = new Action<object>(obj => handler((T)obj));
9192
_handlers.AddOrUpdate(handlerType.FullName,
92-
(m) => new ConcurrentBag<EventHandler>(new[] { handler }),
93+
(m) => new ConcurrentBag<Action<object>>(new [] { handlerForBag }),
9394
(m, currentBag) =>
9495
{
95-
currentBag.Add(handler);
96+
currentBag.Add(handlerForBag);
9697
return currentBag;
9798
});
9899
}
@@ -110,17 +111,28 @@ private void HandleEvent(IEvent evnt)
110111
return;
111112
}
112113
var handlerKey = type.FullName;
113-
ConcurrentBag<EventHandler> handlers = null;
114+
ConcurrentBag<Action<object>> handlers = null;
114115
if (_handlers.TryGetValue(handlerKey, out handlers))
115116
{
116117
var localHandlers = handlers.ToArray();
117118
foreach (var handler in localHandlers)
118119
{
119-
handler(this, evnt);
120+
ExecuteHandler(handler, evnt);
120121
}
121122
}
122123
}
123124

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+
124136
private void HandleResponse(ICommandResponse response)
125137
{
126138
if (null == response) return;
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23

34
namespace MasterDevs.ChromeDevTools
45
{
@@ -8,6 +9,6 @@ public interface IChromeSession
89

910
Task<ICommandResponse> SendAsync<T>();
1011

11-
void Subscribe<T>(EventHandler handler);
12+
void Subscribe<T>(Action<T> handler) where T : class;
1213
}
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following
65
// set of attributes. Change these attribute values to modify the information
76
// associated with an assembly.
8-
[assembly: AssemblyTitle("Chrome Developer Tools for CSharp")]
7+
[assembly: AssemblyTitle("Chrome Developer Tools")]
98
[assembly: AssemblyDescription("Contains the classes and utilities used to interact with the Chrome Developer Tools")]
109
[assembly: AssemblyConfiguration("")]
1110
[assembly: AssemblyCompany("MasterDevs")]
@@ -22,15 +21,5 @@
2221
// The following GUID is for the ID of the typelib if this project is exposed to COM
2322
[assembly: Guid("e7da0b93-c53b-4b4e-a873-88490c1e61cc")]
2423

25-
// Version information for an assembly consists of the following four values:
26-
//
27-
// Major Version
28-
// Minor Version
29-
// Build Number
30-
// Revision
31-
//
32-
// You can specify all the values or you can default the Build and Revision Numbers
33-
// by using the '*' as shown below:
34-
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.*")]
36-
[assembly: AssemblyFileVersion("1.0.0.*")]
24+
[assembly: AssemblyVersion("1.0.1.*")]
25+
[assembly: AssemblyFileVersion("1.0.1")]

source/Sample/Program.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ private static void Main(string[] args)
3333
// but we only subscribe to certain events in this session
3434
var pageEnableResult = chromeSession.SendAsync<ChromeDevTools.Protocol.Page.EnableCommand>().Result;
3535
Console.WriteLine("PageEnable: " + pageEnableResult.Id);
36-
chromeSession.Subscribe<Protocol.Page.DomContentEventFiredEvent>((o, e) =>
36+
chromeSession.Subscribe<Protocol.Page.DomContentEventFiredEvent>(domContentEvent =>
3737
{
38-
var domContentEvent = (Event<DomContentEventFiredEvent>)e;
39-
Console.WriteLine("DomContentEvent: " + domContentEvent.Params.Timestamp);
38+
Console.WriteLine("DomContentEvent: " + domContentEvent.Timestamp);
4039
});
4140
// you might never see this, but that's what an event is ... right?
42-
chromeSession.Subscribe<Protocol.Page.FrameStartedLoadingEvent>((o, e) =>
41+
chromeSession.Subscribe<Protocol.Page.FrameStartedLoadingEvent>(frameStartedLoadingEvent =>
4342
{
44-
var frameStartedLoadingEvent = (Event<FrameStartedLoadingEvent>)e;
45-
Console.WriteLine("FrameStartedLoading: " + frameStartedLoadingEvent.Params.FrameId);
43+
Console.WriteLine("FrameStartedLoading: " + frameStartedLoadingEvent.FrameId);
4644
});
4745

4846
Console.ReadLine();

0 commit comments

Comments
 (0)