https://github.com/atomicgo/event
๐
Simple generic event system for Go
https://github.com/atomicgo/event
atomicgo event go golang golang-library hacktoberfest
Last synced: about 2 months ago
JSON representation
๐ Simple generic event system for Go
- Host: GitHub
- URL: https://github.com/atomicgo/event
- Owner: atomicgo
- License: mit
- Created: 2024-09-01T22:04:44.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-09-12T20:42:47.000Z (9 months ago)
- Last Synced: 2025-02-19T13:16:07.531Z (4 months ago)
- Topics: atomicgo, event, go, golang, golang-library, hacktoberfest
- Language: Go
- Homepage: https://atomicgo.dev
- Size: 20.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AtomicGo | event
---
Documentation
|
Contributing
|
Code of Conduct---
![]()
go get atomicgo.dev/event
# event
```go
import "atomicgo.dev/event"
```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.
```go
package mainimport (
"fmt"
"time""atomicgo.dev/event"
)func delay() {
time.Sleep(time.Millisecond * 10)
}type Player struct {
Name string
}// Create a new event
var PlayerJoinEvent = event.New[Player]()func main() {
// Listen to the event as many times as you want
PlayerJoinEvent.Listen(func(p Player) {
fmt.Printf("Player %q joined the game\n", p.Name)
})PlayerJoinEvent.Listen(func(_ Player) {
// Do something else
})// ...
// Trigger the event somewhere - can be in a different function or package
PlayerJoinEvent.Trigger(Player{Name: "Marvin"})
delay() // delay for deterministic output
PlayerJoinEvent.Trigger(Player{Name: "Bob"})
delay() // delay for deterministic output
PlayerJoinEvent.Trigger(Player{Name: "Alice"})// Keep the program alive
time.Sleep(time.Second)}
```#### Output
```
Player "Marvin" joined the game
Player "Bob" joined the game
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\)\) 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")
```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 {
// contains filtered or unexported fields
}
``````go
func New[T any]() *Event[T]
```New creates and returns a new Event instance for the specified type T.
### func \(\*Event\[T\]\) [Close]()```go
func (e *Event[T]) Close()
```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.
```go
package mainimport (
"fmt"
"time""atomicgo.dev/event"
)func delay() {
time.Sleep(time.Millisecond * 10)
}func main() {
// Create a new event
exampleEvent := event.New[int]()// Listen to the event
exampleEvent.Listen(func(v int) {
fmt.Println(v)
})// Trigger the event
exampleEvent.Trigger(1)
delay() // delay for deterministic output
exampleEvent.Trigger(2)
delay() // delay for deterministic output
exampleEvent.Trigger(3)// Time for listeners to process the event
delay()// Close the event
exampleEvent.Close()// Trigger the event again
exampleEvent.Trigger(4)
delay() // delay for deterministic output
exampleEvent.Trigger(5)
delay() // delay for deterministic output
exampleEvent.Trigger(6)// Keep the program alive
time.Sleep(time.Second)}
```#### Output
```
1
2
3
```
### func \(\*Event\[T\]\) [Listen]()```go
func (e *Event[T]) Listen(f func(T)) error
```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]()```go
func (e *Event[T]) Trigger(value T) error
```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]()
---
> [AtomicGo.dev](https://atomicgo.dev) ย ยทย
> with โค๏ธ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)