ToolBX.SignalBus
3.0.0
dotnet add package ToolBX.SignalBus --version 3.0.0
NuGet\Install-Package ToolBX.SignalBus -Version 3.0.0
<PackageReference Include="ToolBX.SignalBus" Version="3.0.0" />
paket add ToolBX.SignalBus --version 3.0.0
#r "nuget: ToolBX.SignalBus, 3.0.0"
// Install ToolBX.SignalBus as a Cake Addin #addin nuget:?package=ToolBX.SignalBus&version=3.0.0 // Install ToolBX.SignalBus as a Cake Tool #tool nuget:?package=ToolBX.SignalBus&version=3.0.0
SignalBus
Getting started
//The signal identifier can be just about anything but I do recommend using enums
public enum Signal
{
Something,
SomethingElse,
AnotherThing
}
private readonly ISignalBus _signalBus;
public SomeService(ISignalBus signalBus)
{
_signalBus = signalBus;
}
public void SomeMethod()
{
//Whenever Signal.Something is triggered anywhere in your application this will call the 'TheSomethingMethod' below
_signalBus.Subscribe(Signal.Something, TheSomethingMethod)
}
public SomeOtherMethod()
{
//This will trigger every action that has subscribed to the Signal.Something identifier with this argument
_signalBus.Trigger(Signal.Something, new ActualArgumentType { Name = "Henry", Level = 15, Job = Job.Warrior });
}
//You do need an object-typed args parameter even if you don't use arguments... I haven't found a better way to deal with this yet unfortunately
private void TheSomethingMethod(object args)
{
var arguments = (ActualArgumentType)args;
...
}
Setup
With AutoInject
If you already use AutoInject or AssemblyInitializer then you're already good to go.
See the AutoInject repo for more information on how to set it up.
Without AutoInject
Add the following line when adding services.
services.AddSignalBus();
Subscribing
There are two ways to subscribe to events : Subscribe
and SubscribeRetroactively
.
Subscribe
works like you would expect. The subscribing callback will be called when an object triggers the signal. SubscribeRetroactively
does the same thing but will trigger right away if the signal was already triggered when it subscribed. It may not apply to every use case but it's useful when you want to avoid time dependencies.
Important
This may seem like a great idea to apply everywhere but I strongly advise against it. There are times when using signals is cleaner than the alternative but other times when you should prioritize localized events.
Signals should only be used for global events that affect many objects simultaneously.
For instance, if the player needs to know when an enemy dies then it may be a good idea to use signals. This way, the player doesn't have to subscribe to every single enemy's OnDeath event directly.
However, if you want to know when the player's collider comes into collision with an object then it may be a better idea for your player to subscribe to its collider's event directly instead of broadcasting the collision via the SignalBus. Unless other unrelated objects need to know about this of course.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- ToolBX.AutoInject (>= 3.0.0)
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 |
---|---|---|
3.0.0 | 104 | 9/26/2024 |
3.0.0-beta1 | 80 | 9/23/2024 |
2.2.0 | 125 | 1/13/2024 |
2.2.0-beta2 | 127 | 1/7/2024 |
2.2.0-beta1 | 126 | 12/13/2023 |
2.1.0 | 128 | 12/12/2023 |
2.0.3 | 188 | 6/19/2023 |
2.0.2 | 175 | 6/2/2023 |
2.0.2-beta3 | 118 | 5/27/2023 |
2.0.2-beta2 | 120 | 5/23/2023 |
2.0.2-beta1 | 130 | 5/23/2023 |
2.0.1 | 218 | 4/26/2023 |
2.0.0 | 365 | 11/9/2022 |
2.0.0-beta1 | 156 | 9/29/2022 |
1.0.1 | 424 | 9/29/2022 |
1.0.0 | 449 | 3/5/2022 |