A Handler that allows subscribers (any caller) to subscribe to a specific event

All subscribers will be notified if the EventHandler dispatched the event.

Example

At first the EventHandler has to be created with a unique name that subscribes the purpose.

  • the generic can be of any type / interface
  • to create a handler which only handles "notification" (without payload) the generic has to be <void>
  • the unique name gets relevant when combining EventHandlers with EventCombiners
const StringEventHandler = new EventHandler<string>('StringEventHandler');

To receive messages from the handler, a subscription has to be made with a unique name.

  • the name of the subscription is relevant when handling multiple subscribers an referencing when unsubscribing
  • the event which is provided in the callback is a DomainEvent with the dispatched payload (typed)
StringEventHandler.subscribe('StringEventLogger', (event) => {
console.log(event.payload);
});

To send messages a dispatch with the correctly typed value has to be made.

StringEventHandler.dispatch('my payload'); // StringEventLogger logs 'my payload'

When a Subscription is unsubscribed, the callback will no longer be called

StringEventHandler.unsubscribe('StringEventLogger');
StringEventHandler.dispatch('another payload'); // StringEventLogger is no longer notified

Logging

To use the logging functionality the handler has to be extended to activate the following logging methods:

  • protected logSubscribe(name: string) - log when .subscribe() is called
  • protected logUnsubscribe(name: string) - log when .unsubscribe() is called
  • protected logDispatch() - log when dispatch() is called
  • protected logDispatchedSubscription(subscription: Subscription<Payload>) - log when a subscription is notified

Type Parameters

  • Payload

Hierarchy

  • EventHandler

Accessors

  • get name(): string
  • 💬 The name of this handler which is used at a possible EventCombiner

    Returns string

Methods

  • 💬 Dispatches a DomainEvent.

    Therefore, all Subscribers callbacks are called with the given optional payload.

    Parameters

    • payload: Payload

      (optional) payload to broadcast to all Subscribers

    Returns void

  • 💬 log when dispatch() is called

    • extend this class to implement this function in order to inject logging

    Returns void

  • 💬 log when a subscription is notified.

    • provides the subscription itself (all parameters from subscribe())
    • extend this class to implement this function in order to inject logging

    Parameters

    • subscription: Subscription<Payload>

    Returns void

  • 💬 log when .subscribe() is called.

    • provides the name of the subscription
    • extend this class to implement this function in order to inject logging

    Parameters

    • name: string

    Returns void

  • 💬 log when .unsubscribe() is called.

    • provides the name of the subscription
    • extend this class to implement this function in order to inject logging

    Parameters

    • name: string

    Returns void

  • 💬 Subscribes the given callback to this handler, so that its called on dispatch.

    Parameters

    • name: string

      of the calling Subscriber

    • callback: ((event: DomainEvent<Payload>) => void)

      to call if the event has been dispatched

    • suppressLogging: boolean = false

      suppresses the usage of the extendable logging functionality of this handler

    Returns void

  • 💬 Unsubscribes a previously subscribed event with the same name

    Parameters

    • name: string

      of the Subscriber that has to match the name, the subscription was made with

    Returns void

Generated using TypeDoc