Files
goyomi/internal/registry/registry.go
T
2026-05-11 06:48:23 +00:00

44 lines
910 B
Go
Executable File

package registry
import (
"fmt"
"sort"
"sync"
"goyomi/internal/source"
)
var (
mu sync.RWMutex
sources = map[int64]source.CatalogueSource{}
)
// Register adds a source. Panics on duplicate ID — caught at startup.
func Register(s source.CatalogueSource) {
mu.Lock()
defer mu.Unlock()
if _, exists := sources[s.ID()]; exists {
panic(fmt.Sprintf("registry: duplicate source ID %d (%s/%s)", s.ID(), s.Name(), s.Lang()))
}
sources[s.ID()] = s
}
func Get(id int64) (source.CatalogueSource, bool) {
mu.RLock()
defer mu.RUnlock()
s, ok := sources[id]
return s, ok
}
// All returns all registered sources sorted by ID.
func All() []source.CatalogueSource {
mu.RLock()
defer mu.RUnlock()
out := make([]source.CatalogueSource, 0, len(sources))
for _, s := range sources {
out = append(out, s)
}
sort.Slice(out, func(i, j int) bool { return out[i].ID() < out[j].ID() })
return out
}