59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
type Notifier[TNotification any] struct {
|
|
bufferSize int
|
|
|
|
Listeners map[string]chan TNotification
|
|
}
|
|
|
|
func (n *Notifier[TNotification]) Create(id string) error {
|
|
if _, exists := n.Listeners[id]; exists {
|
|
return errors.New("This listener already exists")
|
|
}
|
|
|
|
n.Listeners[id] = make(chan TNotification, n.bufferSize)
|
|
|
|
return nil
|
|
}
|
|
|
|
var ChannelFullErr = errors.New("Channel is full")
|
|
|
|
// Ensures the listener exists before sending
|
|
func (n *Notifier[TNotification]) SendAndCreate(id string, notification TNotification) error {
|
|
if _, exists := n.Listeners[id]; !exists {
|
|
if err := n.Create(id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
ch := n.Listeners[id]
|
|
|
|
select {
|
|
case ch <- notification:
|
|
return nil
|
|
default:
|
|
return ChannelFullErr
|
|
}
|
|
}
|
|
|
|
func (n *Notifier[TNotification]) Delete(id string) error {
|
|
if _, exists := n.Listeners[id]; !exists {
|
|
return errors.New("This listener does not exists")
|
|
}
|
|
|
|
delete(n.Listeners, id)
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewNotifier[TNotification any](bufferSize int) Notifier[TNotification] {
|
|
return Notifier[TNotification]{
|
|
bufferSize: bufferSize,
|
|
Listeners: make(map[string]chan TNotification),
|
|
}
|
|
}
|