OoLunar.AsyncEvents 1.2.0

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

// Install OoLunar.AsyncEvents as a Cake Tool
#tool nuget:?package=OoLunar.AsyncEvents&version=1.2.0                

OoLunar.AsyncEvents

Async events for .NET

Features

  • Pre/Post event handler support.
  • AsyncEventHandlerPriority support.
  • Mark your event handlers with AsyncEventHandlerAttribute.
  • Add, modify, or remove your event handlers even after they've been prepared by calling AsyncEvent<TEventArgs>.Prepare() once you've made your changes.
  • AsyncEvent<TEventArgs> is thread-safe and can be used in multi-threaded environments.
  • A AsyncEventContainer to allow for easy management of multiple AsyncEvent<TEventArgs> instances and mass-preparation of all event handlers.

Installation

OoLunar.AsyncEvents is available on NuGet.

Web documentation can be found at https://oolunar.github.io/AsyncEvents/.

Usage

The async events design is based off of DSharpPlus.Commands's context check system, and is designed to be used in a similar manner. There are two main concepts to understand: pre/post handlers, and the handler priority system.

Pre/Post Handlers

Any method that is to be used as a pre or post handler should have the AsyncEventHandler attribute applied to it. This attribute takes an optional HandlerPriority parameter, which is an integer-based enum that determines the order in which handlers are executed. Handlers with higher/greater priority values are executed first. If two handlers have the same priority, the order in which they are executed is undefined.

[AsyncEventHandler(HandlerPriority.Highest)]
public async ValueTask<bool> LogAsync(MyAsyncEventArgs asyncEventArgs)
{
    // Read any data from the event args
    // Can return a boolean to indicate whether the event should continue
    _logger.LogInformation("Event {EventName} fired", asyncEventArgs.EventName);
    return true;
}

[AsyncEventHandler]
public async ValueTask ExecuteAsync(MyAsyncEventArgs asyncEventArgs)
{
    // Do something
}

It is important to note that pre handlers can return a boolean value to indicate whether the event should continue. If any pre handler returns false, the post handlers will not be executed. All pre handlers will run regardless of whether any of them return false. The only exception to this is if a pre handler throws an exception, in which case the behavior is undefined and determined by the caller.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.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.2.0 35 12/27/2024
1.1.1 28 12/27/2024
1.1.0 28 12/27/2024
1.0.0 74 12/21/2024