Skip to content

Commit

Permalink
feat: 添加 Event.Len 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 19, 2024
1 parent 10163e3 commit 516cedc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
10 changes: 10 additions & 0 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,19 @@ func (e *Event[T]) Subscribe(subscriber SubscribeFunc[T]) context.CancelFunc {
return func() { e.subscribers.Delete(ptr) }
}

// Reset 重置对象
func (e *Event[T]) Reset() {
e.subscribers.Range(func(key, _ any) bool {
e.subscribers.Delete(key)
return true
})
}

// Len 订阅者的数量
func (e *Event[T]) Len() (c int) {
e.subscribers.Range(func(key, value any) bool {
c++
return true
})
return
}
37 changes: 13 additions & 24 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func TestPublisher_Publish(t *testing.T) {
e.Publish(true, "123")

buf1 := new(bytes.Buffer)
sub1 := func(data string) {
buf1.WriteString(data)
}
sub1 := func(data string) { buf1.WriteString(data) }

c1 := e.Subscribe(sub1)
a.NotNil(c1)
Expand All @@ -42,15 +40,13 @@ func TestPublisher_Publish(t *testing.T) {

buf1.Reset()
buf2 := new(bytes.Buffer)
sub2 := func(data string) {
buf2.WriteString(data)
}
sub2 := func(data string) { buf2.WriteString(data) }
a.Empty(buf2.Bytes())
e.Subscribe(sub2)
e.Publish(false, "p2")
time.Sleep(time.Microsecond * 500)
a.Equal(buf1.String(), "p2")
a.Equal(buf2.String(), "p2")
a.Equal(buf1.String(), "p2").
Equal(buf2.String(), "p2")

buf1.Reset()
buf2.Reset()
Expand All @@ -60,21 +56,22 @@ func TestPublisher_Publish(t *testing.T) {
a.Empty(buf1.String())
a.Equal(buf2.String(), "p3")

e.Reset()
a.Zero(e.len())
}

func TestPublisher_Destroy(t *testing.T) {
func TestPublisher_Reset(t *testing.T) {
a := assert.New(t, false)

e := New[string]()
a.NotNil(e)
a.Zero(e.len())
a.Zero(e.Len())

e = New[string]()
a.NotNil(e)
e.Subscribe(s1)
a.Equal(e.len(), 1)
a.Equal(e.Len(), 1)

e.Reset()
a.Zero(e.Len())
}

func TestSubscriber_Attach_Detach(t *testing.T) {
Expand All @@ -85,19 +82,11 @@ func TestSubscriber_Attach_Detach(t *testing.T) {
c1 := e.Subscribe(s1)
c2 := e.Subscribe(s2)

a.Equal(e.len(), 2)
a.Equal(e.Len(), 2)

c1()
a.Equal(e.len(), 1)
a.Equal(e.Len(), 1)

c2()
a.Equal(e.len(), 0)
}

func (e *Event[T]) len() (c int) {
e.subscribers.Range(func(key, value any) bool {
c++
return true
})
return
a.Equal(e.Len(), 0)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/issue9/events

require github.com/issue9/assert/v4 v4.2.0
require github.com/issue9/assert/v4 v4.3.0

go 1.18
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/issue9/assert/v4 v4.2.0 h1:XJGMFYW0xfESqFRPLWbSsr0xWdkofytvQbDfNb5n9fw=
github.com/issue9/assert/v4 v4.2.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/assert/v4 v4.3.0 h1:W3XDKmttsfzihYGxJ9rJoL2ViJgWERB9IxfHcxjv65U=
github.com/issue9/assert/v4 v4.3.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=

0 comments on commit 516cedc

Please sign in to comment.