Skip to content

Commit

Permalink
refactor: 将 ErrStopped 改为函数
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Jul 21, 2023
1 parent ba01a59 commit 34b659d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v3

- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
id: go
Expand Down
14 changes: 9 additions & 5 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
"sync"
)

// ErrStopped 表示发布都已经调用 [Publisher.Destroy] 销毁了事件处理器
var ErrStopped = errors.New("该事件已经停止发布新内容")
var errStopped = errors.New("events: stopped")

// SubscribeFunc 订阅者函数
//
Expand Down Expand Up @@ -67,6 +66,9 @@ type Eventer[T any] interface {
Subscriber[T]
}

// errStopped 表示发布都已经调用 [Publisher.Destroy] 销毁了事件处理器
func ErrStopped() error { return errStopped }

// New 声明一个新的事件处理
//
// T 为事件传递过程的参数类型;
Expand All @@ -77,8 +79,10 @@ func New[T any]() Eventer[T] {
}

func (e *event[T]) Publish(sync bool, data T) error {
if e.funcs == nil { // 初如化时将 e.funcs 设置为了 5,所以为 nil 表示已经调用 [Publisher.Destroy]
return ErrStopped
// 初如化时将 e.funcs 设置为了非 nil 状态,
// 所以为 nil 表示已经调用 [Publisher.Destroy]
if e.funcs == nil {
return ErrStopped()
}

e.locker.RLock()
Expand Down Expand Up @@ -111,7 +115,7 @@ func (e *event[T]) Destroy() {

func (e *event[T]) Attach(subscriber SubscribeFunc[T]) (int, error) {
if e.funcs == nil {
return 0, ErrStopped
return 0, ErrStopped()
}

ret := e.count
Expand Down
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/v3 v3.0.4
require github.com/issue9/assert/v3 v3.0.5

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/v3 v3.0.4 h1:WsYZQ6PQmM/pGFrbkn5GIXjWeVZHv+wcl2829UTX1Qc=
github.com/issue9/assert/v3 v3.0.4/go.mod h1:yft/uaskRpwQTyBT3n1zRl91SR1wNlO4fLZHzOa4bdM=
github.com/issue9/assert/v3 v3.0.5 h1:Mlz/4v2Ph6ZDm3Fk3A1jhitH4M/2dkHeHNKe7tTZdH8=
github.com/issue9/assert/v3 v3.0.5/go.mod h1:yft/uaskRpwQTyBT3n1zRl91SR1wNlO4fLZHzOa4bdM=

0 comments on commit 34b659d

Please sign in to comment.