diff --git a/source/ChromeDevTools/ChromeSession.cs b/source/ChromeDevTools/ChromeSession.cs index d6c2901a561d6a853c540a7baeb881dd311fa2d5..087c4d5dae6500a1d7458c6d29285a0ae8ef226f 100644 --- a/source/ChromeDevTools/ChromeSession.cs +++ b/source/ChromeDevTools/ChromeSession.cs @@ -12,7 +12,7 @@ namespace MasterDevs.ChromeDevTools public class ChromeSession : IChromeSession { private readonly string _endpoint; - private readonly ConcurrentDictionary<string, ConcurrentBag<EventHandler>> _handlers = new ConcurrentDictionary<string, ConcurrentBag<EventHandler>>(); + private readonly ConcurrentDictionary<string, ConcurrentBag<Action<object>>> _handlers = new ConcurrentDictionary<string, ConcurrentBag<Action<object>>>(); private ICommandFactory _commandFactory; private IEventFactory _eventFactory; private ManualResetEvent _openEvent = new ManualResetEvent(false); @@ -85,14 +85,15 @@ namespace MasterDevs.ChromeDevTools return SendCommand(command); } - public void Subscribe<T>(EventHandler handler) + public void Subscribe<T>(Action<T> handler) where T : class { var handlerType = typeof(T); + var handlerForBag = new Action<object>(obj => handler((T)obj)); _handlers.AddOrUpdate(handlerType.FullName, - (m) => new ConcurrentBag<EventHandler>(new[] { handler }), + (m) => new ConcurrentBag<Action<object>>(new [] { handlerForBag }), (m, currentBag) => { - currentBag.Add(handler); + currentBag.Add(handlerForBag); return currentBag; }); } @@ -110,17 +111,28 @@ namespace MasterDevs.ChromeDevTools return; } var handlerKey = type.FullName; - ConcurrentBag<EventHandler> handlers = null; + ConcurrentBag<Action<object>> handlers = null; if (_handlers.TryGetValue(handlerKey, out handlers)) { var localHandlers = handlers.ToArray(); foreach (var handler in localHandlers) { - handler(this, evnt); + ExecuteHandler(handler, evnt); } } } + private void ExecuteHandler(Action<object> handler, dynamic evnt) + { + if (evnt.GetType().GetGenericTypeDefinition() == typeof(Event<>)) + { + handler(evnt.Params); + } else + { + handler(evnt); + } + } + private void HandleResponse(ICommandResponse response) { if (null == response) return; diff --git a/source/ChromeDevTools/IChromeSession.cs b/source/ChromeDevTools/IChromeSession.cs index dd7f98979bbf31577df59f9ab908943dd3dc3d06..7840557a68811877ad1ca0b38799043bb36196be 100644 --- a/source/ChromeDevTools/IChromeSession.cs +++ b/source/ChromeDevTools/IChromeSession.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; namespace MasterDevs.ChromeDevTools { @@ -8,6 +9,6 @@ namespace MasterDevs.ChromeDevTools Task<ICommandResponse> SendAsync<T>(); - void Subscribe<T>(EventHandler handler); + void Subscribe<T>(Action<T> handler) where T : class; } } \ No newline at end of file diff --git a/source/ChromeDevTools/Properties/AssemblyInfo.cs b/source/ChromeDevTools/Properties/AssemblyInfo.cs index a84f8a35b269091e8311ec7bf4ad380b3dfd05bd..979e2eafdfb4fed442b16b531c26fc08cbbf6451 100644 --- a/source/ChromeDevTools/Properties/AssemblyInfo.cs +++ b/source/ChromeDevTools/Properties/AssemblyInfo.cs @@ -1,11 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("Chrome Developer Tools for CSharp")] +[assembly: AssemblyTitle("Chrome Developer Tools")] [assembly: AssemblyDescription("Contains the classes and utilities used to interact with the Chrome Developer Tools")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("MasterDevs")] @@ -22,15 +21,5 @@ using System.Runtime.InteropServices; // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("e7da0b93-c53b-4b4e-a873-88490c1e61cc")] -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.*")] -[assembly: AssemblyFileVersion("1.0.0.*")] +[assembly: AssemblyVersion("1.0.1.*")] +[assembly: AssemblyFileVersion("1.0.1")] diff --git a/source/Sample/Program.cs b/source/Sample/Program.cs index c8fefbed4c9a1c8700a1592d7a734ea4778dd6b3..be37866cd8dc7739937ed1e1dab1111dd9b59a46 100644 --- a/source/Sample/Program.cs +++ b/source/Sample/Program.cs @@ -33,16 +33,14 @@ namespace MasterDevs.ChromeDevTools.Sample // but we only subscribe to certain events in this session var pageEnableResult = chromeSession.SendAsync<ChromeDevTools.Protocol.Page.EnableCommand>().Result; Console.WriteLine("PageEnable: " + pageEnableResult.Id); - chromeSession.Subscribe<Protocol.Page.DomContentEventFiredEvent>((o, e) => + chromeSession.Subscribe<Protocol.Page.DomContentEventFiredEvent>(domContentEvent => { - var domContentEvent = (Event<DomContentEventFiredEvent>)e; - Console.WriteLine("DomContentEvent: " + domContentEvent.Params.Timestamp); + Console.WriteLine("DomContentEvent: " + domContentEvent.Timestamp); }); // you might never see this, but that's what an event is ... right? - chromeSession.Subscribe<Protocol.Page.FrameStartedLoadingEvent>((o, e) => + chromeSession.Subscribe<Protocol.Page.FrameStartedLoadingEvent>(frameStartedLoadingEvent => { - var frameStartedLoadingEvent = (Event<FrameStartedLoadingEvent>)e; - Console.WriteLine("FrameStartedLoading: " + frameStartedLoadingEvent.Params.FrameId); + Console.WriteLine("FrameStartedLoading: " + frameStartedLoadingEvent.FrameId); }); Console.ReadLine();