1
0
mirror of https://github.com/pocke/goevent.git synced 2025-04-24 13:48:55 +08:00

Implement Table Off

This commit is contained in:
pocke 2015-01-11 22:51:20 +09:00
parent 37afb7b77b
commit c1d558fc9e
2 changed files with 28 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
type Table interface {
On(string, interface{}) error
Trigger(string, ...interface{}) error
Off(string, interface{})
}
type table struct {
@ -45,4 +46,16 @@ func (t *table) On(name string, f interface{}) error {
return ev.On(f)
}
// XXX: return error?
func (t *table) Off(name string, f interface{}) {
t.mu.Lock()
defer t.mu.Unlock()
e, ok := t.events[name]
if !ok {
return
}
e.Off(f)
}
var _ Table = &table{}

View File

@ -35,3 +35,18 @@ func TestTableTriggerFail(t *testing.T) {
t.Error("should return error when event has not been defined yet. But got nil")
}
}
func TestTableOff(t *testing.T) {
ta := goevent.NewTable()
ta.Off("foo", func() {})
i := 0
f := func() { i++ }
ta.On("foo", f)
ta.Trigger("foo")
ta.Off("foo", f)
ta.Trigger("foo")
if i != 1 {
t.Errorf("i expected 1, but got %d", i)
}
}