Remove lists when empty after deleting elements.

This commit is contained in:
Alex Bramley 2011-11-09 21:38:47 +00:00
parent bdb5c9b1a0
commit 177149fd3c
1 changed files with 13 additions and 6 deletions

View File

@ -82,26 +82,31 @@ N: for _, name := range names {
} }
} }
func _del(l *list.List, id HandlerID) { func _del(l *list.List, id HandlerID) bool {
for e := l.Front(); e != nil; e = e.Next() { for e := l.Front(); e != nil; e = e.Next() {
if e.Value.(Handler).Id() == id { if e.Value.(Handler).Id() == id {
l.Remove(e) l.Remove(e)
} }
} }
return l.Len() == 0
} }
func (r *registry) DelHandler(h Handler, names ...string) { func (r *registry) DelHandler(h Handler, names ...string) {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
if len(names) == 0 { if len(names) == 0 {
for _, l := range r.events { for name, l := range r.events {
_del(l, h.Id()) if _del(l, h.Id()) {
r.events[name] = nil, false
}
} }
} else { } else {
for _, name := range names { for _, name := range names {
name = strings.ToLower(name) name = strings.ToLower(name)
if l, ok := r.events[name]; ok { if l, ok := r.events[name]; ok {
_del(l, h.Id()) if _del(l, h.Id()) {
r.events[name] = nil, false
}
} }
} }
} }
@ -112,10 +117,12 @@ func (r *registry) Dispatch(name string, ev ...interface{}) {
} }
func (r *registry) ClearEvents(name string) { func (r *registry) ClearEvents(name string) {
name = strings.ToLower(name)
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
if l, ok := r.events[strings.ToLower(name)]; ok { if l, ok := r.events[name]; ok {
l.Init() l.Init() // I hope this is enough to GC all list elements.
r.events[name] = nil, false
} }
} }