@@ -39,7 +39,7 @@ public sealed class Broker : IAsyncDisposable
3939 private readonly BiDi _bidi ;
4040 private readonly ITransport _transport ;
4141
42- private readonly ConcurrentDictionary < long , ( Command , TaskCompletionSource < EmptyResult > ) > _pendingCommands = new ( ) ;
42+ private readonly ConcurrentDictionary < long , CommandInfo > _pendingCommands = new ( ) ;
4343 private readonly BlockingCollection < MessageEvent > _pendingEvents = [ ] ;
4444 private readonly Dictionary < string , Type > _eventTypesMap = [ ] ;
4545
@@ -206,7 +206,7 @@ private async Task<EmptyResult> ExecuteCommandCoreAsync<TCommand>(TCommand comma
206206
207207 cts . Token . Register ( ( ) => tcs . TrySetCanceled ( cts . Token ) ) ;
208208
209- _pendingCommands [ command . Id ] = ( command , tcs ) ;
209+ _pendingCommands [ command . Id ] = new ( command . Id , command . ResultType , tcs ) ;
210210
211211 var data = JsonSerializer . SerializeToUtf8Bytes ( command , typeof ( TCommand ) , _jsonSerializerContext ) ;
212212
@@ -379,8 +379,8 @@ private void ProcessReceivedMessage(byte[]? data)
379379 if ( id is null ) throw new JsonException ( "The remote end responded with 'success' message type, but missed required 'id' property." ) ;
380380
381381 var successCommand = _pendingCommands [ id . Value ] ;
382- var messageSuccess = JsonSerializer . Deserialize ( ref resultReader , successCommand . Item1 . ResultType , _jsonSerializerContext ) ! ;
383- successCommand . Item2 . SetResult ( ( EmptyResult ) messageSuccess ) ;
382+ var messageSuccess = JsonSerializer . Deserialize ( ref resultReader , successCommand . ResultType , _jsonSerializerContext ) ! ;
383+ successCommand . TaskCompletionSource . SetResult ( ( EmptyResult ) messageSuccess ) ;
384384 _pendingCommands . TryRemove ( id . Value , out _ ) ;
385385 break ;
386386
@@ -400,9 +400,18 @@ private void ProcessReceivedMessage(byte[]? data)
400400
401401 var messageError = new MessageError ( id . Value ) { Error = error , Message = message } ;
402402 var errorCommand = _pendingCommands [ messageError . Id ] ;
403- errorCommand . Item2 . SetException ( new BiDiException ( $ "{ messageError . Error } : { messageError . Message } ") ) ;
403+ errorCommand . TaskCompletionSource . SetException ( new BiDiException ( $ "{ messageError . Error } : { messageError . Message } ") ) ;
404404 _pendingCommands . TryRemove ( messageError . Id , out _ ) ;
405405 break ;
406406 }
407407 }
408+
409+ class CommandInfo ( long id , Type resultType , TaskCompletionSource < EmptyResult > taskCompletionSource )
410+ {
411+ public long Id { get ; } = id ;
412+
413+ public Type ResultType { get ; } = resultType ;
414+
415+ public TaskCompletionSource < EmptyResult > TaskCompletionSource { get ; } = taskCompletionSource ;
416+ } ;
408417}
0 commit comments