| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using NexMediator.Abstractions.Interfaces; |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace NexMediator.Core.Internal; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Executes stream requests using compiled delegates per request/response types. |
| | | 11 | | /// |
| | | 12 | | /// Optimizations: |
| | | 13 | | /// - Compiles expressions to avoid reflection at runtime. |
| | | 14 | | /// - Caches executor delegates by request and response types. |
| | | 15 | | /// - Avoids dynamic casting overhead. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class NexStreamExecutor |
| | | 18 | | { |
| | 1 | 19 | | private static readonly ConcurrentDictionary<(Type requestType, Type responseType), |
| | 1 | 20 | | Func<object, IServiceProvider, CancellationToken, IAsyncEnumerable<object>>> _cache = |
| | 1 | 21 | | new(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Executes a stream request and returns an async sequence of responses. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <typeparam name="TResponse">Type of each response item.</typeparam> |
| | | 27 | | /// <param name="request">The stream request to execute.</param> |
| | | 28 | | /// <param name="provider">Service provider for resolving the handler.</param> |
| | | 29 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 30 | | /// <returns>An async enumerable of <typeparamref name="TResponse"/> items.</returns> |
| | | 31 | | public static IAsyncEnumerable<TResponse> Execute<TResponse>( |
| | | 32 | | INexStreamRequest<TResponse> request, |
| | | 33 | | IServiceProvider provider, |
| | | 34 | | CancellationToken cancellationToken) |
| | 3 | 35 | | { |
| | 3 | 36 | | var requestType = request.GetType(); |
| | 3 | 37 | | var key = (requestType, typeof(TResponse)); |
| | | 38 | | |
| | 3 | 39 | | var executor = _cache.GetOrAdd(key, static tuple => |
| | 2 | 40 | | { |
| | 2 | 41 | | var (reqType, resType) = tuple; |
| | 2 | 42 | | var handlerType = typeof(INexStreamRequestHandler<,>) |
| | 2 | 43 | | .MakeGenericType(reqType, resType); |
| | 2 | 44 | | var handleMethod = handlerType |
| | 2 | 45 | | .GetMethod(nameof(INexStreamRequestHandler<INexStreamRequest<object>, object>.Handle))!; |
| | 3 | 46 | | |
| | 2 | 47 | | var requestParam = Expression.Parameter(typeof(object), "request"); |
| | 2 | 48 | | var providerParam = Expression.Parameter(typeof(IServiceProvider), "provider"); |
| | 2 | 49 | | var ctParam = Expression.Parameter(typeof(CancellationToken), "ct"); |
| | 3 | 50 | | |
| | 2 | 51 | | var castRequest = Expression.Convert(requestParam, reqType); |
| | 2 | 52 | | var getHandler = Expression.Call( |
| | 2 | 53 | | typeof(ServiceProviderServiceExtensions), |
| | 2 | 54 | | nameof(ServiceProviderServiceExtensions.GetRequiredService), |
| | 2 | 55 | | new[] { handlerType }, |
| | 2 | 56 | | providerParam); |
| | 3 | 57 | | |
| | 2 | 58 | | var callHandle = Expression.Call( |
| | 2 | 59 | | getHandler, |
| | 2 | 60 | | handleMethod, |
| | 2 | 61 | | castRequest, |
| | 2 | 62 | | ctParam); |
| | 3 | 63 | | |
| | 2 | 64 | | var convert = Expression.Convert( |
| | 2 | 65 | | callHandle, |
| | 2 | 66 | | typeof(IAsyncEnumerable<object>)); |
| | 3 | 67 | | |
| | 2 | 68 | | var lambda = Expression.Lambda<Func<object, IServiceProvider, CancellationToken, IAsyncEnumerable<object>>>( |
| | 2 | 69 | | convert, |
| | 2 | 70 | | requestParam, |
| | 2 | 71 | | providerParam, |
| | 2 | 72 | | ctParam); |
| | 3 | 73 | | |
| | 2 | 74 | | return lambda.Compile(); |
| | 5 | 75 | | }); |
| | | 76 | | |
| | 3 | 77 | | var result = executor(request!, provider, cancellationToken); |
| | 2 | 78 | | return CastAsync<TResponse>(result, cancellationToken); |
| | 2 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Casts each object item in an async sequence to <typeparamref name="TResponse"/>. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <typeparam name="TResponse">Target item type.</typeparam> |
| | | 85 | | /// <param name="source">Source sequence of objects.</param> |
| | | 86 | | /// <param name="ct">Token to cancel the iteration.</param> |
| | | 87 | | /// <returns>An async enumerable of <typeparamref name="TResponse"/> items.</returns> |
| | | 88 | | private static async IAsyncEnumerable<TResponse> CastAsync<TResponse>( |
| | | 89 | | IAsyncEnumerable<object> source, |
| | | 90 | | [EnumeratorCancellation] CancellationToken ct = default) |
| | 2 | 91 | | { |
| | 11 | 92 | | await foreach (var item in source.WithCancellation(ct).ConfigureAwait(false)) |
| | 3 | 93 | | { |
| | 3 | 94 | | yield return (TResponse)item!; |
| | 3 | 95 | | } |
| | 1 | 96 | | } |
| | | 97 | | } |