MCPcopy Create free account
hub / github.com/codnect/procyon / RegisterSingleton

Method RegisterSingleton

component/container.go:195–214  ·  view source on GitHub ↗

RegisterSingleton registers a singleton instance with the given name. Returns an error if a singleton with the same name already exists.

(name string, instance any)

Source from the content-addressed store, hash-verified

193// RegisterSingleton registers a singleton instance with the given name.
194// Returns an error if a singleton with the same name already exists.
195func (d *DefaultContainer) RegisterSingleton(name string, instance any) error {
196 if name == "" {
197 return errors.New("empty name")
198 }
199
200 if instance == nil {
201 return errors.New("nil instance")
202 }
203
204 d.muSingletons.Lock()
205 defer d.muSingletons.Unlock()
206
207 if _, exists := d.singletons[name]; exists {
208 return ErrInstanceAlreadyExists
209 }
210
211 d.singletons[name] = instance
212 d.typesOfSingletons[name] = reflect.TypeOf(instance)
213 return nil
214}
215
216// ContainsSingleton checks whether a singleton with the specified name exists.
217func (d *DefaultContainer) ContainsSingleton(name string) bool {

Calls

no outgoing calls