ArqueBus is a .Net core generic memory event bus.
Build | |
Tests | |
Coverage | |
Quality | |
Nuget |
The way to create a bus is really simple, just new EventBus<T, TModel>();
where
T
is the type used to find the subscription!TModel
is the type for the objects passed by the publications, you can useobject
, a base class or implement an interface for your models.
var eventBus = new EventBus<string, IModel>();
eventBus.Subscribe("test.first", (data) =>
{
Console.WriteLine(data.StringValue);
});
underTest.Publish("test.first", new Model(){StringValue = "my.string"});
// Create new event bus
var eventBus = new EventBus<string, object>();
// Listen for publications
var options = new DataflowBlockOptions { CancellationToken = cancellationTokenSource.Token };
var listenedTask = eventBus.ListenAsync<string>("test.first", options);
// publish something
eventBus.Publish("test.first", "published-string-1");
eventBus.Publish("test.first", "published-string-2");
// Cancel listening for publications or awaiting listnedTask will hang forever.
cancellationTokenSource.Cancel();
//Get data
var result = await listenedTask;
Console.Writeline(result.Skip(0).First()) // published-string-1
Console.Writeline(result.Skip(1).First()) // published-string-2