some changes
This commit is contained in:
parent
4ec9720541
commit
b47bac67ca
37 changed files with 397 additions and 190 deletions
32
Femto.Api/Infrastructure/EventBus.cs
Normal file
32
Femto.Api/Infrastructure/EventBus.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System.Threading.Channels;
|
||||
using Femto.Common.Integration;
|
||||
|
||||
namespace Femto.Api.Infrastructure;
|
||||
|
||||
public class EventBus(Channel<IEvent> channel) : BackgroundService, IEventBus
|
||||
{
|
||||
private readonly ICollection<IEventBus.Subscriber> _subscribers = [];
|
||||
|
||||
public Task Publish<T>(T evt)
|
||||
where T : IEvent
|
||||
{
|
||||
channel.Writer.TryWrite(evt);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Subscribe(IEventBus.Subscriber subscriber)
|
||||
{
|
||||
this._subscribers.Add(subscriber);
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await foreach (var message in channel.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
await Task.WhenAll(
|
||||
this._subscribers.Select(subscriber => subscriber.Invoke(message, stoppingToken))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue