Return the list element and the remover from the internal handlerSet/commandList methods.

This makes the remove method useful. Before it was only useful inside the Remove closures.
This commit is contained in:
Chris Rhodes 2013-02-17 22:02:02 -08:00
parent c86a9a257d
commit 6fc99107e6
2 changed files with 17 additions and 14 deletions

View File

@ -46,7 +46,7 @@ func newHandlerSet() *handlerSet {
return &handlerSet{set: make(map[string]*list.List)}
}
func (hs *handlerSet) add(event string, handler Handler) Remover {
func (hs *handlerSet) add(event string, handler Handler) (*list.Element, Remover) {
hs.Lock()
defer hs.Unlock()
event = strings.ToLower(event)
@ -56,7 +56,7 @@ func (hs *handlerSet) add(event string, handler Handler) Remover {
hs.set[event] = l
}
element := l.PushBack(&handlerElement{event, handler})
return RemoverFunc(func() {
return element, RemoverFunc(func() {
hs.remove(element)
})
}
@ -106,7 +106,7 @@ func newCommandList() *commandList {
return &commandList{list: list.New()}
}
func (cl *commandList) add(regex string, handler Handler, priority int) Remover {
func (cl *commandList) add(regex string, handler Handler, priority int) (element *list.Element, remover Remover) {
cl.Lock()
defer cl.Unlock()
c := &commandElement{
@ -119,13 +119,14 @@ func (cl *commandList) add(regex string, handler Handler, priority int) Remover
c := e.Value.(*commandElement)
if c.regex == regex {
logging.Error("Command prefix '%s' already registered.", regex)
return nil
return
}
}
element := cl.list.PushBack(c)
return RemoverFunc(func() {
element = cl.list.PushBack(c)
remover = RemoverFunc(func() {
cl.remove(element)
})
return
}
func (cl *commandList) remove(element *list.Element) {
@ -161,7 +162,8 @@ func (cl *commandList) match(text string) (handler Handler) {
// strings of digits like "332" (mainly because I really didn't feel like
// putting massive constant tables in).
func (conn *Conn) Handle(name string, handler Handler) Remover {
return conn.handlers.add(name, handler)
_, remover := conn.handlers.add(name, handler)
return remover
}
func (conn *Conn) HandleFunc(name string, handlerFunc HandlerFunc) Remover {
@ -169,7 +171,8 @@ func (conn *Conn) HandleFunc(name string, handlerFunc HandlerFunc) Remover {
}
func (conn *Conn) Command(regex string, handler Handler, priority int) Remover {
return conn.commands.add(regex, handler, priority)
_, remover := conn.commands.add(regex, handler, priority)
return remover
}
func (conn *Conn) CommandFunc(regex string, handlerFunc HandlerFunc, priority int) Remover {

View File

@ -17,7 +17,7 @@ func TestHandlerSet(t *testing.T) {
}
// Add one
hn1 := hs.add("ONE", HandlerFunc(f))
_, hn1 := hs.add("ONE", HandlerFunc(f))
hl, ok := hs.set["one"]
if len(hs.set) != 1 || !ok {
t.Errorf("Set doesn't contain 'one' list after add().")
@ -27,7 +27,7 @@ func TestHandlerSet(t *testing.T) {
}
// Add another one...
hn2 := hs.add("one", HandlerFunc(f))
_, hn2 := hs.add("one", HandlerFunc(f))
if len(hs.set) != 1 {
t.Errorf("Set contains more than 'one' list after add().")
}
@ -36,7 +36,7 @@ func TestHandlerSet(t *testing.T) {
}
// Add a third one!
hn3 := hs.add("one", HandlerFunc(f))
_, hn3 := hs.add("one", HandlerFunc(f))
if len(hs.set) != 1 {
t.Errorf("Set contains more than 'one' list after add().")
}
@ -45,7 +45,7 @@ func TestHandlerSet(t *testing.T) {
}
// And finally a fourth one!
hn4 := hs.add("one", HandlerFunc(f))
_, hn4 := hs.add("one", HandlerFunc(f))
if len(hs.set) != 1 {
t.Errorf("Set contains more than 'one' list after add().")
}
@ -131,12 +131,12 @@ func TestCommandSet(t *testing.T) {
t.Errorf("New list contains things!")
}
cn1 := cl.add("one", HandlerFunc(func(c *Conn, l *Line) {}), 0)
_, cn1 := cl.add("one", HandlerFunc(func(c *Conn, l *Line) {}), 0)
if cl.list.Len() != 1 {
t.Errorf("Command 'one' not added to list correctly.")
}
cn2 := cl.add("one two", HandlerFunc(func(c *Conn, l *Line) {}), 0)
_, cn2 := cl.add("one two", HandlerFunc(func(c *Conn, l *Line) {}), 0)
if cl.list.Len() != 2 {
t.Errorf("Command 'one two' not added to set correctly.")
}