diff --git a/README.md b/README.md index 83a5409..07f8aad 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ import "atomicgo.dev/event" ``` -Package event provides a generic event system for Go. +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. @@ -150,17 +150,26 @@ Player "Alice" joined the game ## Index +- [Variables](<#variables>) - [type Event](<#Event>) - [func New\[T any\]\(\) \*Event\[T\]](<#New>) - [func \(e \*Event\[T\]\) Close\(\)](<#Event[T].Close>) - - [func \(e \*Event\[T\]\) Listen\(f func\(T\)\)](<#Event[T].Listen>) - - [func \(e \*Event\[T\]\) Trigger\(value T\)](<#Event[T].Trigger>) + - [func \(e \*Event\[T\]\) Listen\(f func\(T\)\) error](<#Event[T].Listen>) + - [func \(e \*Event\[T\]\) Trigger\(value T\) error](<#Event[T].Trigger>) +## Variables + +ErrEventClosed is returned when an operation is attempted on a closed event. + +```go +var ErrEventClosed = errors.New("event is closed") +``` + -## type [Event]() +## type [Event]() -Event represents an event system that can handle multiple listeners. +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. ```go type Event[T any] struct { @@ -169,22 +178,22 @@ type Event[T any] struct { ``` -### func [New]() +### func [New]() ```go func New[T any]() *Event[T] ``` -New creates a new event. +New creates and returns a new Event instance for the specified type T. -### func \(\*Event\[T\]\) [Close]() +### func \(\*Event\[T\]\) [Close]() ```go func (e *Event[T]) Close() ``` -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. +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. @@ -250,22 +259,22 @@ func main() { -### func \(\*Event\[T\]\) [Listen]() +### func \(\*Event\[T\]\) [Listen]() ```go -func (e *Event[T]) Listen(f func(T)) +func (e *Event[T]) Listen(f func(T)) error ``` -Listen gets called when the event is triggered. +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. -### func \(\*Event\[T\]\) [Trigger]() +### func \(\*Event\[T\]\) [Trigger]() ```go -func (e *Event[T]) Trigger(value T) +func (e *Event[T]) Trigger(value T) error ``` -Trigger triggers the event and notifies all listeners. +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. Generated by [gomarkdoc]()