Documentation ¶
Overview ¶
Package event is an self-organizing event system, where an event can generate more events
SPDX-License-Identifier: Apache-2.0
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type None ¶
type None struct{}
None is an empty struct, for cases where receivers do not need any data to process
type Receiver ¶
type Receiver[T any] interface { Process(T) T }
Receiving and returning the same type allows T to be a value type, or to contain value types, no need for pointers. If the receiver can have an error, then T needs to contain an error, and the semantics of error handling are defined by the receivers.
Im particular, if: - T contains an error - there are multiple receivers - a receiver before the last receiver has an error Then ideally the remaining receivers should do nothing, so that the original error is reported, to make debugging easier.
Notes: - Since the Process method accepts and returns the same type, a receiver can be created by composing functions. - The None type is provided for cases where the receivers do not need any data.
type ReceiverFunc ¶
type ReceiverFunc[T any] func(T) T
ReceiverFunc is an adapter to allow the user of ordinary functions as Receivers
func (ReceiverFunc[T]) Process ¶
func (r ReceiverFunc[T]) Process(t T) T
Process is the Receiver implementation
type Registry ¶
type Registry[I constraint.Ordered, T any] struct { // contains filtered or unexported fields }
Registry is an event registry of event receivers. A receiver may also be a sender. There can be any number of receivers for a particular operation, executed in the order they are registered. I is the type of id used for operations, it is only used for ordering the calls to receivers. Each operation must have a unique id.
The senders and receivers do not provide or receive the id, only the event type T. T is the type of event.
The zero value is ready to use.
func (*Registry[I, T]) Register ¶
Register a receiver. Receivers can be registered at any time, typically they are registered during initialization. The same receiver can be registered multiple times. It can be particularly useful to add a debugging receiver after each normal receiver, to debug the data processing after each step.
func (*Registry[I, T]) Remove ¶
Remove a receiver from a specific id. Removes only the first occurrence, unless the optional all flag is true.
func (*Registry[I, T]) RemoveId ¶
func (r *Registry[I, T]) RemoveId(id I)
Remove a given id and all receivers associated with it.
func (*Registry[I, T]) Send ¶
func (r *Registry[I, T]) Send(val T) T
Send an event to any receivers of the specified operation. If there are no receivers, then the call is a no operation.
T may be cumulative, where each receiver can add to it, or T can just be the result of the final operation. It is up to the user to decide on the semantics of T. If there are no receivers, the T value passed will be returned.
See comments on Receiver interface regarding the None type and error handling.