Skip to content
Snippets Groups Projects
Commit 238d66ed authored by brewdente's avatar brewdente
Browse files

Making the event/subscription model a little easier to use. Incrementing

the version number.
parent 1e08d83b
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
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
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")]
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment