mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 21:17:37 +03:00
31 lines
540 B
Go
31 lines
540 B
Go
package utils
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/karlkfi/inject"
|
|
)
|
|
|
|
var Graph inject.Graph
|
|
|
|
var (
|
|
definitions map[reflect.Type]interface{}
|
|
)
|
|
|
|
func DefineSingleton(ptr interface{}, constructor interface{}) interface{} {
|
|
typ := reflect.TypeOf(ptr)
|
|
provider := inject.NewAutoProvider(constructor)
|
|
|
|
if _, found := definitions[typ]; found {
|
|
ptr = definitions[typ]
|
|
} else {
|
|
definitions[typ] = ptr
|
|
}
|
|
Graph.Define(ptr, provider)
|
|
return ptr
|
|
}
|
|
|
|
func init() {
|
|
definitions = make(map[reflect.Type]interface{})
|
|
Graph = inject.NewGraph()
|
|
}
|