Skip to content
Snippets Groups Projects
Commit fd8aa433 authored by svatal's avatar svatal
Browse files

SendAsync returns correct result type

.. so user does not have to use cast on every command result that
returns something interesting
parent 973567ad
No related branches found
No related tags found
No related merge requests found
Showing
with 39 additions and 22 deletions
......@@ -81,10 +81,23 @@ namespace MasterDevs.ChromeDevTools
return SendCommand(command, cancellationToken);
}
public Task<ICommandResponse> SendAsync<T>(T parameter, CancellationToken cancellationToken)
public Task<CommandResponse<T>> SendAsync<T>(ICommand<T> parameter, CancellationToken cancellationToken)
{
var command = _commandFactory.Create(parameter);
return SendCommand(command, cancellationToken);
var task = SendCommand(command, cancellationToken);
return CastTaskResult<ICommandResponse, CommandResponse<T>>(task);
}
private Task<TDerived> CastTaskResult<TBase, TDerived>(Task<TBase> task) where TDerived: TBase
{
var tcs = new TaskCompletionSource<TDerived>();
task.ContinueWith(t => tcs.SetResult((TDerived)t.Result),
TaskContinuationOptions.OnlyOnRanToCompletion);
task.ContinueWith(t => tcs.SetException(t.Exception.InnerExceptions),
TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith(t => tcs.SetCanceled(),
TaskContinuationOptions.OnlyOnCanceled);
return tcs.Task;
}
public void Subscribe<T>(Action<T> handler) where T : class
......
......@@ -5,9 +5,9 @@ namespace MasterDevs.ChromeDevTools
{
public static class ChromeSessionExtensions
{
public static Task<ICommandResponse> SendAsync<T>(this IChromeSession session, T parameter)
public static Task<CommandResponse<T>> SendAsync<T>(this IChromeSession session, ICommand<T> parameter)
{
return session.SendAsync<T>(parameter, CancellationToken.None);
return session.SendAsync(parameter, CancellationToken.None);
}
public static Task<ICommandResponse> SendAsync<T>(this IChromeSession session)
......
......@@ -4,9 +4,13 @@ using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools
{
public interface ICommand<T>
{
}
public interface IChromeSession
{
Task<ICommandResponse> SendAsync<T>(T parameter, CancellationToken cancellationToken);
Task<CommandResponse<TResponse>> SendAsync<TResponse>(ICommand<TResponse> parameter, CancellationToken cancellationToken);
Task<ICommandResponse> SendAsync<T>(CancellationToken cancellationToken);
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Accessibility
/// </summary>
[Command(ProtocolName.Accessibility.GetPartialAXTree)]
[SupportedBy("Chrome")]
public class GetPartialAXTreeCommand
public class GetPartialAXTreeCommand: ICommand<GetPartialAXTreeCommandResponse>
{
/// <summary>
/// Gets or sets ID of node to get the partial accessibility tree for.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.Disable)]
[SupportedBy("Chrome")]
public class DisableCommand
public class DisableCommand: ICommand<DisableCommandResponse>
{
}
}
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.Enable)]
[SupportedBy("Chrome")]
public class EnableCommand
public class EnableCommand: ICommand<EnableCommandResponse>
{
}
}
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.GetCurrentTime)]
[SupportedBy("Chrome")]
public class GetCurrentTimeCommand
public class GetCurrentTimeCommand: ICommand<GetCurrentTimeCommandResponse>
{
/// <summary>
/// Gets or sets Id of animation.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.GetPlaybackRate)]
[SupportedBy("Chrome")]
public class GetPlaybackRateCommand
public class GetPlaybackRateCommand: ICommand<GetPlaybackRateCommandResponse>
{
}
}
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.ReleaseAnimations)]
[SupportedBy("Chrome")]
public class ReleaseAnimationsCommand
public class ReleaseAnimationsCommand: ICommand<ReleaseAnimationsCommandResponse>
{
/// <summary>
/// Gets or sets List of animation ids to seek.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.ResolveAnimation)]
[SupportedBy("Chrome")]
public class ResolveAnimationCommand
public class ResolveAnimationCommand: ICommand<ResolveAnimationCommandResponse>
{
/// <summary>
/// Gets or sets Animation id.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.SeekAnimations)]
[SupportedBy("Chrome")]
public class SeekAnimationsCommand
public class SeekAnimationsCommand: ICommand<SeekAnimationsCommandResponse>
{
/// <summary>
/// Gets or sets List of animation ids to seek.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.SetPaused)]
[SupportedBy("Chrome")]
public class SetPausedCommand
public class SetPausedCommand: ICommand<SetPausedCommandResponse>
{
/// <summary>
/// Gets or sets Animations to set the pause state of.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.SetPlaybackRate)]
[SupportedBy("Chrome")]
public class SetPlaybackRateCommand
public class SetPlaybackRateCommand: ICommand<SetPlaybackRateCommandResponse>
{
/// <summary>
/// Gets or sets Playback rate for animations on page
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Animation
/// </summary>
[Command(ProtocolName.Animation.SetTiming)]
[SupportedBy("Chrome")]
public class SetTimingCommand
public class SetTimingCommand: ICommand<SetTimingCommandResponse>
{
/// <summary>
/// Gets or sets Animation id.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.ApplicationCache
/// </summary>
[Command(ProtocolName.ApplicationCache.Enable)]
[SupportedBy("Chrome")]
public class EnableCommand
public class EnableCommand: ICommand<EnableCommandResponse>
{
}
}
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.ApplicationCache
/// </summary>
[Command(ProtocolName.ApplicationCache.GetApplicationCacheForFrame)]
[SupportedBy("Chrome")]
public class GetApplicationCacheForFrameCommand
public class GetApplicationCacheForFrameCommand: ICommand<GetApplicationCacheForFrameCommandResponse>
{
/// <summary>
/// Gets or sets Identifier of the frame containing document whose application cache is retrieved.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.ApplicationCache
/// </summary>
[Command(ProtocolName.ApplicationCache.GetFramesWithManifests)]
[SupportedBy("Chrome")]
public class GetFramesWithManifestsCommand
public class GetFramesWithManifestsCommand: ICommand<GetFramesWithManifestsCommandResponse>
{
}
}
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.ApplicationCache
/// </summary>
[Command(ProtocolName.ApplicationCache.GetManifestForFrame)]
[SupportedBy("Chrome")]
public class GetManifestForFrameCommand
public class GetManifestForFrameCommand: ICommand<GetManifestForFrameCommandResponse>
{
/// <summary>
/// Gets or sets Identifier of the frame containing document whose manifest is retrieved.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Browser
/// </summary>
[Command(ProtocolName.Browser.GetWindowBounds)]
[SupportedBy("Chrome")]
public class GetWindowBoundsCommand
public class GetWindowBoundsCommand: ICommand<GetWindowBoundsCommandResponse>
{
/// <summary>
/// Gets or sets Browser window id.
......
......@@ -9,7 +9,7 @@ namespace MasterDevs.ChromeDevTools.Protocol.Chrome.Browser
/// </summary>
[Command(ProtocolName.Browser.GetWindowForTarget)]
[SupportedBy("Chrome")]
public class GetWindowForTargetCommand
public class GetWindowForTargetCommand: ICommand<GetWindowForTargetCommandResponse>
{
/// <summary>
/// Gets or sets Devtools agent host id.
......
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