register/unregister tweaks

This commit is contained in:
Josh Deprez 2021-09-02 14:09:53 +10:00
parent d71db51d53
commit 60bbd14487

View file

@ -319,6 +319,15 @@ func (g *Game) Register(component, parent interface{}) error {
} }
func (g *Game) register(component, parent interface{}) error { func (g *Game) register(component, parent interface{}) error {
// register in g.byID if needed. nothing else errors so do this first
if i, ok := component.(Identifier); ok {
id := i.Ident()
if _, exists := g.byID[id]; exists {
return fmt.Errorf("duplicate id %q", id)
}
g.byID[id] = i
}
// register in g.par // register in g.par
if parent != nil { if parent != nil {
g.par[component] = parent g.par[component] = parent
@ -348,17 +357,6 @@ func (g *Game) register(component, parent interface{}) error {
g.byAB[k][component] = struct{}{} g.byAB[k][component] = struct{}{}
} }
} }
// register in g.byID if needed
i, ok := component.(Identifier)
if !ok {
return nil
}
id := i.Ident()
if _, exists := g.byID[id]; exists {
return fmt.Errorf("duplicate id %q", id)
}
g.byID[id] = i
return nil return nil
} }
@ -408,11 +406,9 @@ func (g *Game) unregister(component interface{}) {
} }
// unregister from g.byID if needed // unregister from g.byID if needed
i, ok := component.(Identifier) if i, ok := component.(Identifier); ok {
if !ok {
return
}
delete(g.byID, i.Ident()) delete(g.byID, i.Ident())
}
} }
// --------- Helper stuff --------- // --------- Helper stuff ---------