From 34b659d3ec20b002557038c841b63960e7397c0f Mon Sep 17 00:00:00 2001 From: caixw Date: Fri, 21 Jul 2023 22:58:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=20ErrStopped=20=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/go.yml | 2 +- events.go | 14 +++++++++----- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5b80bbd..f8bbc92 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/events.go b/events.go index 52cdc9b..84f8b08 100644 --- a/events.go +++ b/events.go @@ -22,8 +22,7 @@ import ( "sync" ) -// ErrStopped 表示发布都已经调用 [Publisher.Destroy] 销毁了事件处理器 -var ErrStopped = errors.New("该事件已经停止发布新内容") +var errStopped = errors.New("events: stopped") // SubscribeFunc 订阅者函数 // @@ -67,6 +66,9 @@ type Eventer[T any] interface { Subscriber[T] } +// errStopped 表示发布都已经调用 [Publisher.Destroy] 销毁了事件处理器 +func ErrStopped() error { return errStopped } + // New 声明一个新的事件处理 // // T 为事件传递过程的参数类型; @@ -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() @@ -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 diff --git a/go.mod b/go.mod index d4534ef..ef2d55b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2ba59f3..45d127e 100644 --- a/go.sum +++ b/go.sum @@ -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=