ByteAether.WeakEvent
1.1.0
Prefix Reserved
dotnet add package ByteAether.WeakEvent --version 1.1.0
NuGet\Install-Package ByteAether.WeakEvent -Version 1.1.0
<PackageReference Include="ByteAether.WeakEvent" Version="1.1.0" />
<PackageVersion Include="ByteAether.WeakEvent" Version="1.1.0" />
<PackageReference Include="ByteAether.WeakEvent" />
paket add ByteAether.WeakEvent --version 1.1.0
#r "nuget: ByteAether.WeakEvent, 1.1.0"
#:package ByteAether.WeakEvent@1.1.0
#addin nuget:?package=ByteAether.WeakEvent&version=1.1.0
#tool nuget:?package=ByteAether.WeakEvent&version=1.1.0
WeakEvent
from ByteAether
WeakEvent is a lightweight .NET implementation of the weak event pattern. A normal event can keep an inactive subscriber alive because the publisher holds a reference to its handler. WeakEvent breaks that retention by storing instance handlers with weak references, allowing subscriber objects to be reclaimed when no longer used elsewhere, even if they have not explicitly unsubscribed.
The library fits applications where long-lived publishers notify short-lived or repeatedly created subscribers, such as UI components, desktop applications, background services, and Blazor components. It supports synchronous and asynchronous handlers, notifications with or without payload data, and cancellation-aware publishing. Static handlers remain alive for the lifetime of the event source, while dead instance subscriptions are ignored and cleaned up during publication.
Key Features
- Weak References: Subscribers are held weakly so they can be garbage-collected when no longer in use.
- Events With or Without Payloads: Use
WeakEventfor notifications orWeakEvent<TEvent>to pass event data. - Synchronous and Asynchronous Handlers: Subscribe with
Action,Func<Task>, or a cancellation-aware async delegate. - Automatic Cleanup: Dead subscribers are removed during publication.
- Simple API: Methods for subscribing, unsubscribing, and publishing.
Publication behavior
- Subscribers are invoked sequentially in subscription order.
- Publication uses a subscriber snapshot captured when it starts. Subscribing or unsubscribing during publication affects later publications, not the current one.
- Cancellation is checked before publication starts and before each subscriber is invoked. Cancellation-aware handlers receive the token passed to
PublishAsync. - Static handlers remain alive for the lifetime of the event source. Instance handlers, including capturing lambdas, are held weakly.
- Synchronous handler exceptions propagate unchanged. Asynchronous handler exceptions propagate after the returned task is awaited.
- Each invocation in a multicast delegate is subscribed independently and can be unsubscribed as a group.
Installation
Install via NuGet:
dotnet add package ByteAether.WeakEvent
Use the --version option to specify a preview version to install.
Usage
Using the WeakEvent
using ByteAether.WeakEvent;
// Create an event without payload data.
var myEvent = new WeakEvent();
// Create and subscribe a handler.
var subscriber = () => Console.WriteLine("Event received!");
myEvent.Subscribe(subscriber);
// Publish the event.
await myEvent.PublishAsync();
Using the WeakEvent<TEvent>
using ByteAether.WeakEvent;
// Create an event with payload data.
var myEvent = new WeakEvent<MyEventData>();
// Create and subscribe a handler.
var subscriber = (MyEventData data) => Console.WriteLine("Received: " + data.Message);
myEvent.Subscribe(subscriber);
// Publish the event.
await myEvent.PublishAsync(new MyEventData("Hello, World!"));
// Define your event data
public record MyEventData(string Message);
API Overview
WeakEvent
SubscriberCount
Number of live subscribers currently registered.Subscribe(Action handler)
Subscribe(Func<Task> handler)
Subscribe(Func<CancellationToken, Task> handler)
Subscribes the handler. Each invocation in a multicast delegate is subscribed independently. Live handlers are invoked when the event is published.Unsubscribe(Action handler)
Unsubscribe(Func<Task> handler)
Unsubscribe(Func<CancellationToken, Task> handler)
Unsubscribes the handler and returns whether at least one matching invocation was removed. A multicast delegate unsubscribes all matching invocations.PublishAsync(CancellationToken cancellationToken = default)
Invokes live subscribers sequentially and removes dead subscribers.
WeakEvent<TEvent>
SubscriberCount
Number of live subscribers currently registered.Subscribe(Action<TEvent> handler)
Subscribe(Func<TEvent, Task> handler)
Subscribe(Func<TEvent, CancellationToken, Task> handler)
Subscribes the handler. Each invocation in a multicast delegate is subscribed independently. Live handlers are invoked when the event is published.Unsubscribe(Action<TEvent> handler)
Unsubscribe(Func<TEvent, Task> handler)
Unsubscribe(Func<TEvent, CancellationToken, Task> handler)
Unsubscribes the handler and returns whether at least one matching invocation was removed. A multicast delegate unsubscribes all matching invocations.PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)
Invokes live subscribers sequentially witheventDataand removes dead subscribers.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.