How emre-emiroglu/signalbus works
1. Project type and scope
- This is a Unity UPM package named SignalBus.
- It provides a lightweight publish/subscribe event bus for decoupled communication between Unity components and plain C# classes.
- The repository is small and focused: runtime source, tests, and package metadata.
2. Top-level architecture
- The code is split into three main parts:
- Runtime/: the actual package implementation
- Tests/EditMode/: editor-side unit tests
- Tests/PlayMode/: runtime behavior tests in Unity play mode
- package.json and the .asmdef files define Unity package and assembly boundaries.
3. Public API layer
- The main entry point for consumers is SignalBusUtilities.
- It acts as a static façade, so client code can call Declare, Subscribe, Unsubscribe, and Fire without manually managing a bus instance.
- This is the boundary most users interact with; it hides the underlying construction/lifecycle concerns.
4. Core bus implementation
- SignalBus is the central runtime object that owns signal declarations and dispatch behavior.
- It is responsible for:
- tracking whether a signal type has been declared
- registering subscribers
- removing subscribers
- firing signals to listeners
- The README indicates it enforces declaration-before-use and throws on duplicate declarations.
5. Subscription and dispatch mechanics
- SignalBinding is the internal delivery mechanism used by SignalBus.
- It holds the receiver list for a given signal and manages:
- adding/removing handlers
- invoking handlers
- checking whether any subscribers exist
- The important architectural detail is that signal dispatch is not just a raw list of callbacks; it is a managed binding layer.
6. Priority-based ordering
- Subscriptions support a priority value.
- That priority affects the order in which callbacks are invoked when a signal is fired.
- The README also notes that unsubscribe must use the same priority value used at subscribe time, which means priority is part of the identity of the subscription.
7. Signal lifecycle and data flow
- The normal flow is:
- client declares a signal type
- client subscribes one or more callbacks
- client fires a signal instance
- SignalBus finds the corresponding binding
- SignalBinding invokes subscribers in priority order
- If a signal is fired with no subscribers, the package logs a warning rather than failing silently.
8. Error and guard behavior
- The package appears to enforce a few important rules:
- multiple declarations are rejected with an exception
- use before declaration is rejected with an exception
- no subscribers on fire generates a warning
- These checks make the bus deterministic and help catch configuration mistakes early.
9. Unity-specific packaging
- Runtime/SignalBus.Runtime.asmdef isolates the runtime code into its own assembly.
- Separate test assemblies exist for edit mode and play mode.
- This structure matters for architecture diagrams because it shows the package is designed to be imported into Unity projects as a reusable module, not as an application.
10. Test coverage boundary
- The presence of both EditMode and PlayMode tests suggests the package validates both pure logic and Unity-runtime integration behavior.
- This usually means:
- EditMode tests cover core bus logic deterministically
- PlayMode tests validate behavior in an actual Unity runtime context
- The tests sit outside the runtime assembly, reinforcing a clean separation between implementation and verification.
11. External integration surface
- The only meaningful external dependency implied by the repo structure is Unity itself.
- The package is meant to be consumed by Unity projects via Git URL / UPM.
- There are no signs of network services, databases, or backend infrastructure; the architecture is self-contained and in-process.
12. What to show in an architecture diagram
- A useful diagram would have:
- Game / component code calling into SignalBusUtilities
- SignalBusUtilities delegating to a SignalBus instance
- SignalBus managing a map of signal types to SignalBinding
- SignalBinding holding ordered subscriber callbacks and invoking them on fire
- Tests verifying both the core logic and Unity execution behavior
- The key boundary is between the consumer-facing static API and the internal subscription/dispatch machinery.
Main entry points: SignalBus.cs Signal bus · SignalBusUtilities.cs Bus utils · SignalBus.Runtime.asmdef Runtime asmdef · SignalBusPlayModeTests.cs PlayMode tests
Components 10
Package
Unity package metadata and assembly boundaries
- Package file · Unity package metadata and dependency boundary
package.json - Runtime asmdef · Runtime assembly boundary for the package
Runtime/SignalBus.Runtime.asmdef
Runtime
Public API and core bus implementation
- Bus utils · Static entry point for declaring, subscribing, unsubscribing, and firing signals
Runtime/SignalBusUtilities.cs - Signal bus · Owns signal declarations and routes signal delivery
Runtime/SignalBus.cs - Bindings · Per-signal subscriber collection and ordered invocation
Runtime/SignalBinding.cs
Tests
Edit mode and play mode verification
- EditMode asmdef · Assembly boundary for edit mode tests
Tests/EditMode/SignalBus.Tests.EditMode.asmdef - EditMode tests · Deterministic tests for core bus behavior
Tests/EditMode/SignalBusEditModeTests.cs - PlayMode asmdef · Assembly boundary for play mode tests
Tests/PlayMode/SignalBus.Tests.PlayMode.asmdef - PlayMode tests · Unity runtime behavior tests for the bus
Tests/PlayMode/SignalBusPlayModeTests.cs
Other components
- Game code · Unity components and plain C# callers using the package
Connections 15
- Game code → Bus utils: uses · Consumer code calls the static façade
- Bus utils → Signal bus: delegates to · Facade routes API calls to the bus instance
- Signal bus → Bindings: manages · Bus creates and looks up per-signal bindings
- Signal bus → Bus utils: exposes · Public API is the primary consumer-facing boundary
- Bindings → Signal bus: reports to · Binding is the internal dispatch mechanism used by the bus
- Signal bus → Game code: fires to · Signals are delivered back to subscribed callers
- Package file → Runtime asmdef: packages · Package metadata defines the runtime assembly module
- Runtime asmdef → Bus utils: includes · Runtime assembly contains the public façade
- Runtime asmdef → Signal bus: includes · Runtime assembly contains the core bus
- Runtime asmdef → Bindings: includes · Runtime assembly contains the binding layer
- EditMode asmdef → EditMode tests: includes · Edit mode assembly contains its tests
- PlayMode asmdef → PlayMode tests: includes · Play mode assembly contains its tests
- EditMode tests → Signal bus: verifies · Edit mode tests cover core bus logic
- PlayMode tests → Bus utils: verifies · Play mode tests validate runtime-facing behavior
- PlayMode tests → Signal bus: verifies · Play mode tests validate dispatch in Unity runtime