InterfaceEvents 2.0.1
dotnet add package InterfaceEvents --version 2.0.1
NuGet\Install-Package InterfaceEvents -Version 2.0.1
<PackageReference Include="InterfaceEvents" Version="2.0.1" />
paket add InterfaceEvents --version 2.0.1
#r "nuget: InterfaceEvents, 2.0.1"
// Install InterfaceEvents as a Cake Addin #addin nuget:?package=InterfaceEvents&version=2.0.1 // Install InterfaceEvents as a Cake Tool #tool nuget:?package=InterfaceEvents&version=2.0.1
Documentation here: https://thedudxo.github.io/InterfaceEvents/index.html
Features
- Items with a higher priority are called first. negative priorities are fine.
- Subscribing/Unsubscribing during an event will take effect afterwards.
- Optionally only notify subscribers once. After the initial event, any new subscribers are notified apon subscription.
- Optionally only notify subscribers with the highest priority.
- Optionally Create Priority Dictionaries that determine what order types should receive events in relative to eachother.
Why interfaces?
Pros
- No unesseceary Sender/Args parameters (though you can add them if needed)
- more explicit declerations:
- It's immedately clear when a class receives events
- Guarenteed to use the same method names
- personally prefer ".Subscribe(x)" to "+="
- The above features I've been able to add
Cons
- Not core to c#
- Sending an event has slighty weird syntax:
myEvent.Send(s => s.OnMyEvent());
- This is to allow any method signature to be used for receving the event.
Examples
Getting Started
Creating and subscribing to event
using DudCo.Events;
interface IOnEvent
{
void OnEvent();
}
class EventReceiver : IOnEvent
{
public void OnEvent()
{
//do event stuff
}
}
class EventsExample
{
public void Main()
{
EventSender<IOnEvent> someEvent = new EventSender<IOnEvent>();
EventReceiver receiver = new EventReceiver()
someEvent.Subscribe(reveiver);
someEvent.Send(s => s.OnEvent());
}
}
Items can only be subscribed to an event once, and you'll get a System.ArgumentException
if you try to subscribe multiple times.
<hr>
Subscribing with priority
someEvent.Subscribe(something, 5)
someEvent.Subscribe(somethingElse, -2)
<hr>
Subscribing in bulk
Event.Subscribe(a, b);
when bulk subscribing, the priority must be specified first:
Event.Subscribe(2, c, d, e);
also works with unsubscribing:
Event.Unsubscribe(a, b, c, d, e);
<hr>
TrySubscribe
If the item you're trying to subscribe might allready be subscribed, you can use TrySubscribe()
and TryUnsubscribe()
methods.
They will not throw exceptions, but instead return a sucsess/failure bool.
Dont use it everywhere, only if you need to, as it will hide bugs (by not throwing).
<hr>
Sending event with parameters
public interface IOnCollision
{
void OnCollision(CollisionInfo collision);
}
private void SendCollisionEvent(CollisionInfo info)
{
collisionEvent.Send(s => s.OnCollision(info));
}
<hr>
Optional Features
EventBuilder
The event builder can be used to create EventSenders with advanced features.
This will create the default EventSender, same as using new EventSender()
:
EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
.Build();
<hr>
Type Priority Dictionaries
Type priority dictionaries let you specify what priority all instances of a type will be given when subscribing via eventSender.SubscribeByRegisteredType()
An item in the dictionary subscribed with eventSender.Subscribe()
will throw an exception.
public class MyPriorityDictionary : PriorityDictionary
{
public MyPriorityDictionary()
{
Add<ClassA>(0);
Add<ClassB>(After<ClassA>());
Add<ClassC>(Before<ClassB>());
}
}
Create by either passing in the constructor, or using an EventBuilder.
EventSender<IOnEvent> myEvent = new EventSender<IOnEvent>(new MyPriorityDictionary());
EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
.WithPriorityDictionary(new MyPriorityDictionary())
.Build();
<hr>
Only notify highest priority
This option will only send events to the subscriber with the highest priortiy. all subscribers which share the highest priority are notified.
EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
.SendOnlyHighestPriority()
.Build();
<hr>
Only notify once
This option will only alow an event to be sent once. after which:
- current subscribers are unsubscribed
- new subscribers are not subscribed, but instead receive the event immediately
this means that the EventSender holds no references to the subscribers after the event has been sent, so they may be garbage collected.
this option is usefull for delaying the creation of dependant objects untill thier dependancies have been created.
trying to send the event multiple times will throw a System.InvalidOperationException
.
EventSender<IOnEvent> myEvent = new EventBuilder<IOnEvent>()
.SendOnlyOnce()
.Build();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.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.
Resending an event after a subscriber threw an exception does not cause eventSender to throw a concurrent send exception.
added Subscribe(int priority, T item) overload.