ByteAether.WeakEvent 1.0.0

dotnet add package ByteAether.WeakEvent --version 1.0.0
                    
NuGet\Install-Package ByteAether.WeakEvent -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ByteAether.WeakEvent" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ByteAether.WeakEvent" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ByteAether.WeakEvent" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ByteAether.WeakEvent --version 1.0.0
                    
#r "nuget: ByteAether.WeakEvent, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ByteAether.WeakEvent&version=1.0.0
                    
Install ByteAether.WeakEvent as a Cake Addin
#tool nuget:?package=ByteAether.WeakEvent&version=1.0.0
                    
Install ByteAether.WeakEvent as a Cake Tool

WeakEvent

from ByteAether

License NuGet Version NuGet Downloads GitHub Build Status GitHub Security

WeakEvent is a lightweight .NET library that implements the weak event pattern using weak references. It ensures that event subscribers don't prevent garbage collection, avoiding memory leaks.

Key Features

.NET Standard 2.0

  • Weak References: Subscribers are held weakly so they can be garbage-collected when no longer in use.
  • Events With or Without Data: Choose between WeakEvent for notifications without data and WeakEvent<TEvent> for events that pass data.
  • Automatic Cleanup: Dead subscribers are removed automatically during event publishing.
  • Simple API: Intuitive methods for subscribing, unsubscribing, and publishing events.

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 instance of the weak event without event data
var myEvent = new WeakEvent();

// Create a subscriber and subscribe
var subscriber = () => Console.WriteLine("Event received!");
myEvent.Subscribe(subscriber);

// Raise the event
await myEvent.PublishAsync();

Using the WeakEvent<TEvent>

using ByteAether.WeakEvent;

// Create an instance of the weak event with event data
var myEvent = new WeakEvent<MyEventData>();

// Create a subscriber and subscribe
var subscriber = (MyEventData data) => Console.WriteLine("Received: " + data.Message);
myEvent.Subscribe(subscriber);

// Raise the event
await myEvent.PublishAsync(new MyEventData("Hello, World!"));

// Define your event data
public record MyEventData(string Message);

API Overview

WeakEvent

  • Subscribe(Action handler)
    Subscribe(Func<Task> handler)
    Subscribe(Func<CancellationToken, Task> handler)
    Subscribes the specified handler to the event. The handler will be invoked when the event is raised, provided that its target is still alive.
  • Unsubscribe(Action handler)
    Unsubscribe(Func<Task> handler)
    Unsubscribe(Func<CancellationToken, Task> handler)
    Unsubscribes the specified handler from the event.
  • PublishAsync(CancellationToken cancellationToken = default)
    Raises the event by invoking all live subscribers. Dead subscribers (whose targets have been garbage-collected) are removed.

WeakEvent<TEvent>

  • Subscribe(Action<TEvent> handler)
    Subscribe(Func<TEvent, Task> handler)
    Subscribe(Func<TEvent, CancellationToken, Task> handler)
    Subscribes the specified handler to the event. The handler will be invoked when the event is raised, provided that its target is still alive.
  • Unsubscribe(Action<TEvent> handler)
    Unsubscribe(Func<TEvent, Task> handler)
    Unsubscribe(Func<TEvent, CancellationToken, Task> handler)
    Unsubscribes the specified handler from the event.
  • PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)
    Raises the event by invoking all live subscribers with the provided event data. Dead subscribers (whose targets have been garbage-collected) are removed.
Product 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
1.0.0 133 3/18/2025
0.1.0 84 2/24/2025