Files
ShrinkEventBus/Samples~/DotNetConsole/Program.cs
T
cneicy 0449e82c0c
Publish UPM package / publish (push) Successful in 2s
Publish NuGet packages / publish (push) Canceled after 3m12s
perf: reduce diagnostics and queued dispatch allocations in 2.3.2
2026-11-06 20:52:09 +08:00

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;
}