1010using Microsoft . Extensions . Logging ;
1111using System . Collections . Generic ;
1212using System . Linq ;
13+ using OmniSharp . Extensions . JsonRpc . Client ;
1314
1415namespace OmniSharp . Extensions . JsonRpc
1516{
@@ -30,50 +31,76 @@ public RequestRouterBase(ISerializer serializer, IServiceProvider serviceProvide
3031
3132 public IServiceProvider ServiceProvider { get ; }
3233
33- public async Task RouteNotification ( TDescriptor descriptor , Notification notification , CancellationToken token )
34+ public async Task RouteNotification ( IRequestDescriptor < TDescriptor > descriptors , Notification notification , CancellationToken token )
3435 {
3536 using var debug = _logger . TimeDebug ( "Routing Notification {Method}" , notification . Method ) ;
3637 using var _ = _logger . BeginScope ( new [ ] {
3738 new KeyValuePair < string , string > ( "Method" , notification . Method ) ,
3839 new KeyValuePair < string , string > ( "Params" , notification . Params ? . ToString ( ) )
3940 } ) ;
40- using var scope = _serviceScopeFactory . CreateScope ( ) ;
41- var context = scope . ServiceProvider . GetRequiredService < IRequestContext > ( ) ;
42- context . Descriptor = descriptor ;
43- var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
4441
45- if ( descriptor . Params is null )
46- {
47- await HandleNotification ( mediator , descriptor , EmptyRequest . Instance , token ) ;
48- }
49- else
42+ await Task . WhenAll ( descriptors . Select ( descriptor => InnerRouteNotification ( descriptor ) ) ) ;
43+
44+ async Task InnerRouteNotification ( TDescriptor descriptor )
5045 {
51- _logger . LogDebug ( "Converting params for Notification {Method} to {Type}" , notification . Method , descriptor . Params . FullName ) ;
52- object @params ;
53- if ( descriptor . IsDelegatingHandler )
46+ using var scope = _serviceScopeFactory . CreateScope ( ) ;
47+ var context = scope . ServiceProvider . GetRequiredService < IRequestContext > ( ) ;
48+ context . Descriptor = descriptor ;
49+ var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
50+
51+ if ( descriptor . Params is null )
5452 {
55- // new DelegatingRequest();
56- var o = notification . Params ? . ToObject ( descriptor . Params . GetGenericArguments ( ) [ 0 ] , _serializer . JsonSerializer ) ;
57- @params = Activator . CreateInstance ( descriptor . Params , new object [ ] { o } ) ;
53+ await HandleNotification ( mediator , descriptor , EmptyRequest . Instance , token ) ;
5854 }
5955 else
6056 {
61- @params = notification . Params ? . ToObject ( descriptor . Params , _serializer . JsonSerializer ) ;
57+ _logger . LogDebug ( "Converting params for Notification {Method} to {Type}" , notification . Method , descriptor . Params . FullName ) ;
58+ object @params ;
59+ if ( descriptor . IsDelegatingHandler )
60+ {
61+ // new DelegatingRequest();
62+ var o = notification . Params ? . ToObject ( descriptor . Params . GetGenericArguments ( ) [ 0 ] , _serializer . JsonSerializer ) ;
63+ @params = Activator . CreateInstance ( descriptor . Params , new object [ ] { o } ) ;
64+ }
65+ else
66+ {
67+ @params = notification . Params ? . ToObject ( descriptor . Params , _serializer . JsonSerializer ) ;
68+ }
69+
70+ await HandleNotification ( mediator , descriptor , @params ?? Activator . CreateInstance ( descriptor . Params ) , token ) ;
6271 }
63-
64- await HandleNotification ( mediator , descriptor , @params ?? Activator . CreateInstance ( descriptor . Params ) , token ) ;
6572 }
6673 }
6774
68- public virtual async Task < ErrorResponse > RouteRequest ( TDescriptor descriptor , Request request , CancellationToken token )
75+ public virtual async Task < ErrorResponse > RouteRequest ( IRequestDescriptor < TDescriptor > descriptors , Request request , CancellationToken token )
6976 {
7077 using var debug = _logger . TimeDebug ( "Routing Request ({Id}) {Method}" , request . Id , request . Method ) ;
7178 using var _ = _logger . BeginScope ( new [ ] {
7279 new KeyValuePair < string , string > ( "Id" , request . Id ? . ToString ( ) ) ,
7380 new KeyValuePair < string , string > ( "Method" , request . Method ) ,
7481 new KeyValuePair < string , string > ( "Params" , request . Params ? . ToString ( ) )
7582 } ) ;
76- using var scope = _serviceScopeFactory . CreateScope ( ) ;
83+
84+ if ( typeof ( IAggregateResults ) . IsAssignableFrom ( descriptors . Default . Response ) )
85+ {
86+ var responses = await Task . WhenAll ( descriptors . Select ( InnerRouteRequest ) ) ;
87+ var errorResponse = responses . FirstOrDefault ( x => x . IsError ) ;
88+ if ( errorResponse . IsError ) return errorResponse ;
89+ if ( responses . Length == 1 )
90+ {
91+ return responses [ 0 ] ;
92+ }
93+
94+ if ( ! ( responses [ 0 ] . Value is OutgoingResponse or) ) throw new NotSupportedException ( "Unsupported response type" ) ;
95+ if ( ! ( or . Result is IAggregateResults ar ) ) throw new NotSupportedException ( "Unsupported result type" ) ;
96+ return new OutgoingResponse ( request . Id , ar . AggregateResults ( responses . Skip ( 1 ) . Select ( z => z . Value ) . OfType < OutgoingResponse > ( ) . Select ( z => z . Result ) ) , request ) ;
97+ }
98+
99+ return await InnerRouteRequest ( descriptors . Default ) ;
100+
101+ async Task < ErrorResponse > InnerRouteRequest ( TDescriptor descriptor )
102+ {
103+ using var scope = _serviceScopeFactory . CreateScope ( ) ;
77104 var context = scope . ServiceProvider . GetRequiredService < IRequestContext > ( ) ;
78105 context . Descriptor = descriptor ;
79106 var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
@@ -132,12 +159,12 @@ public virtual async Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Re
132159
133160 _logger . LogTrace ( "Response value was {Type}" , responseValue ? . GetType ( ) . FullName ) ;
134161 }
135-
136162 return new JsonRpc . Client . OutgoingResponse ( request . Id , responseValue , request ) ;
163+ }
137164 }
138165
139- public abstract TDescriptor GetDescriptor ( Notification notification ) ;
140- public abstract TDescriptor GetDescriptor ( Request request ) ;
166+ public abstract IRequestDescriptor < TDescriptor > GetDescriptor ( Notification notification ) ;
167+ public abstract IRequestDescriptor < TDescriptor > GetDescriptor ( Request request ) ;
141168
142169 private static readonly MethodInfo SendRequestUnit = typeof ( RequestRouterBase < TDescriptor > )
143170 . GetMethods ( BindingFlags . NonPublic | BindingFlags . Static )
0 commit comments