https://github.com/oolunar/asyncevents
Async events for .NET
https://github.com/oolunar/asyncevents
Last synced: 8 months ago
JSON representation
Async events for .NET
- Host: GitHub
- URL: https://github.com/oolunar/asyncevents
- Owner: OoLunar
- License: mit
- Created: 2024-12-21T06:28:40.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-01-16T22:43:12.000Z (9 months ago)
- Last Synced: 2025-02-10T19:48:13.043Z (8 months ago)
- Language: C#
- Homepage: https://www.forsaken-borders.net/AsyncEvents/
- Size: 116 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.Prepare()` once you've made your changes.
- `AsyncEvent` is thread-safe and can be used in multi-threaded environments.
- A `AsyncEventContainer` to allow for easy management of multiple `AsyncEvent` instances and mass-preparation of all event handlers.## Installation
`OoLunar.AsyncEvents` is available on [NuGet](https://www.nuget.org/packages/OoLunar.AsyncEvents/).
Web documentation can be found at [https://oolunar.github.io/AsyncEvents/](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.```csharp
[AsyncEventHandler(HandlerPriority.Highest)]
public async ValueTask 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.