Interface IHandle<TMessage>
Marks a class as a typed subscriber in the publish/subscribe event framework.
public interface IHandle<TMessage> where TMessage : IMessage
Type Parameters
| Name | Type | Description |
|---|---|---|
TMessage | The specific message type this subscriber handles. |
Examples
// Subscribe a MonoBehaviour to battery level updates:
public class BatteryIndicator : MonoBehaviour, IHandle
{
private void OnEnable()
{
UnityServiceLocator.Current.TryGet(out var aggregator);
aggregator?.Subscribe(this);
}
private void OnDisable()
{
UnityServiceLocator.Current.TryGet(out var aggregator);
aggregator?.Unsubscribe(this);
}
public void Handle(BatteryMessage msg) => Debug.Log($"Battery: {msg.level}");
}
Remarks
The generic parameter binds each implementation to exactly one message type at compile time. This is intentional: strong typing lets the IEventAggregator route messages without reflection on every dispatch, and it prevents a subscriber from accidentally receiving messages it was not written to handle.
A class that handles multiple message types simply implements the interface multiple times with
different type arguments (e.g., IHandle, IHandle).
The aggregator's Subscribe(object) overload discovers all of them in one call via reflection.
Methods
Handle(TMessage)
Processes the received message and applies its payload to this subscriber's state.
void Handle(TMessage message)
Parameters
| Name | Type | Description |
|---|---|---|
message | TMessage | The message payload delivered by the aggregator. |
Remarks
Called synchronously by ChannelKey) — keep implementations fast. Any heavy work (file I/O, complex calculations) should be deferred to a queue or coroutine rather than blocking the publish call.