ToolBX.SignalBus 2.2.0

dotnet add package ToolBX.SignalBus --version 2.2.0
NuGet\Install-Package ToolBX.SignalBus -Version 2.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ToolBX.SignalBus" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ToolBX.SignalBus --version 2.2.0
#r "nuget: ToolBX.SignalBus, 2.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ToolBX.SignalBus as a Cake Addin
#addin nuget:?package=ToolBX.SignalBus&version=2.2.0

// Install ToolBX.SignalBus as a Cake Tool
#tool nuget:?package=ToolBX.SignalBus&version=2.2.0

SignalBus

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 Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.2.0 102 1/13/2024
2.2.0-beta2 82 1/7/2024
2.2.0-beta1 97 12/13/2023
2.1.0 98 12/12/2023
2.0.3 158 6/19/2023
2.0.2 146 6/2/2023
2.0.2-beta3 89 5/27/2023
2.0.2-beta2 91 5/23/2023
2.0.2-beta1 103 5/23/2023
2.0.1 186 4/26/2023
2.0.0 331 11/9/2022
2.0.0-beta1 131 9/29/2022
1.0.1 392 9/29/2022
1.0.0 409 3/5/2022