navidrome/utils/graph.go
2016-03-03 13:14:05 -05:00

29 lines
541 B
Go

package utils
import (
"github.com/karlkfi/inject"
"reflect"
)
var Graph inject.Graph
var (
definitions map[reflect.Type]interface{}
)
func DefineSingleton(ptr interface{}, constructor interface{}, args ...interface{}) {
typ := reflect.TypeOf(ptr)
provider := inject.NewProvider(constructor, args...)
if _, found := definitions[typ]; found {
ptr = definitions[typ]
} else {
definitions[typ] = ptr
}
Graph.Define(ptr, provider)
}
func init() {
definitions = make(map[reflect.Type]interface{})
Graph = inject.NewGraph()
}