Baksteen.Async.ModernEvents 0.1.0

dotnet add package Baksteen.Async.ModernEvents --version 0.1.0
NuGet\Install-Package Baksteen.Async.ModernEvents -Version 0.1.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="Baksteen.Async.ModernEvents" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Baksteen.Async.ModernEvents --version 0.1.0
#r "nuget: Baksteen.Async.ModernEvents, 0.1.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.
// Install Baksteen.Async.ModernEvents as a Cake Addin
#addin nuget:?package=Baksteen.Async.ModernEvents&version=0.1.0

// Install Baksteen.Async.ModernEvents as a Cake Tool
#tool nuget:?package=Baksteen.Async.ModernEvents&version=0.1.0

Baksteen.Async.ModernEvents

ModernEvents are async capable events that support both sync and async subscribers. This implementation is modeled more or less after David Fowler's suggestion on twitter.

Usage

On the producer side, we have to expose a IModernEvent<TArg> property:

using Baksteen.Async.ModernEvents
...

public class ExampleWithModernEvents
{
    // example event argument record
    public record ClientAddedArgs(string Name);

    // privately declare a concrete ModernEvent field
    private ModernEvent<ClientAddedArgs> _clientAddedEvent = new();

    // publicly only expose the IModernEvent interface
    public IModernEvent<ClientAddedArgs> OnClientAdded { get => _clientAddedEvent; }

    public async Task AddClient(string newClientName)
    {
        // add client to database
        ...

        // notify all (sync & async) subscribers
        await _clientAddedEvent.InvokeAsync(new ClientAddedArgs{ Name = newClientName });
    }
}


On the consumer side, subscribing then looks as follows:

var clientRegistry = new ExampleWithModernEvents();

// sync subscription:
clientRegistry.OnClientAdded.Subscribe(args =>
    Console.WriteLine($"Client {args.Name} was added"));

// async subscription:
clientRegistry.OnClientAdded.Subscribe(async args => {
    Console.WriteLine($"Client {args.Name} was added");
    await Task.CompletedTask; });


The Subscribe() method returns a IDisposable so the consumer can unsubscribe from the event:

var clientRegistry = new ExampleWithModernEvents();

// async subscription:
var subscription = clientRegistry.OnClientAdded.Subscribe(async args => {
    Console.WriteLine($"Client {args.Name} was added");
    await Task.CompletedTask; });

// events will be received here

subscription.Dispose();

// we're no longer subscribed here
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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
0.1.0 71 4/19/2024

First release