diff --git a/README.md b/README.md index 1ba248b..c373af1 100644 --- a/README.md +++ b/README.md @@ -97,24 +97,105 @@ package main import ( "fmt" + "time" + + "atomicgo.dev/event" ) +type Player struct { + Name string +} + +var PlayerJoinEvent = event.New[Player]() + func main() { - fmt.Println("Hello, World!") + // 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(p Player) { + // Do something else + }) + + // ... + + // Trigger the event somewhere - can be in a different function or package + PlayerJoinEvent.Trigger(Player{Name: "Marvin"}) + PlayerJoinEvent.Trigger(Player{Name: "Bob"}) + PlayerJoinEvent.Trigger(Player{Name: "Alice"}) + + // Keep the program alive + time.Sleep(time.Second) + } ``` #### Output ``` -Hello, World! +Player "Marvin" joined the game +Player "Bob" joined the game +Player "Alice" joined the game ``` ## Index +- [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>) + + + +## type [Event]() + +Event represents an event system that can handle multiple listeners. + +```go +type Event[T any] struct { + // contains filtered or unexported fields +} +``` + + +### func [New]() + +```go +func New[T any]() *Event[T] +``` + + + + +### func \(\*Event\[T\]\) [Close]() + +```go +func (e *Event[T]) Close() +``` + + + + +### func \(\*Event\[T\]\) [Listen]() + +```go +func (e *Event[T]) Listen(f func(T)) +``` + +Listen gets called when the event is triggered. + + +### func \(\*Event\[T\]\) [Trigger]() + +```go +func (e *Event[T]) Trigger(value T) +``` +Trigger triggers the event and notifies all listeners. Generated by [gomarkdoc]()