Skip to content

Commit 2e76456

Browse files
committed
docs: autoupdate
1 parent eaf9942 commit 2e76456

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

README.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
import "atomicgo.dev/event"
8787
```
8888

89-
Package event provides a generic event system for Go.
89+
Package event provides a generic and thread\-safe event system for Go. It allows multiple listeners to subscribe to events carrying data of any type. Listeners can be added and notified when events are triggered, and the event can be closed to prevent further operations.
9090

9191

9292

@@ -150,17 +150,26 @@ Player "Alice" joined the game
150150

151151
## Index
152152

153+
- [Variables](<#variables>)
153154
- [type Event](<#Event>)
154155
- [func New\[T any\]\(\) \*Event\[T\]](<#New>)
155156
- [func \(e \*Event\[T\]\) Close\(\)](<#Event[T].Close>)
156-
- [func \(e \*Event\[T\]\) Listen\(f func\(T\)\)](<#Event[T].Listen>)
157-
- [func \(e \*Event\[T\]\) Trigger\(value T\)](<#Event[T].Trigger>)
157+
- [func \(e \*Event\[T\]\) Listen\(f func\(T\)\) error](<#Event[T].Listen>)
158+
- [func \(e \*Event\[T\]\) Trigger\(value T\) error](<#Event[T].Trigger>)
158159

159160

161+
## Variables
162+
163+
<a name="ErrEventClosed"></a>ErrEventClosed is returned when an operation is attempted on a closed event.
164+
165+
```go
166+
var ErrEventClosed = errors.New("event is closed")
167+
```
168+
160169
<a name="Event"></a>
161-
## type [Event](<https://github.com/atomicgo/event/blob/main/event.go#L6-L10>)
170+
## type [Event](<https://github.com/atomicgo/event/blob/main/event.go#L13-L17>)
162171

163-
Event represents an event system that can handle multiple listeners.
172+
Event represents a generic, thread\-safe event system that can handle multiple listeners. The type parameter T specifies the type of data that the event carries when triggered.
164173

165174
```go
166175
type Event[T any] struct {
@@ -169,22 +178,22 @@ type Event[T any] struct {
169178
```
170179

171180
<a name="New"></a>
172-
### func [New](<https://github.com/atomicgo/event/blob/main/event.go#L13>)
181+
### func [New](<https://github.com/atomicgo/event/blob/main/event.go#L20>)
173182

174183
```go
175184
func New[T any]() *Event[T]
176185
```
177186

178-
New creates a new event.
187+
New creates and returns a new Event instance for the specified type T.
179188

180189
<a name="Event[T].Close"></a>
181-
### func \(\*Event\[T\]\) [Close](<https://github.com/atomicgo/event/blob/main/event.go#L64>)
190+
### func \(\*Event\[T\]\) [Close](<https://github.com/atomicgo/event/blob/main/event.go#L74>)
182191

183192
```go
184193
func (e *Event[T]) Close()
185194
```
186195

187-
Close closes the event and all its listeners. After calling this method, the event can't be used anymore and new listeners can't be added.
196+
Close closes the event system, preventing any new listeners from being added or events from being triggered. After calling Close, any subsequent calls to Trigger or Listen will return ErrEventClosed. Existing listeners are removed, and resources are cleaned up.
188197

189198

190199

@@ -250,22 +259,22 @@ func main() {
250259

251260

252261
<a name="Event[T].Listen"></a>
253-
### func \(\*Event\[T\]\) [Listen](<https://github.com/atomicgo/event/blob/main/event.go#L35>)
262+
### func \(\*Event\[T\]\) [Listen](<https://github.com/atomicgo/event/blob/main/event.go#L58>)
254263

255264
```go
256-
func (e *Event[T]) Listen(f func(T))
265+
func (e *Event[T]) Listen(f func(T)) error
257266
```
258267

259-
Listen gets called when the event is triggered.
268+
Listen registers a new listener callback function for the event. The listener will be invoked with the event's data whenever Trigger is called. Returns ErrEventClosed if the event has been closed.
260269

261270
<a name="Event[T].Trigger"></a>
262-
### func \(\*Event\[T\]\) [Trigger](<https://github.com/atomicgo/event/blob/main/event.go#L21>)
271+
### func \(\*Event\[T\]\) [Trigger](<https://github.com/atomicgo/event/blob/main/event.go#L27>)
263272

264273
```go
265-
func (e *Event[T]) Trigger(value T)
274+
func (e *Event[T]) Trigger(value T) error
266275
```
267276

268-
Trigger triggers the event and notifies all listeners.
277+
Trigger notifies all registered listeners by invoking their callback functions with the provided value. It runs each listener in a separate goroutine and waits for all listeners to complete. Returns ErrEventClosed if the event has been closed.
269278

270279
Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
271280

0 commit comments

Comments
 (0)