Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/reactivemarbles/observableevents
https://github.com/reactivemarbles/observableevents
csharp dotnet
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/reactivemarbles/observableevents
- Owner: reactivemarbles
- License: mit
- Created: 2020-09-21T08:59:54.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-28T22:29:58.000Z (7 months ago)
- Last Synced: 2024-05-01T11:22:29.871Z (7 months ago)
- Topics: csharp, dotnet
- Language: C#
- Size: 303 KB
- Stars: 117
- Watchers: 7
- Forks: 10
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Observable Event Generator
This project is a .NET source generator which produces `IObservable` for events contained within a object including all base classes. `ObservableEvents` generator will convert events within an assembly and create observable wrappers for them, it is based on [Pharmacist](https://github.com/reactiveui/Pharmacist) and uses [.NET Source Generator](https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview) technology.
## NuGet Packages
Make sure your project is using the newer `PackageReference` inside your CSPROJ. The older style is buggy and should be moved away from regardless. See here for discussions how to [upgrade](https://docs.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference).
Install the following packages to start using Observable Events.
| Name | Platform | NuGet |
| ----------------------------- | ----------------- | -------------------------------- |
| [ReactiveMarbles.ObservableEvents.SourceGenerator][Core] | Core - Libary | [![CoreBadge]][Core] |[Core]: https://www.nuget.org/packages/ReactiveMarbles.ObservableEvents.SourceGenerator/
[CoreBadge]: https://img.shields.io/nuget/v/ReactiveMarbles.ObservableEvents.SourceGenerator.svg## Manual Installation
Include the following in your .csproj file
```xml
```
The `PrivateAssets` will prevent the ObservableEvents source generator from being inherited by other projects.
## How to use
### Instance Based
It injects a class for instance based events into your source code which will expose a extension method called `Events()`. You need to include the namespace `ReactiveMarbles.ObservableEvents` to access to the extension method. You can then use this to get `IObservable` instances from your events.
```cs
using ReactiveMarbles.ObservableEvents;public void MyClass : INotifyPropertyChanged
{
// Assumes this belong in a class with a event called PropertyChanged.
public void RunEvents()
{
this.Events().PropertyChanged.Subscribe(x => Console.WriteLine($"The {x} property has changed"));
}public event PropertyChangedEventHandler PropertyChanged;
}
```### Static Events
You must use include a attribute `GenerateStaticEventObservables` on the assembly level for a particular class. This will generate a class `RxEvents` in the same namespace as the specified class.
```cs
[assembly: GenerateStaticEventObservablesAttribute(typeof(StaticTest))]
public static class StaticTest
{
public static event EventHandler? TestChanged;
}
```