English | Русский
A library of wrapper classes that turn events and values into standalone objects.
This allows them to be passed around directly through a unified API for subscribing and awaiting.
- Event passing. Allows passing events as arguments to methods or other classes.
- Unification. Common types for events and values avoid duplicated subscription and awaiting logic across classes.
- Performance. A delegate-based Observer implementation with struct subscriptions and minimal additional allocations.
- Asynchrony. Supports await via
UniTask; the integration is compiled only when the UniTask package is installed.
UPM (Unity Package Manager)
https://github.com/CatCodeGames/EventPrimitives.git?path=Assets/EventPrimitives
- Events — when compatibility with standard C# events (
+=/-=) matters. - Observable — when subscriptions are added and removed often: subscribing does not allocate, and unsubscribing takes effect immediately, even during a notification.
Events are lightweight wrappers around standard C# events, exposing them through a unified API provided by the library.
// Initialize with notification only when the value changes
EventValue<int> state = new EventValue<int>(0, NotificationMode.OnChange);
state.Value = 10;
state.Changed += value => Debug.Log(value);
// Subscribe and immediately invoke the handler with the current value
state.AddListener(value => Debug.Log(value), invokeImmediately: true);
// Subscribe with automatic unsubscription at the end of the using block (struct, no allocations)
using var handle = state.AddListenerScoped(value => Debug.Log(value));
// Manual subscription lifetime management (the struct can be stored in a field)
var token = state.AddListenerScoped(value => Debug.Log(value), invokeImmediately: true);
token.Dispose();
var cts = new CancellationTokenSource();
// Asynchronously wait for a condition, checking the current value first
await state.WaitAsync(value => value > 10, cts.Token);
// Asynchronously wait for the next value change
await state.WaitAsync(cts.Token);EventValue<T> — a container for a value and its change event.
It can notify subscribers on every write or only when the value actually changes.
The IReadOnlyEventValue interface prevents external code from changing the value and invoking the event.
EventSource — a container for an event.
The IReadOnlyEventSource interface prevents external code from invoking the event.
Subscription methods can invoke the handler immediately when it is added.
AddListener/RemoveListener— subscribe to and unsubscribe from the event directly.AddListenerScoped— returns a struct that unsubscribes onDispose(): automatically at the end of a using block, or manually if the struct is stored in a field.WaitAsync— asynchronously waits for an event viaUniTaskwithout allocations.
Observable is a lightweight delegate-based implementation of the Observer pattern. Subscriptions are structs, which keeps additional allocations to a minimum. The implementation is single-threaded, as is usual in Unity.
// Initialize with notification only when the value changes
var state = new ObservableValue<int>(0, NotificationMode.OnChange);
state.Value = 10;
// Subscribe
var subscription = state.Subscribe(value => Debug.Log(value));
// Unsubscribe
subscription.Unsubscribe();
// or
subscription.Dispose();
var cts = new CancellationTokenSource();
// Asynchronously wait for a condition, checking the current value first
await state.WaitAsync(value => value > 10, cts.Token);ObservableValue<T> — a container for a value and its change event.
It can notify subscribers on every write or only when the value actually changes.
The IReadOnlyObservableValue interface prevents external code from changing the value and invoking the event.
ObservableSource — a container for an event.
The IReadOnlyObservableSource interface prevents external code from invoking the event.
Subscribers are kept in an internal SlotMap — an array of slot structs that are reused after removal. This reduces allocations when adding and removing subscribers.
The classes are single-threaded. A subscriber unsubscribed during a notification is not called any more, and one added during a notification is called starting with the next one. Unlike a standard C# event, which works on a snapshot, an unsubscription takes effect immediately.
WaitAsync— asynchronously waits for an event viaUniTaskwithout allocations.