10
10
using Microsoft . Extensions . Logging ;
11
11
using System . Collections . Generic ;
12
12
using System . Linq ;
13
+ using OmniSharp . Extensions . JsonRpc . Client ;
13
14
14
15
namespace OmniSharp . Extensions . JsonRpc
15
16
{
@@ -30,50 +31,76 @@ public RequestRouterBase(ISerializer serializer, IServiceProvider serviceProvide
30
31
31
32
public IServiceProvider ServiceProvider { get ; }
32
33
33
- public async Task RouteNotification ( TDescriptor descriptor , Notification notification , CancellationToken token )
34
+ public async Task RouteNotification ( IRequestDescriptor < TDescriptor > descriptors , Notification notification , CancellationToken token )
34
35
{
35
36
using var debug = _logger . TimeDebug ( "Routing Notification {Method}" , notification . Method ) ;
36
37
using var _ = _logger . BeginScope ( new [ ] {
37
38
new KeyValuePair < string , string > ( "Method" , notification . Method ) ,
38
39
new KeyValuePair < string , string > ( "Params" , notification . Params ? . ToString ( ) )
39
40
} ) ;
40
- using var scope = _serviceScopeFactory . CreateScope ( ) ;
41
- var context = scope . ServiceProvider . GetRequiredService < IRequestContext > ( ) ;
42
- context . Descriptor = descriptor ;
43
- var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
44
41
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 )
50
45
{
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 )
54
52
{
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 ) ;
58
54
}
59
55
else
60
56
{
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 ) ;
62
71
}
63
-
64
- await HandleNotification ( mediator , descriptor , @params ?? Activator . CreateInstance ( descriptor . Params ) , token ) ;
65
72
}
66
73
}
67
74
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 )
69
76
{
70
77
using var debug = _logger . TimeDebug ( "Routing Request ({Id}) {Method}" , request . Id , request . Method ) ;
71
78
using var _ = _logger . BeginScope ( new [ ] {
72
79
new KeyValuePair < string , string > ( "Id" , request . Id ? . ToString ( ) ) ,
73
80
new KeyValuePair < string , string > ( "Method" , request . Method ) ,
74
81
new KeyValuePair < string , string > ( "Params" , request . Params ? . ToString ( ) )
75
82
} ) ;
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 ( ) ;
77
104
var context = scope . ServiceProvider . GetRequiredService < IRequestContext > ( ) ;
78
105
context . Descriptor = descriptor ;
79
106
var mediator = scope . ServiceProvider . GetRequiredService < IMediator > ( ) ;
@@ -132,12 +159,12 @@ public virtual async Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Re
132
159
133
160
_logger . LogTrace ( "Response value was {Type}" , responseValue ? . GetType ( ) . FullName ) ;
134
161
}
135
-
136
162
return new JsonRpc . Client . OutgoingResponse ( request . Id , responseValue , request ) ;
163
+ }
137
164
}
138
165
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 ) ;
141
168
142
169
private static readonly MethodInfo SendRequestUnit = typeof ( RequestRouterBase < TDescriptor > )
143
170
. GetMethods ( BindingFlags . NonPublic | BindingFlags . Static )
0 commit comments