|
3 | 3 | using System.Diagnostics;
|
4 | 4 | using System.Linq;
|
5 | 5 | using System.Linq.Expressions;
|
6 |
| -using System.Reflection; |
7 | 6 | using System.Threading;
|
8 | 7 | using System.Threading.Tasks;
|
9 | 8 | using MediatR;
|
|
12 | 11 |
|
13 | 12 | namespace Zapto.Mediator;
|
14 | 13 |
|
15 |
| -public class ServiceProviderMediator : IMediator |
| 14 | +public class ServiceProviderMediator : PublisherBase, IMediator |
16 | 15 | {
|
| 16 | + private IBackgroundPublisher? _background; |
17 | 17 | private readonly IServiceProvider _provider;
|
18 | 18 |
|
19 |
| - public ServiceProviderMediator(IServiceProvider provider) |
| 19 | + public IBackgroundPublisher Background => _background ??= _provider.GetRequiredService<IBackgroundPublisher>(); |
| 20 | + |
| 21 | + public ServiceProviderMediator(IServiceProvider provider) : base(provider) |
20 | 22 | {
|
21 | 23 | _provider = provider;
|
22 | 24 | }
|
@@ -226,75 +228,4 @@ CancellationToken cancellationToken
|
226 | 228 |
|
227 | 229 | return next();
|
228 | 230 | }
|
229 |
| - |
230 |
| - /// <inheritdoc /> |
231 |
| - public ValueTask Publish(object notification, CancellationToken cancellationToken = default) |
232 |
| - { |
233 |
| - return NotificationWrapper.Get(notification.GetType()).Handle(notification, cancellationToken, this); |
234 |
| - } |
235 |
| - |
236 |
| - public ValueTask Publish(MediatorNamespace ns, object notification, CancellationToken cancellationToken = default) |
237 |
| - { |
238 |
| - return NotificationWrapper.Get(notification.GetType()).Handle(ns, notification, cancellationToken, this); |
239 |
| - } |
240 |
| - |
241 |
| - /// <inheritdoc /> |
242 |
| - public async ValueTask Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) |
243 |
| - where TNotification : INotification |
244 |
| - { |
245 |
| - var didHandle = false; |
246 |
| - |
247 |
| - foreach (var handler in _provider.GetServices<INotificationHandler<TNotification>>()) |
248 |
| - { |
249 |
| - await handler.Handle(_provider, notification, cancellationToken); |
250 |
| - didHandle = true; |
251 |
| - } |
252 |
| - |
253 |
| - var didGenericHandle = await _provider |
254 |
| - .GetRequiredService<GenericNotificationHandler<TNotification>>() |
255 |
| - .Handle(_provider, notification, cancellationToken); |
256 |
| - |
257 |
| - if (!didHandle && !didGenericHandle && _provider.GetService<IDefaultNotificationHandler>() is { } defaultHandler) |
258 |
| - { |
259 |
| - await defaultHandler.Handle(_provider, notification, cancellationToken); |
260 |
| - } |
261 |
| - } |
262 |
| - |
263 |
| - /// <inheritdoc /> |
264 |
| - public async ValueTask Publish<TNotification>(MediatorNamespace ns, TNotification notification, CancellationToken cancellationToken = default) |
265 |
| - where TNotification : INotification |
266 |
| - { |
267 |
| - var services = _provider |
268 |
| - .GetServices<INamespaceNotificationHandler<TNotification>>() |
269 |
| - .Where(i => i.Namespace == ns); |
270 |
| - |
271 |
| - foreach (var factory in services) |
272 |
| - { |
273 |
| - await factory.GetHandler(_provider).Handle(_provider, notification, cancellationToken); |
274 |
| - } |
275 |
| - } |
276 |
| - |
277 |
| - /// <inheritdoc /> |
278 |
| - public IDisposable RegisterNotificationHandler(object handler, Func<Func<Task>, Task>? invokeAsync = null, bool queue = true) |
279 |
| - { |
280 |
| - if (queue) |
281 |
| - { |
282 |
| - if (invokeAsync is { } invoker) |
283 |
| - { |
284 |
| - invokeAsync = cb => |
285 |
| - { |
286 |
| - _ = Task.Run(() => invoker(cb)); |
287 |
| - return Task.CompletedTask; |
288 |
| - }; |
289 |
| - } |
290 |
| - else |
291 |
| - { |
292 |
| - invokeAsync = Task.Run; |
293 |
| - } |
294 |
| - } |
295 |
| - |
296 |
| - return (IDisposable) typeof(NotificationAttributeHandler<>).MakeGenericType(handler.GetType()) |
297 |
| - .GetMethod(nameof(NotificationAttributeHandler<object>.RegisterHandlers), BindingFlags.Static | BindingFlags.Public)! |
298 |
| - .Invoke(null, new[] { _provider, handler, invokeAsync }); |
299 |
| - } |
300 | 231 | }
|
0 commit comments