Skip to content

Commit d7f0493

Browse files
committed
Immediate
1 parent 5e84d9d commit d7f0493

File tree

1 file changed

+11
-8
lines changed
  • dotnet/src/webdriver/BiDi/Communication

1 file changed

+11
-8
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public sealed class Broker : IAsyncDisposable
3737
private readonly BiDi _bidi;
3838
private readonly ITransport _transport;
3939

40-
private readonly ConcurrentDictionary<long, CommandInfo> _pendingCommands = new();
40+
private readonly ConcurrentDictionary<long, CommandInfo<EmptyResult>> _pendingCommands = new();
4141
private readonly BlockingCollection<MessageEvent> _pendingEvents = [];
4242
private readonly Dictionary<string, JsonTypeInfo> _eventTypesMap = [];
4343

@@ -143,17 +143,17 @@ public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand comma
143143
where TResult : EmptyResult
144144
{
145145
command.Id = Interlocked.Increment(ref _currentCommandId);
146-
var tcs = new TaskCompletionSource<JsonElement>(TaskCreationOptions.RunContinuationsAsynchronously);
146+
var tcs = new TaskCompletionSource<EmptyResult>(TaskCreationOptions.RunContinuationsAsynchronously);
147147
var timeout = options?.Timeout ?? TimeSpan.FromSeconds(30);
148148
using var cts = new CancellationTokenSource(timeout);
149149
cts.Token.Register(() => tcs.TrySetCanceled(cts.Token));
150-
var commandInfo = new CommandInfo(command.Id, command.ResultType, tcs);
150+
var commandInfo = new CommandInfo<EmptyResult>(command.Id, tcs, jsonResultTypeInfo);
151151
_pendingCommands[command.Id] = commandInfo;
152152
var data = JsonSerializer.SerializeToUtf8Bytes(command, jsonCommandTypeInfo);
153153

154154
await _transport.SendAsync(data, cts.Token).ConfigureAwait(false);
155-
var resultJson = await tcs.Task.ConfigureAwait(false);
156-
return JsonSerializer.Deserialize(resultJson, jsonResultTypeInfo)!;
155+
156+
return (TResult)await tcs.Task.ConfigureAwait(false);
157157
}
158158

159159
public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Action<TEventArgs> action, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo)
@@ -301,7 +301,7 @@ private void ProcessReceivedMessage(byte[]? data)
301301

302302
if (_pendingCommands.TryGetValue(id.Value, out var successCommand))
303303
{
304-
successCommand.TaskCompletionSource.SetResult(JsonElement.ParseValue(ref resultReader));
304+
successCommand.TaskCompletionSource.SetResult((EmptyResult)JsonSerializer.Deserialize(ref resultReader, successCommand.JsonResultTypeInfo)!);
305305
_pendingCommands.TryRemove(id.Value, out _);
306306
}
307307
else
@@ -345,10 +345,13 @@ private void ProcessReceivedMessage(byte[]? data)
345345
}
346346
}
347347

348-
class CommandInfo(long id, Type resultType, TaskCompletionSource<JsonElement> taskCompletionSource)
348+
class CommandInfo<TResult>(long id, TaskCompletionSource<TResult> taskCompletionSource, JsonTypeInfo jsonResultTypeInfo)
349+
where TResult : EmptyResult
349350
{
350351
public long Id { get; } = id;
351352

352-
public TaskCompletionSource<JsonElement> TaskCompletionSource { get; } = taskCompletionSource;
353+
public TaskCompletionSource<TResult> TaskCompletionSource { get; } = taskCompletionSource;
354+
355+
public JsonTypeInfo JsonResultTypeInfo { get; } = jsonResultTypeInfo;
353356
};
354357
}

0 commit comments

Comments
 (0)