Documentation
¶
Index ¶
- type AsyncSignal
- func (s *AsyncSignal[T]) AddListener(listener SignalListener[T], key ...string) int
- func (s *AsyncSignal[T]) Emit(ctx context.Context, payload T)
- func (s *AsyncSignal[T]) IsEmpty() bool
- func (s *AsyncSignal[T]) Len() int
- func (s *AsyncSignal[T]) RemoveListener(key string) int
- func (s *AsyncSignal[T]) Reset()
- type BaseSignal
- func (s *BaseSignal[T]) AddListener(listener SignalListener[T], key ...string) int
- func (s *BaseSignal[T]) AddListenerWithErr(listener SignalListenerErr[T], key ...string) int
- func (s *BaseSignal[T]) Emit(ctx context.Context, payload T)
- func (s *BaseSignal[T]) IsEmpty() bool
- func (s *BaseSignal[T]) Len() int
- func (s *BaseSignal[T]) RemoveListener(key string) int
- func (s *BaseSignal[T]) Reset()
- type Signal
- type SignalListener
- type SignalListenerErr
- type SignalOptions
- type SyncSignal
- func (s *SyncSignal[T]) AddListener(listener SignalListener[T], key ...string) int
- func (s *SyncSignal[T]) AddListenerWithErr(listener SignalListenerErr[T], key ...string) int
- func (s *SyncSignal[T]) Emit(ctx context.Context, payload T)
- func (s *SyncSignal[T]) IsEmpty() bool
- func (s *SyncSignal[T]) Len() int
- func (s *SyncSignal[T]) RemoveListener(key string) int
- func (s *SyncSignal[T]) Reset()
- func (s *SyncSignal[T]) TryEmit(ctx context.Context, payload T) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AsyncSignal ¶ added in v1.1.0
type AsyncSignal[T any] struct { // contains filtered or unexported fields }
AsyncSignal is a struct that implements the Signal interface. This is the default implementation. It provides the same functionality as the SyncSignal but the listeners are called in a separate goroutine. This means that all listeners are called asynchronously. However, the method waits for all the listeners to finish before returning. If you don't want to wait for the listeners to finish, you can call the Emit method in a separate goroutine.
func New ¶
func New[T any]() *AsyncSignal[T]
New creates a new signal that can be used to emit and listen to events asynchronously.
Example:
signal := signals.New[int]()
signal.AddListener(func(ctx context.Context, payload int) {
// Listener implementation
// ...
})
signal.Emit(context.Background(), 42)
func (*AsyncSignal[T]) AddListener ¶ added in v1.3.0
func (s *AsyncSignal[T]) AddListener(listener SignalListener[T], key ...string) int
AddListener adds a listener to the signal. Promoted from baseSignal.
func (*AsyncSignal[T]) Emit ¶ added in v1.1.0
func (s *AsyncSignal[T]) Emit(ctx context.Context, payload T)
func (*AsyncSignal[T]) IsEmpty ¶ added in v1.3.0
func (s *AsyncSignal[T]) IsEmpty() bool
IsEmpty checks if the signal has any subscribers. Promoted from baseSignal.
func (*AsyncSignal[T]) Len ¶ added in v1.3.0
func (s *AsyncSignal[T]) Len() int
Len returns the number of listeners. Promoted from baseSignal.
func (*AsyncSignal[T]) RemoveListener ¶ added in v1.3.0
func (s *AsyncSignal[T]) RemoveListener(key string) int
RemoveListener removes a listener from the signal. Promoted from baseSignal.
func (*AsyncSignal[T]) Reset ¶ added in v1.3.0
func (s *AsyncSignal[T]) Reset()
Reset resets the signal. Promoted from baseSignal.
type BaseSignal ¶ added in v1.1.0
type BaseSignal[T any] struct { // contains filtered or unexported fields }
BaseSignal provides the base implementation of the Signal interface. It is intended to be used as an abstract base for underlying signal mechanisms.
Example:
type MyDerivedSignal[T any] struct {
BaseSignal[T]
// Additional fields or methods specific to MyDerivedSignal
}
func (s *MyDerivedSignal[T]) Emit(ctx context.Context, payload T) {
// Custom implementation for emitting the signal
}
func NewBaseSignal ¶ added in v1.3.0
func NewBaseSignal[T any](opts *SignalOptions) *BaseSignal[T]
NewBaseSignal creates a BaseSignal with optional allocation/growth options.
func (*BaseSignal[T]) AddListener ¶ added in v1.1.0
func (s *BaseSignal[T]) AddListener(listener SignalListener[T], key ...string) int
AddListener adds a listener to the signal. The listener will be called whenever the signal is emitted. It returns the number of subscribers after the listener was added. It accepts an optional key that can be used to remove the listener later or to check if the listener was already added. It returns -1 if the listener with the same key was already added to the signal.
Example:
signal := signals.New[int]()
count := signal.AddListener(func(ctx context.Context, payload int) {
// Listener implementation
// ...
}, "key1")
fmt.Println("Number of subscribers after adding listener:", count)
func (*BaseSignal[T]) AddListenerWithErr ¶ added in v1.3.0
func (s *BaseSignal[T]) AddListenerWithErr(listener SignalListenerErr[T], key ...string) int
AddListenerErr adds an error-returning listener. It behaves like AddListener but the listener may return an error. If a key is provided and already exists, it returns -1.
func (*BaseSignal[T]) Emit ¶ added in v1.1.0
func (s *BaseSignal[T]) Emit(ctx context.Context, payload T)
Emit is not implemented in BaseSignal and panics if called. It should be implemented by a derived type.
Example:
type MyDerivedSignal[T any] struct {
BaseSignal[T]
// Additional fields or methods specific to MyDerivedSignal
}
func (s *MyDerivedSignal[T]) Emit(ctx context.Context, payload T) {
// Custom implementation for emitting the signal
}
func (*BaseSignal[T]) IsEmpty ¶ added in v1.1.0
func (s *BaseSignal[T]) IsEmpty() bool
func (*BaseSignal[T]) Len ¶ added in v1.1.0
func (s *BaseSignal[T]) Len() int
func (*BaseSignal[T]) RemoveListener ¶ added in v1.1.0
func (s *BaseSignal[T]) RemoveListener(key string) int
RemoveListener removes a listener from the signal. It returns the number of subscribers after the listener was removed. It returns -1 if the listener was not found.
Example:
signal := signals.New[int]()
signal.AddListener(func(ctx context.Context, payload int) {
// Listener implementation
// ...
}, "key1")
count := signal.RemoveListener("key1")
fmt.Println("Number of subscribers after removing listener:", count)
func (*BaseSignal[T]) Reset ¶ added in v1.1.0
func (s *BaseSignal[T]) Reset()
Reset resets the signal by removing all subscribers from the signal, effectively clearing the list of subscribers. This can be used when you want to stop all listeners from receiving further signals.
Example:
signal := signals.New[int]()
signal.AddListener(func(ctx context.Context, payload int) {
// Listener implementation
// ...
})
signal.Reset() // Removes all listeners
fmt.Println("Number of subscribers after resetting:", signal.Len())
type Signal ¶
type Signal[T any] interface { // Emit notifies all subscribers of the signal and passes the context and the payload. // // If the context has a deadline or cancellable property, the listeners // must respect it. If the signal is async (default), the listeners are called // in a separate goroutine. // // Example: // signal := signals.New[int]() // signal.AddListener(func(ctx context.Context, payload int) { // // Listener implementation // // ... // }) // signal.Emit(context.Background(), 42) Emit(ctx context.Context, payload T) // AddListener adds a listener to the signal. // // The listener will be called whenever the signal is emitted. It returns the // number of subscribers after the listener was added. It accepts an optional key // that can be used to remove the listener later or to check if the listener // was already added. It returns -1 if the listener with the same key // was already added to the signal. // // Example: // signal := signals.NewSync[int]() // count := signal.AddListener(func(ctx context.Context, payload int) { // // Listener implementation // // ... // }) // fmt.Println("Number of subscribers after adding listener:", count) AddListener(handler SignalListener[T], key ...string) int // RemoveListener removes a listener from the signal. // // It returns the number of subscribers after the listener was removed. // It returns -1 if the listener was not found. // // Example: // signal := signals.NewSync[int]() // signal.AddListener(func(ctx context.Context, payload int) { // // Listener implementation // // ... // }, "key1") // count := signal.RemoveListener("key1") // fmt.Println("Number of subscribers after removing listener:", count) RemoveListener(key string) int // Reset resets the signal by removing all subscribers from the signal, // effectively clearing the list of subscribers. // // This can be used when you want to stop all listeners from receiving // further signals. // // Example: // signal := signals.New[int]() // signal.AddListener(func(ctx context.Context, payload int) { // // Listener implementation // // ... // }) // signal.Reset() // Removes all listeners // fmt.Println("Number of subscribers after resetting:", signal.Len()) Reset() // Len returns the number of listeners subscribed to the signal. // // This can be used to check how many listeners are currently waiting for a signal. // The returned value is of type int. // // Example: // signal := signals.NewSync[int]() // signal.AddListener(func(ctx context.Context, payload int) { // // Listener implementation // // ... // }) // fmt.Println("Number of subscribers:", signal.Len()) Len() int // IsEmpty checks if the signal has any subscribers. // // It returns true if the signal has no subscribers, and false otherwise. // This can be used to check if there are any listeners before emitting a signal. // // Example: // signal := signals.New[int]() // fmt.Println("Is signal empty?", signal.IsEmpty()) // Should print true // signal.AddListener(func(ctx context.Context, payload int) { // // Listener implementation // // ... // }) // fmt.Println("Is signal empty?", signal.IsEmpty()) // Should print false IsEmpty() bool }
Signal is the interface that represents a signal that can be subscribed to emitting a payload of type T.
func NewWithOptions ¶ added in v1.3.0
func NewWithOptions[T any](opts *SignalOptions) Signal[T]
NewWithOptions creates a new async Signal with custom allocation/growth options.
type SignalListener ¶
SignalListener is a type definition for a function that will act as a listener for signals. This function takes two parameters:
- A context of type `context.Context`. This is typically used for timeout and cancellation signals, and can carry request-scoped values across API boundaries and between processes.
- A payload of generic type `T`. This can be any type, and represents the data or signal that the listener function will process.
The function does not return any value.
type SignalListenerErr ¶ added in v1.3.0
SignalListenerErr is a type definition for a function that will act as an error-returning listener for signals. This function takes two parameters:
- A context of type `context.Context`. This is typically used for timeout and cancellation signals, and can carry request-scoped values across API boundaries and between processes.
- A payload of generic type `T`. This can be any type, and represents the data or signal that the listener function will process.
The function returns an error value, which can be used to indicate if the listener encountered any issues while processing the signal.
type SignalOptions ¶ added in v1.3.0
SignalOptions allows advanced users to configure allocation and growth strategy for listeners.
type SyncSignal ¶ added in v1.1.0
type SyncSignal[T any] struct { // contains filtered or unexported fields }
SyncSignal is a struct that implements the Signal interface. It provides a synchronous way of notifying all subscribers of a signal. The type parameter `T` is a placeholder for any type.
func NewSync ¶ added in v1.1.0
func NewSync[T any]() *SyncSignal[T]
NewSync creates a new signal that can be used to emit and listen to events synchronously.
Example:
signal := signals.NewSync[int]()
signal.AddListener(func(ctx context.Context, payload int) {
// Listener implementation
// ...
})
signal.Emit(context.Background(), 42)
func (*SyncSignal[T]) AddListener ¶ added in v1.3.0
func (s *SyncSignal[T]) AddListener(listener SignalListener[T], key ...string) int
AddListener adds a listener to the signal. Promoted from baseSignal.
func (*SyncSignal[T]) AddListenerWithErr ¶ added in v1.3.0
func (s *SyncSignal[T]) AddListenerWithErr(listener SignalListenerErr[T], key ...string) int
AddListenerWithErr adds an error-returning listener. Promoted from baseSignal.
func (*SyncSignal[T]) Emit ¶ added in v1.1.0
func (s *SyncSignal[T]) Emit(ctx context.Context, payload T)
Emit notifies all subscribers of the signal and passes the payload in a synchronous way.
func (*SyncSignal[T]) IsEmpty ¶ added in v1.3.0
func (s *SyncSignal[T]) IsEmpty() bool
IsEmpty checks if the signal has any subscribers. Promoted from baseSignal.
func (*SyncSignal[T]) Len ¶ added in v1.3.0
func (s *SyncSignal[T]) Len() int
Len returns the number of listeners. Promoted from baseSignal.
func (*SyncSignal[T]) RemoveListener ¶ added in v1.3.0
func (s *SyncSignal[T]) RemoveListener(key string) int
RemoveListener removes a listener from the signal. Promoted from baseSignal.
func (*SyncSignal[T]) Reset ¶ added in v1.3.0
func (s *SyncSignal[T]) Reset()
Reset resets the signal. Promoted from baseSignal.
func (*SyncSignal[T]) TryEmit ¶ added in v1.3.0
func (s *SyncSignal[T]) TryEmit(ctx context.Context, payload T) error
TryEmit behaves like Emit but returns an error when the provided context is canceled or when any error-returning listener returns a non-nil error. It stops invoking further listeners as soon as an error or cancellation is observed. If no error occurs, it returns nil.