fermentord/internal/configuration/observer.go

36 lines
642 B
Go

package configuration
import (
"log"
"github.com/fsnotify/fsnotify"
)
type ChangeNotifier struct {
channels []chan<- Configuration
}
func NewChangeNotifier() *ChangeNotifier {
return &ChangeNotifier{
channels: make([]chan<- Configuration, 0),
}
}
func (p *ChangeNotifier) Subscribe(ch chan<- Configuration) {
p.channels = append(p.channels, ch)
}
func (p *ChangeNotifier) Notify(config Configuration) {
for _, ch := range p.channels {
select {
case ch <- config:
default:
}
}
}
func (p *ChangeNotifier) OnConfigChange(in fsnotify.Event) {
log.Print("Reloading configuration")
config := Global()
p.Notify(config)
}