diff --git a/irc_callback.go b/irc_callback.go index fd74fa2..cb8498c 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -40,6 +40,18 @@ func (irc *Connection) RemoveCallback(eventcode string, i string) bool { return false } +func (irc *Connection) ClearCallback(eventcode string) bool { + eventcode = strings.ToUpper(eventcode) + + if _, ok := irc.events[eventcode]; ok{ + irc.events[eventcode] = make(map[string]func(*Event)) + return true + } + + irc.Log.Println("Event not found") + return false +} + func (irc *Connection) ReplaceCallback(eventcode string, i string, callback func(*Event)) { eventcode = strings.ToUpper(eventcode) diff --git a/irc_test.go b/irc_test.go index 9938c3d..da514fa 100644 --- a/irc_test.go +++ b/irc_test.go @@ -94,3 +94,29 @@ func TestWildcardCallback(t *testing.T) { t.Error("Wildcard callback not called") } } + +func TestClearCallback(t *testing.T) { + irccon := IRC("go-eventirc", "go-eventirc") + irccon.VerboseCallbackHandler = true + + done := make(chan int, 10) + + irccon.AddCallback("TEST", func(e *Event) { done <- 0 }) + irccon.AddCallback("TEST", func(e *Event) { done <- 1 }) + irccon.ClearCallback("TEST") + irccon.AddCallback("TEST", func(e *Event) { done <- 2 }) + irccon.AddCallback("TEST", func(e *Event) { done <- 3 }) + + irccon.RunCallbacks(&Event{ + Code: "TEST", + }) + + var results []int + + results = append(results, <-done) + results = append(results, <-done) + + if len(results) != 2 || !(results[0] == 2 && results[1] == 3) { + t.Error("Callbacks not cleared") + } +} \ No newline at end of file