refactor: implement SendBroadcastMessage method for event broadcasting

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-06-28 12:13:40 -04:00
commit bcf7f14ed9
4 changed files with 14 additions and 9 deletions

View file

@ -74,8 +74,7 @@ func newPlayTracker(ds model.DataStore, broker events.Broker, pluginManager Plug
}
if conf.Server.EnableNowPlaying {
m.OnExpiration(func(_ string, _ NowPlayingInfo) {
ctx := events.BroadcastToAll(context.Background())
broker.SendMessage(ctx, &events.NowPlayingCount{Count: m.Len()})
broker.SendBroadcastMessage(context.Background(), &events.NowPlayingCount{Count: m.Len()})
})
}
@ -195,8 +194,7 @@ func (p *playTracker) NowPlaying(ctx context.Context, playerId string, playerNam
ttl := time.Duration(remaining+5) * time.Second
_ = p.playMap.AddWithTTL(playerId, info, ttl)
if conf.Server.EnableNowPlaying {
ctx = events.BroadcastToAll(ctx)
p.broker.SendMessage(ctx, &events.NowPlayingCount{Count: p.playMap.Len()})
p.broker.SendBroadcastMessage(ctx, &events.NowPlayingCount{Count: p.playMap.Len()})
}
player, _ := request.PlayerFrom(ctx)
if player.ScrobbleEnabled {

View file

@ -216,7 +216,6 @@ func (s *controller) ScanAll(requestCtx context.Context, fullScan bool) ([]strin
// Prepare the context for the scan
ctx := request.AddValues(s.rootCtx, requestCtx)
ctx = events.BroadcastToAll(ctx)
ctx = auth.WithAdminUser(ctx, s.ds)
// Send the initial scan status event
@ -236,7 +235,7 @@ func (s *controller) ScanAll(requestCtx context.Context, fullScan bool) ([]strin
// If changes were detected, send a refresh event to all clients
if s.changesDetected {
log.Debug(ctx, "Library changes imported. Sending refresh event")
s.broker.SendMessage(ctx, &events.RefreshResource{})
s.broker.SendBroadcastMessage(ctx, &events.RefreshResource{})
}
// Send the final scan status event, with totals
if count, folderCount, err := s.getCounters(ctx); err != nil {
@ -315,5 +314,5 @@ func (s *controller) trackProgress(ctx context.Context, progress <-chan *Progres
}
func (s *controller) sendMessage(ctx context.Context, status *events.ScanStatus) {
s.broker.SendMessage(ctx, status)
s.broker.SendBroadcastMessage(ctx, status)
}

View file

@ -13,8 +13,8 @@ type eventCtxKey string
const broadcastToAllKey eventCtxKey = "broadcastToAll"
// BroadcastToAll is a context key that can be used to broadcast an event to all clients
func BroadcastToAll(ctx context.Context) context.Context {
// broadcastToAll is a context key that can be used to broadcast an event to all clients
func broadcastToAll(ctx context.Context) context.Context {
return context.WithValue(ctx, broadcastToAllKey, true)
}

View file

@ -19,6 +19,7 @@ import (
type Broker interface {
http.Handler
SendMessage(ctx context.Context, event Event)
SendBroadcastMessage(ctx context.Context, event Event)
}
const (
@ -77,6 +78,11 @@ func GetBroker() Broker {
})
}
func (b *broker) SendBroadcastMessage(ctx context.Context, evt Event) {
ctx = broadcastToAll(ctx)
b.SendMessage(ctx, evt)
}
func (b *broker) SendMessage(ctx context.Context, evt Event) {
msg := b.prepareMessage(ctx, evt)
log.Trace("Broker received new event", "type", msg.event, "data", msg.data)
@ -280,4 +286,6 @@ type noopBroker struct {
http.Handler
}
func (b noopBroker) SendBroadcastMessage(context.Context, Event) {}
func (noopBroker) SendMessage(context.Context, Event) {}