eventmux

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package eventmux provides an in-memory event multiplexer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func None

func None[Event any](_ context.Context, _ Event) error

None is an event handler that does nothing.

If you have a service that supports listeners, inject None by default during construction, and you can stop checking for `nil` before notifying them.

Example:

// Construction.
h.listener = eventmux.None[MyEvent]

// Usage (no need to check if `listener` is nil).
h.listener(ctx, e)

Types

type ContextMiddleware

type ContextMiddleware func(ctx context.Context) context.Context

ContextMiddleware is used to transform the context provided to a function.

It exists as an optimization over ObserverMiddleware whenever the value or type of the event is irrelevant, which supports reuse without needlessly depending on the instantiation of generics.

type Mux

type Mux[Event any] struct {
	// contains filtered or unexported fields
}

Mux is a thread-safe in-memory event multiplexer.

func New

func New[Event any]() *Mux[Event]

New creates a Mux.

func (*Mux[Event]) Observe

func (m *Mux[Event]) Observe(ctx context.Context, event Event) error

Observe and propagate an event to registered observers.

func (*Mux[Event]) Shutdown

func (m *Mux[Event]) Shutdown(wg *sync.WaitGroup)

Shutdown the Mux and communicate finishing via the sync.WaitGroup.

func (*Mux[Event]) WillNotify

func (m *Mux[Event]) WillNotify(
	observers ...Observer[Event],
) *Mux[Event]

WillNotify registers an observer. All events observed by Mux will be propagated to all registered observers.

func (*Mux[Event]) WithContextMiddleware

func (m *Mux[Event]) WithContextMiddleware(
	middleware ...ContextMiddleware,
) *Mux[Event]

WithContextMiddleware registers context middleware.

Context middleware will always be applied before observer middleware.

func (*Mux[Event]) WithObserverMiddleware

func (m *Mux[Event]) WithObserverMiddleware(
	middleware ...ObserverMiddleware[Event],
) *Mux[Event]

WithObserverMiddleware registers observer middleware.

Context middleware will always be applied before observer middleware.

type Observer

type Observer[Event any] func(ctx context.Context, e Event) error

Observer is a function that can process a specific event type.

The correct handling of errors depends on what the observer is:

  • Event brokers, such as Mux, must indicate whether the propagation of the event succeeded, regardless of whether the observers succeeded.

  • Middleware must propagate the errors they receive unless the purpose of that middleware is to modify the error.

  • Asynchronous operations must return whether they succeeded. Brokers may depend on this to retry failed operations.

type ObserverMiddleware

type ObserverMiddleware[Event any] func(next Observer[Event]) Observer[Event]

ObserverMiddleware can be used to run arbitrary actions before and after the completion of next.

Note that there are two different ways in which we can use this within a Mux:

  • With Mux.WithObserverMiddleware, which will apply it to all observers.
  • By wrapping the observer before passing it to Mux.WillNotify, which will only apply to this particular observer.

While more involved, the latter approach is more powerful since it allows handling based on both the event and the observer. For example, logging middleware could attach an observer name or ID to each log, making them easier to understand.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL