EventContext

When you subscribe to an EventSource in return you receive a Subscription. You can use that subscription to unsubscribe from the event source or to temporarily pause receiving events. But managing a big number of subscriptions to different event sources can be tricky. EventContext comes to the rescue.

When you subscribe to an EventSource in return you receive a Subscription. You can use that subscription to unsubscribe from the event source or to temporarily pause receiving events. But managing a big number of subscriptions to different event sources can be tricky. EventContext comes to the rescue.

By default, every subscription is registered in a "dummy" EventContext.Global which lives for the lifetime of the whole program and does nothing. But if instead you will create a new EventContext and use it explicitly when subscribing or you will set it as an implicit parameter, taking over EventContext.Global, the subscription will be registered within this new one. It will allow you to manage all registered subscriptions at once and all registered subscriptions will be destroyed when the event context lifetime ends.

Usage of methods in the trait are explained as they are implemented in the default implementation. All operations on an EventContext are synchronized.

See also
Companion
object
class Object
trait Matchable
class Any

Value members

Abstract methods

def destroy(): Unit

Destroys all subscriptions. A destroyed EventContext cannot be used again. This method is called automatically when the event context lifetime ends.

Destroys all subscriptions. A destroyed EventContext cannot be used again. This method is called automatically when the event context lifetime ends.

def isContextDestroyed: Boolean
def isContextStarted: Boolean
def register(subscription: Subscription): Boolean

Registers a new Subscription within the EventContext if the event context is not destroyed. (But it does not have to be started). If the event context is started, the new subscription will be automatically subscribed. If not, it will be subscribed on the consecutive call to start().

Registers a new Subscription within the EventContext if the event context is not destroyed. (But it does not have to be started). If the event context is started, the new subscription will be automatically subscribed. If not, it will be subscribed on the consecutive call to start().

Value Params
subscription

The subscription to be registered

Returns

true if the subscription is registered, false otherwise (e.g. if the event context is destroyed or the subscription is already registered)

def start(): Unit

AnEventContext has to be started before it can register subscriptions. A newly created one is started by default. If the event context maintains subscriptions, they will be re-subscribed.

AnEventContext has to be started before it can register subscriptions. A newly created one is started by default. If the event context maintains subscriptions, they will be re-subscribed.

def stop(): Unit

Unsubscribes all subscriptions and prevents registering new ones before the event context is started again. The subscriptions are not destroyed and will be re-subscribed when the consecutive start() is called.

Unsubscribes all subscriptions and prevents registering new ones before the event context is started again. The subscriptions are not destroyed and will be re-subscribed when the consecutive start() is called.

def unregister(subscription: Subscription): Unit

Unregisters an already registered Subscription. The subscription is not unsubscribed or destroyed. Does nothing if the EventContext does not contain the given subscription.

Unregisters an already registered Subscription. The subscription is not unsubscribed or destroyed. Does nothing if the EventContext does not contain the given subscription.

Value Params
subscription

The subscription to be unregistered