From 6fc99107e65ee964a514f75d10ac9c6ce1c256e5 Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Sun, 17 Feb 2013 22:02:02 -0800 Subject: [PATCH] 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. --- client/dispatch.go | 19 +++++++++++-------- client/dispatch_test.go | 12 ++++++------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/client/dispatch.go b/client/dispatch.go index 46794c3..672e7c0 100644 --- a/client/dispatch.go +++ b/client/dispatch.go @@ -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 { diff --git a/client/dispatch_test.go b/client/dispatch_test.go index 1e3caca..101afcf 100644 --- a/client/dispatch_test.go +++ b/client/dispatch_test.go @@ -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.") }