Documentation
¶
Overview ¶
Package observer aims to simplify the problem of channel-based broadcasting of events from one or more publishers to one or more observers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Property ¶
type Property[T any] interface { // Value returns the current value for this property. Value() T // Update sets a new value for this property. Update(value T) // Observe returns a newly created Stream for this property. Observe() Stream[T] }
Property is an object that is continuously updated by one or more publishers. It is completely goroutine safe: you can use Property concurrently from multiple goroutines.
func NewProperty ¶
NewProperty creates a new Property with the initial value value. It returns the created Property.
type Stream ¶
type Stream[T any] interface { // Value returns the current value for this stream. Value() T // Changes returns the channel that is closed when a new value is available. Changes() chan struct{} // Next advances this stream to the next state. // You should never call this unless Changes channel is closed. Next() T // HasNext checks whether there is a new value available. HasNext() bool // WaitNext waits for Changes to be closed, advances the stream and returns // the current value. WaitNext() T // WaitNextCtx does the same as WaitNext but returns earlier with an error if the given context is cancelled first. WaitNextCtx(ctx context.Context) (T, error) // Clone creates a new independent stream from this one but sharing the same // Property. Updates to the property will be reflected in both streams but // they may have different values depending on when they advance the stream // with Next. Clone() Stream[T] // Peek return the value in the next state // You should never call this unless Changes channel is closed. Peek() T }
Stream represents the list of values a property is updated to. For every property update, that value is appended to the list in the order they happen. The value is discarded once you advance the stream. Please note that Stream is not goroutine safe: you cannot use the same stream on multiple goroutines concurrently. If you want to use multiple streams for the same property, either use Property.Observe (goroutine-safe) or use Stream.Clone (before passing it to another goroutine).