Table of Contents

Class EventAggregator

Namespace
BSGameLib.Game.Events
Assembly
BSGameLib.Game.dll

The default event aggregator. Handlers run on the thread that publishes, and a handler that throws is logged while the others still run.

public class EventAggregator : IEventAggregator
Inheritance
EventAggregator
Implements
Inherited Members

Constructors

EventAggregator(ILogger<EventAggregator>)

Created by dependency injection.

public EventAggregator(ILogger<EventAggregator> logger)

Parameters

logger ILogger<EventAggregator>

Properties

Subscriptions

The active subscriptions, as tokens. The list is a copy, so changing it does nothing.

public IReadOnlyCollection<IEventSubscriptionToken> Subscriptions { get; }

Property Value

IReadOnlyCollection<IEventSubscriptionToken>

Methods

Clear()

Removes every subscription, and disposes each token.

public void Clear()

Publish<T>(T)

Calls every handler subscribed to T with message, on this thread.

public void Publish<T>(T message) where T : IEvent

Parameters

message T

Type Parameters

T

Remarks

Handlers run while the aggregator's lock is held, so a handler should not wait on another thread that subscribes or publishes.

Subscribe<T>(Action<T>)

Subscribes a handler to messages of type T. Disposing the returned token unsubscribes it.

public IEventSubscriptionToken Subscribe<T>(Action<T> action) where T : IEvent

Parameters

action Action<T>

Returns

IEventSubscriptionToken

Type Parameters

T

Subscribe<T>(Action<T>, IEventSubscriptionToken)

Subscribes a handler under a token the caller made. Unsubscribe with that token to stop it; disposing the token does not.

public IEventSubscriptionToken Subscribe<T>(Action<T> action, IEventSubscriptionToken subscriptionToken) where T : IEvent

Parameters

action Action<T>
subscriptionToken IEventSubscriptionToken

Returns

IEventSubscriptionToken

Type Parameters

T

Unsubscribe(IEventSubscriptionToken)

Removes the subscription with this token. A token that is not subscribed does nothing.

public void Unsubscribe(IEventSubscriptionToken subscriptionToken)

Parameters

subscriptionToken IEventSubscriptionToken