LokiCat.R3.ObservableEvents
0.0.13
dotnet add package LokiCat.R3.ObservableEvents --version 0.0.13
NuGet\Install-Package LokiCat.R3.ObservableEvents -Version 0.0.13
<PackageReference Include="LokiCat.R3.ObservableEvents" Version="0.0.13" />
<PackageVersion Include="LokiCat.R3.ObservableEvents" Version="0.0.13" />
<PackageReference Include="LokiCat.R3.ObservableEvents" />
paket add LokiCat.R3.ObservableEvents --version 0.0.13
#r "nuget: LokiCat.R3.ObservableEvents, 0.0.13"
#:package LokiCat.R3.ObservableEvents@0.0.13
#addin nuget:?package=LokiCat.R3.ObservableEvents&version=0.0.13
#tool nuget:?package=LokiCat.R3.ObservableEvents&version=0.0.13
LokiCat.R3.ObservableEvents
R3-compatible source generator for turning C# event
declarations into cached Observable<T>
properties.
This package eliminates boilerplate when wiring up C# event
s into R3 reactive observables.
It generates cached, lazily-connected observables for every event
declared on your partial interface
or partial class
.
โจ Features
- Automatically detects C#
event
declarations inside interfaces or classes - Generates type-safe
Observable<T>
orObservable<(T1, T2, ...)>
properties - Supports events with 0 to 5 parameters
- Emits clean diagnostic warnings for events with more than 5 parameters
- Requires no manual
Observable.Create
logic - Fully R3 idiomatic: observables can be piped, composed, and disposed with
AddTo(this)
- Generated code respects your
.editorconfig
styling preferences
๐ฆ Installation
- Add this NuGet package to your project:
dotnet add package LokiCat.R3.ObservableEvents
- Define your
event
s normally inside apartial interface
orpartial class
:
public partial interface IPauseMenu
{
event Action MainMenu;
event Action Resume;
event Action<Node, int> SaveProgress;
}
- Build your project. The generator will automatically emit:
private Observable<Unit> _onMainMenu;
public Observable<Unit> OnMainMenu =>
Event(ref _onMainMenu, handler => MainMenu += handler);
private Observable<Unit> _onResume;
public Observable<Unit> OnResume =>
Event(ref _onResume, handler => Resume += handler);
private Observable<(Node, int)> _onSaveProgress;
public Observable<(Node, int)> OnSaveProgress =>
Event(ref _onSaveProgress, handler => SaveProgress += handler);
You can now subscribe:
pauseMenu.OnResume
.Subscribe(() => Console.WriteLine("Game resumed"))
.AddTo(this);
โ Supported Event Forms
Event form | Generated Observable type |
---|---|
event Action Something() |
Observable<Unit> |
event Action<Node> Something() |
Observable<Node> |
event Action<Node, int> Something() |
Observable<(Node, int)> |
... up to 5 args | Observable<(T1, T2, ..., T5)> |
Events with 6+ parameters will trigger a diagnostic warning and will not generate observables.
๐ง How It Works
- The generator scans all syntax trees for
event
declarations. - It finds the corresponding delegate type (if external), and inspects its parameters.
- It emits a
private Observable<T>
cache field and apublic Observable<T>
property. - It uses the
Event(...)
extension method to lazily subscribe and forward events into R3 pipelines.
๐ Example: Full Interface
public partial interface ISettingsMenu
{
event Action Opened;
event Action<string> LanguageChanged;
}
๐ป๏ธ Generates:
private Observable<Unit> _onOpened;
public Observable<Unit> OnOpened =>
Event(ref _onOpened, handler => Opened += handler);
private Observable<string> _onLanguageChanged;
public Observable<string> OnLanguageChanged =>
Event(ref _onLanguageChanged, handler => LanguageChanged += handler);
๐งช Troubleshooting
- Your interfaces or classes must be marked
partial
- Events must use
Action
,Action<T>
, or compatible delegate types - Check
.g.cs
output inobj/Debug/.../generated/
- Use
#nullable enable
if you want null-safety - Rebuild your project to trigger generation
๐ License
MIT License
๐ก Bonus Tip
Pair this generator with R3, and your own event-driven architectures to create clean, reactive, and highly composable C# APIs without boilerplate.
Learn more about Target Frameworks and .NET Standard.
-
net7.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.