| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using NexMediator.Abstractions.Interfaces; |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | |
| | | 6 | | namespace NexMediator.Core.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Builds and caches a dispatcher for each notification type. |
| | | 10 | | /// The dispatcher: |
| | | 11 | | /// 1. Gets all notification handlers. |
| | | 12 | | /// 2. Calls each handler.Handle in parallel. |
| | | 13 | | /// 3. Uses Task.WhenAll to wait for all handlers to finish. |
| | | 14 | | /// </summary> |
| | | 15 | | internal static class NexNotificationExecutor |
| | | 16 | | { |
| | 1 | 17 | | private static readonly ConcurrentDictionary<Type, Func<object, IServiceProvider, CancellationToken, Task>> _cache = |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Publishes a notification by invoking all handlers in parallel. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <typeparam name="TNotification">Notification type.</typeparam> |
| | | 23 | | /// <param name="notification">The notification to publish.</param> |
| | | 24 | | /// <param name="provider">Service provider to resolve handlers.</param> |
| | | 25 | | /// <param name="ct">Cancellation token.</param> |
| | | 26 | | /// <returns>A Task that completes when all handlers have run.</returns> |
| | | 27 | | /// <exception cref="ArgumentNullException">Thrown if notification is null.</exception> |
| | | 28 | | public static Task Publish<TNotification>(TNotification notification, IServiceProvider provider, CancellationToken c |
| | | 29 | | where TNotification : INexNotification |
| | 2 | 30 | | { |
| | 2 | 31 | | if (notification == null) |
| | 0 | 32 | | throw new ArgumentNullException(nameof(notification)); |
| | | 33 | | |
| | 2 | 34 | | var type = typeof(TNotification); |
| | 2 | 35 | | var dispatcher = _cache.GetOrAdd(type, CreateDispatcher); |
| | 2 | 36 | | return dispatcher(notification!, provider, ct); |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Creates a compiled dispatcher delegate for a notification type. |
| | | 41 | | /// </summary> |
| | | 42 | | private static Func<object, IServiceProvider, CancellationToken, Task> CreateDispatcher(Type type) |
| | 1 | 43 | | { |
| | 1 | 44 | | var handlerInterface = typeof(INexNotificationHandler<>).MakeGenericType(type); |
| | 1 | 45 | | var method = handlerInterface.GetMethod(nameof(INexNotificationHandler<INexNotification>.Handle))!; |
| | | 46 | | |
| | 1 | 47 | | var notificationParam = Expression.Parameter(typeof(object), "notification"); |
| | 1 | 48 | | var providerParam = Expression.Parameter(typeof(IServiceProvider), "provider"); |
| | 1 | 49 | | var ctParam = Expression.Parameter(typeof(CancellationToken), "ct"); |
| | | 50 | | |
| | 1 | 51 | | var castNotification = Expression.Convert(notificationParam, type); |
| | 1 | 52 | | var getServices = typeof(ServiceProviderServiceExtensions) |
| | 1 | 53 | | .GetMethods() |
| | 4 | 54 | | .First(m => m.Name == nameof(ServiceProviderServiceExtensions.GetServices) |
| | 4 | 55 | | && m.IsGenericMethodDefinition) |
| | 1 | 56 | | .MakeGenericMethod(handlerInterface); |
| | 1 | 57 | | var handlersCall = Expression.Call(getServices, providerParam); |
| | | 58 | | |
| | 1 | 59 | | var handlersVar = Expression.Variable(typeof(IEnumerable<>).MakeGenericType(handlerInterface), "handlers"); |
| | 1 | 60 | | var tasksVar = Expression.Variable(typeof(List<Task>), "tasks"); |
| | 1 | 61 | | var handlerVar = Expression.Variable(handlerInterface, "handler"); |
| | | 62 | | |
| | 1 | 63 | | var newList = Expression.New(typeof(List<Task>)); |
| | 1 | 64 | | var assignList = Expression.Assign(tasksVar, newList); |
| | 1 | 65 | | var assignHandlers = Expression.Assign(handlersVar, handlersCall); |
| | | 66 | | |
| | 1 | 67 | | var callHandle = Expression.Call(handlerVar, method, castNotification, ctParam); |
| | 1 | 68 | | var addCall = Expression.Call(tasksVar, nameof(List<Task>.Add), null, callHandle); |
| | 1 | 69 | | var loop = ForEach(handlerInterface, handlersVar, handlerVar, addCall); |
| | 1 | 70 | | var whenAll = Expression.Call(typeof(Task).GetMethod(nameof(Task.WhenAll), new[] { typeof(IEnumerable<Task>) })! |
| | | 71 | | |
| | 1 | 72 | | var body = Expression.Block( |
| | 1 | 73 | | new[] { handlersVar, tasksVar, handlerVar }, |
| | 1 | 74 | | assignHandlers, |
| | 1 | 75 | | assignList, |
| | 1 | 76 | | loop, |
| | 1 | 77 | | whenAll); |
| | | 78 | | |
| | 1 | 79 | | return Expression.Lambda<Func<object, IServiceProvider, CancellationToken, Task>>( |
| | 1 | 80 | | body, |
| | 1 | 81 | | notificationParam, |
| | 1 | 82 | | providerParam, |
| | 1 | 83 | | ctParam) |
| | 1 | 84 | | .Compile(); |
| | 1 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Helper to build a foreach over a collection expression. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="itemType">Type of items in the collection.</param> |
| | | 91 | | /// <param name="collection">Expression for the collection.</param> |
| | | 92 | | /// <param name="loopVar">Loop variable expression.</param> |
| | | 93 | | /// <param name="body">Loop body expression.</param> |
| | | 94 | | /// <returns>An expression representing the foreach loop.</returns> |
| | | 95 | | private static Expression ForEach(Type itemType, Expression collection, ParameterExpression loopVar, Expression body |
| | 1 | 96 | | { |
| | 1 | 97 | | var enumerableType = typeof(IEnumerable<>).MakeGenericType(itemType); |
| | 1 | 98 | | var enumeratorType = typeof(IEnumerator<>).MakeGenericType(itemType); |
| | | 99 | | |
| | 1 | 100 | | var enumVar = Expression.Variable(enumeratorType, "enumerator"); |
| | 1 | 101 | | var getEnum = Expression.Call(collection, enumerableType.GetMethod(nameof(IEnumerable<object>.GetEnumerator))!); |
| | 1 | 102 | | var assignEnum = Expression.Assign(enumVar, getEnum); |
| | | 103 | | |
| | 1 | 104 | | var moveNext = Expression.Call(enumVar, typeof(System.Collections.IEnumerator).GetMethod(nameof(System.Collectio |
| | 1 | 105 | | var breakLabel = Expression.Label("LoopBreak"); |
| | | 106 | | |
| | 1 | 107 | | var loop = Expression.Loop( |
| | 1 | 108 | | Expression.IfThenElse( |
| | 1 | 109 | | Expression.IsFalse(moveNext), |
| | 1 | 110 | | Expression.Break(breakLabel), |
| | 1 | 111 | | Expression.Block( |
| | 1 | 112 | | new[] { loopVar }, |
| | 1 | 113 | | Expression.Assign(loopVar, Expression.Property(enumVar, nameof(IEnumerator<object>.Current))), |
| | 1 | 114 | | body)), |
| | 1 | 115 | | breakLabel); |
| | | 116 | | |
| | 1 | 117 | | return Expression.Block(new[] { enumVar }, assignEnum, loop); |
| | 1 | 118 | | } |
| | | 119 | | } |