mirror of
https://github.com/gookit/event
synced 2025-09-26 19:11:14 +08:00
RemoveListener support closure function; fix: #9
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
@@ -88,11 +87,11 @@ func (lq *ListenerQueue) Remove(listener Listener) {
|
||||
}
|
||||
|
||||
// unsafe.Pointer(listener)
|
||||
ptrVal := fmt.Sprintf("%p", listener)
|
||||
ptrVal := getListenCompareKey(listener)
|
||||
|
||||
var newItems []*ListenerItem
|
||||
for _, li := range lq.items {
|
||||
liPtrVal := fmt.Sprintf("%p", li.Listener)
|
||||
liPtrVal := getListenCompareKey(li.Listener)
|
||||
if liPtrVal == ptrVal {
|
||||
continue
|
||||
}
|
||||
|
10
listener_key.go
Normal file
10
listener_key.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// Package event is lightweight event manager and dispatcher implements by Go.
|
||||
package event
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func getListenCompareKey(src Listener) reflect.Value {
|
||||
return reflect.ValueOf(src)
|
||||
}
|
117
listener_remove_test.go
Normal file
117
listener_remove_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type globalTestVal struct {
|
||||
n int
|
||||
sum int
|
||||
}
|
||||
type testListenerCalc struct {
|
||||
bind int
|
||||
owner *globalTestVal
|
||||
}
|
||||
|
||||
func (l testListenerCalc) Handle(e Event) error {
|
||||
l.owner.n++
|
||||
l.owner.sum += l.bind
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_RemoveListener(t *testing.T) {
|
||||
t.Run("", func(t *testing.T) {
|
||||
global := &globalTestVal{}
|
||||
makeFn := func(a int) ListenerFunc {
|
||||
return func(e Event) error {
|
||||
global.n++
|
||||
global.sum += a
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
evBus := NewManager("")
|
||||
const evName = "ev1"
|
||||
|
||||
f1 := makeFn(11)
|
||||
f2 := makeFn(22)
|
||||
f3 := makeFn(33)
|
||||
p4 := &testListenerCalc{bind: 44, owner: global}
|
||||
p5 := &testListenerCalc{bind: 55, owner: global}
|
||||
p6 := &testListenerCalc{bind: 66, owner: global}
|
||||
|
||||
evBus.On(evName, f1)
|
||||
evBus.On(evName, f2)
|
||||
evBus.On(evName, f3)
|
||||
evBus.On(evName, p4)
|
||||
evBus.On(evName, p5)
|
||||
evBus.On(evName, p6)
|
||||
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 6)
|
||||
require.Equal(t, global.sum, 231) //11+22+33+44+55+66=231
|
||||
|
||||
evBus.RemoveListener(evName, f2)
|
||||
evBus.RemoveListener(evName, p5)
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 6+4)
|
||||
require.Equal(t, global.sum, 385) // 231+11+33+44+66=385
|
||||
|
||||
evBus.RemoveListener(evName, f1)
|
||||
evBus.RemoveListener(evName, f1) // not exist function.
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 6+4+3)
|
||||
require.Equal(t, global.sum, 528) // 385+33+44+66=528
|
||||
|
||||
evBus.RemoveListener(evName, p6)
|
||||
evBus.RemoveListener(evName, p6) // not exist function.
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 6+4+3+2)
|
||||
require.Equal(t, global.sum, 605) // 528+33+44=605
|
||||
})
|
||||
t.Run("same func", func(t *testing.T) {
|
||||
global := &globalStatic
|
||||
|
||||
f1 := ListenerFunc(testFuncCalc1)
|
||||
f2 := ListenerFunc(testFuncCalc2)
|
||||
f2same := ListenerFunc(testFuncCalc2)
|
||||
f2copy := f2
|
||||
|
||||
evBus := NewManager("")
|
||||
const evName = "ev1"
|
||||
evBus.On(evName, f1)
|
||||
evBus.On(evName, f2)
|
||||
evBus.On(evName, f2same)
|
||||
evBus.On(evName, f2copy)
|
||||
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 4)
|
||||
require.Equal(t, global.sum, 77) //11+22+22+22=77
|
||||
|
||||
evBus.RemoveListener(evName, f1)
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 7)
|
||||
require.Equal(t, global.sum, 143) // 77+22+22+22=143
|
||||
|
||||
evBus.RemoveListener(evName, f2)
|
||||
evBus.MustFire(evName, nil)
|
||||
require.Equal(t, global.n, 7)
|
||||
require.Equal(t, global.sum, 143) //
|
||||
})
|
||||
}
|
||||
|
||||
var globalStatic = globalTestVal{}
|
||||
|
||||
func testFuncCalc1(e Event) error {
|
||||
globalStatic.n++
|
||||
globalStatic.sum += 11
|
||||
return nil
|
||||
}
|
||||
func testFuncCalc2(e Event) error {
|
||||
globalStatic.n++
|
||||
globalStatic.sum += 22
|
||||
return nil
|
||||
}
|
||||
|
||||
///
|
@@ -1,6 +1,7 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -117,6 +118,9 @@ func (em *Manager) addListenerItem(name string, li *ListenerItem) {
|
||||
if li.Listener == nil {
|
||||
panic("event: the event '" + name + "' listener cannot be empty")
|
||||
}
|
||||
if reflect.ValueOf(li.Listener).Kind() == reflect.Struct {
|
||||
panic("don't use struct Listener, can be pointer Listener")
|
||||
}
|
||||
|
||||
// exists, append it.
|
||||
if lq, ok := em.listeners[name]; ok {
|
||||
|
Reference in New Issue
Block a user