Use singleton in other places as well

This commit is contained in:
Deluan 2021-06-21 18:41:11 -04:00
parent 1f997357a9
commit 743e469795
7 changed files with 39 additions and 104 deletions

View file

@ -5,14 +5,14 @@ package cmd
import (
"sync"
"github.com/navidrome/navidrome/server/events"
"github.com/google/wire"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/agents/lastfm"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/scanner"
"github.com/navidrome/navidrome/scheduler"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/server/nativeapi"
"github.com/navidrome/navidrome/server/subsonic"
)
@ -23,7 +23,7 @@ var allProviders = wire.NewSet(
nativeapi.New,
persistence.New,
lastfm.NewRouter,
GetBroker,
events.GetBroker,
)
func CreateServer(musicFolder string) *server.Server {
@ -71,41 +71,3 @@ func createScanner() scanner.Scanner {
scanner.New,
))
}
// Broker must be a Singleton
var (
onceBroker sync.Once
brokerInstance events.Broker
)
func GetBroker() events.Broker {
onceBroker.Do(func() {
brokerInstance = createBroker()
})
return brokerInstance
}
func createBroker() events.Broker {
panic(wire.Build(
events.NewBroker,
))
}
// Scheduler must be a Singleton
var (
onceScheduler sync.Once
schedulerInstance scheduler.Scheduler
)
func GetScheduler() scheduler.Scheduler {
onceScheduler.Do(func() {
schedulerInstance = createScheduler()
})
return schedulerInstance
}
func createScheduler() scheduler.Scheduler {
panic(wire.Build(
scheduler.New,
))
}