From 238d66edb9c2758f94c04409afb58a9ee8fc7756 Mon Sep 17 00:00:00 2001
From: brewdente <brewdente@gmail.com>
Date: Tue, 16 Jun 2015 23:32:04 -0400
Subject: [PATCH] Making the event/subscription model a little easier to use. 
 Incrementing the version number.

---
 source/ChromeDevTools/ChromeSession.cs        | 24 ++++++++++++++-----
 source/ChromeDevTools/IChromeSession.cs       |  5 ++--
 .../ChromeDevTools/Properties/AssemblyInfo.cs | 17 +++----------
 source/Sample/Program.cs                      | 10 ++++----
 4 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/source/ChromeDevTools/ChromeSession.cs b/source/ChromeDevTools/ChromeSession.cs
index d6c2901..087c4d5 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 dd7f989..7840557 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 a84f8a3..979e2ea 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 c8fefbe..be37866 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();
-- 
GitLab