Documentation
¶
Index ¶
Constants ¶
const (
// ChanNotFound error message returned when a channel is not found for a key
ChanNotFound = "no channel found for key"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChanCaster ¶
type ChanCaster[K comparable, T any] struct { // contains filtered or unexported fields }
ChanCaster is a generic struct that manages concurrent channels associated with keys. It uses an errgroup to manage the execution of functions associated with each channel. K: The type of key used to access channels. T: The type of data that will be sent and received on the channels.
func New ¶
func New[K comparable, T any](ctx context.Context) *ChanCaster[K, T]
New creates a new ChanCasters instance with the provided context. The context will be used to cancel the execution of functions within the errgroup. The returned errgroup context will be canceled the first time a function returns a non-nil error or the first time Wait returns, whichever occurs first.
func (*ChanCaster[K, T]) Add ¶
func (c *ChanCaster[K, T]) Add(key K, fn ChanFunc) error
Add creates a new channel associated with the provided key and runs the provided function concurrently. The function should not take any arguments and must return an error if there is one. If the context is already canceled, an error is returned.
func (*ChanCaster[K, T]) All ¶ added in v0.1.2
func (c *ChanCaster[K, T]) All() []K
All returns a slice containing all keys associated with channels in the ChanCaster instance.
func (*ChanCaster[K, T]) Close ¶
func (c *ChanCaster[K, T]) Close(key K) error
Close closes the channel associated with the provided key and removes it from the map. If the key is not found, a CHAN_NOT_FOUND error is returned.
func (*ChanCaster[K, T]) Get ¶
func (c *ChanCaster[K, T]) Get(key K) (chan T, error)
Get retrieves the channel associated with the provided key. If the context is already canceled, an error is returned. If the key is not found, a nil channel and CHAN_NOT_FOUND error are returned.
func (*ChanCaster[K, T]) Publish ¶ added in v0.1.1
func (c *ChanCaster[K, T]) Publish(key K, data T) error
Publish sends the provided data to the channel associated with the given key.
If no channel is found for the specified key, a ChanNotFound error is returned.
func (*ChanCaster[K, T]) Wait ¶
func (c *ChanCaster[K, T]) Wait() error
Wait blocks until all functions added with Add() have finished execution. It returns the first non-nil error (if any) from the functions.