41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
using ShrinkEventBus;
|
|
|
|
using var host = new ShrinkEventBusHost();
|
|
var bus = host.CreateBus(ShrinkBusKey.Game, ShrinkBusOptions.Inline());
|
|
var subscriber = new OrderSubscriber();
|
|
using var binding = host.Attach(subscriber);
|
|
var diagnostics = 0;
|
|
using var observation = EventBus.ObserveDiagnostics(_ => diagnostics++);
|
|
|
|
bus.Post(new OrderPlaced(2));
|
|
await bus.PostAsync(new OrderPlaced(3));
|
|
if (subscriber.Total != 5 || diagnostics != 2)
|
|
throw new InvalidOperationException("Generated subscription or diagnostics failed.");
|
|
|
|
var workerKey = new ShrinkBusKey("worker", "orders");
|
|
var worker = host.CreateBus(workerKey, ShrinkBusOptions.DedicatedThread());
|
|
var workerSubscriber = new OrderSubscriber();
|
|
using var workerBinding = host.Attach(workerSubscriber, workerKey);
|
|
await worker.PostAsync(new OrderPlaced(7));
|
|
if (workerSubscriber.Total != 7)
|
|
throw new InvalidOperationException("Dedicated-thread delivery failed.");
|
|
|
|
binding.Dispose();
|
|
bus.Post(new OrderPlaced(10));
|
|
if (subscriber.Total != 5)
|
|
throw new InvalidOperationException("Unsubscribe failed.");
|
|
|
|
await host.ShutdownAsync();
|
|
Console.WriteLine("EVENTBUS_CONSOLE_PASS: sync, async, diagnostics, worker, unsubscribe");
|
|
|
|
public readonly record struct OrderPlaced(int Amount) : IShrinkEvent;
|
|
|
|
[ShrinkEventSubscriber]
|
|
public sealed partial class OrderSubscriber
|
|
{
|
|
public int Total { get; private set; }
|
|
|
|
[ShrinkSubscribe]
|
|
private void OnOrder(OrderPlaced order) => Total += order.Amount;
|
|
}
|