Skip to content

Commit

Permalink
refactor: 采用 xsync 代替标准库的 sync
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 19, 2024
1 parent 516cedc commit e582743
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
go: ['1.18.x', '1.22.x']
go: ['1.18.x', '1.23.x']

steps:

Expand Down
25 changes: 10 additions & 15 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ package events
import (
"context"
"reflect"
"sync"

"github.com/puzpuzpuz/xsync/v3"
)

type (
Expand Down Expand Up @@ -53,7 +54,7 @@ type (
//
// 同时实现了 [Subscriber] 和 [Publisher] 两个接口。
Event[T any] struct {
subscribers *sync.Map
subscribers *xsync.MapOf[uintptr, SubscribeFunc[T]]
}
)

Expand All @@ -62,19 +63,19 @@ type (
// T 为事件传递过程的参数类型;
func New[T any]() *Event[T] {
return &Event[T]{
subscribers: &sync.Map{},
subscribers: xsync.NewMapOf[uintptr, SubscribeFunc[T]](),
}
}

func (e *Event[T]) Publish(sync bool, data T) {
if sync {
e.subscribers.Range(func(key, value any) bool {
go func(sub SubscribeFunc[T]) { sub(data) }(value.(SubscribeFunc[T]))
e.subscribers.Range(func(key uintptr, value SubscribeFunc[T]) bool {
go func(sub SubscribeFunc[T]) { sub(data) }(value)
return true
})
} else {
e.subscribers.Range(func(key, value any) bool {
value.(SubscribeFunc[T])(data)
e.subscribers.Range(func(key uintptr, value SubscribeFunc[T]) bool {
value(data)
return true
})
}
Expand All @@ -88,17 +89,11 @@ func (e *Event[T]) Subscribe(subscriber SubscribeFunc[T]) context.CancelFunc {

// Reset 重置对象
func (e *Event[T]) Reset() {
e.subscribers.Range(func(key, _ any) bool {
e.subscribers.Range(func(key uintptr, _ SubscribeFunc[T]) 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
}
func (e *Event[T]) Len() (c int) { return e.subscribers.Size() }
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/issue9/events

require github.com/issue9/assert/v4 v4.3.0
require (
github.com/issue9/assert/v4 v4.3.1
github.com/puzpuzpuz/xsync/v3 v3.4.0
)

go 1.18
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/issue9/assert/v4 v4.3.0 h1:W3XDKmttsfzihYGxJ9rJoL2ViJgWERB9IxfHcxjv65U=
github.com/issue9/assert/v4 v4.3.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/issue9/assert/v4 v4.3.1 h1:dHYODk1yV7j/1baIB6K6UggI4r1Hfuljqic7PaDbwLg=
github.com/issue9/assert/v4 v4.3.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=
github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=

0 comments on commit e582743

Please sign in to comment.