Skip to content

Lightweight, thread-safe C# event bus (event aggregator) for games and apps. Designed for simplicity, ease of use and robustness.

License

Notifications You must be signed in to change notification settings

Lurler/TrimKit.EventBus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TrimKit.EventBus

A lightweight and mutation-safe event bus (event aggregator) for C# applications and games. Designed for simplicity and ease of use.

TrimKit.EventBus allows different parts of your system to communicate via strongly-typed events without hard dependencies or direct references.

Features

  • Lightweight & dependency-free.
  • Thread-safe.
  • Mutation-safe - handlers can safely modify subscriptions even during event publishing.
  • Disposable subscriptions - each Subscribe() returns an IDisposable token for easy unsubscribe.
  • SubscribeOnce() support - automatically unsubscribes after first event.
  • Duplicate protection - prevents duplicate handler registration.

Installation

Use provided nuget package or download the source.

NuGet

🔧 dotnet add package TrimKit.EventBus

Quick start

Define your events, they must inherit from EventArgs:

public sealed class GameStartedEvent : EventArgs
{
    public DateTime StartTime { get; }
    public GameStartedEvent(DateTime startTime) => StartTime = startTime;
}

public sealed class CharacterDamagedEvent : EventArgs
{
    public string Name { get; }
    public int Damage { get; }
    public CharacterDamagedEvent(string name, int damage)
    {
        Name = name;
        Damage = damage;
    }
}

Create the event bus itself:

var bus = new EventBus();

Add subscribers. You can subscribe normally or use SubscribeOnce() for a one-time handler.

// normal subscription
var token = bus.Subscribe<CharacterDamagedEvent>((s, e) =>
{
    Console.WriteLine($"{e.Name} took {e.Damage} damage!");
});

// subscribe once
bus.SubscribeOnce<GameStartedEvent>((s, e) =>
{
    Console.WriteLine($"[Once] Game started at {e.StartTime:T}");
});

Each Subscribe() returns an IDisposable token you can later call Dispose() on to unsubscribe:

token.Dispose();

And now you can start publishing events:

bus.Publish(new GameStartedEvent(DateTime.Now));
bus.Publish(null, new CharacterDamagedEvent("Goblin", 25));

Thread safety

All operations (Subscribe, Unsubscribe, Publish, Reset) are protected by a lock and safe for concurrent use. During event dispatch, handlers are called using a snapshot copy, so you can safely modify subscriptions while publishing.

API Overview

Method Description
IDisposable Subscribe<T>(EventHandler<T> handler) Subscribe to event T.
IDisposable SubscribeOnce<T>(EventHandler<T> handler) Subscribe for one-time delivery.
bool Unsubscribe<T>(EventHandler<T> handler) Remove specific subscription.
void Publish<T>(object? sender, T eventArgs) Publish event to all subscribers.
void Publish<T>(T eventArgs) Publish event to all subscribers.
int GetSubscriberCount<T>() Get count of current subscribers for a type.
void Reset() Remove all subscriptions.

Changes

  • v1.0.1 - Fixed namespaces.
  • v1.0 - Initial release.

TrimKit Collection

This library is part of the TrimKit collection - a set of small, focused C# libraries that make game development more enjoyable by reducing the need for boilerplate code and providing simple reusable building blocks that can be dropped into any project.

Each module is independent and can be used standalone or combined with others for a complete lightweight foundation.

Contribution

Contributions are welcome!

You can start with submitting an issue on GitHub.

License

This library is released under the MIT License.

About

Lightweight, thread-safe C# event bus (event aggregator) for games and apps. Designed for simplicity, ease of use and robustness.

Topics

Resources

License

Stars

Watchers

Forks

Languages