Skip to content

Commit

Permalink
docs: autoupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Sep 5, 2024
1 parent 3c3937b commit 7c575b5
Showing 1 changed file with 83 additions and 2 deletions.
85 changes: 83 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>)


<a name="Event"></a>
## type [Event](<https://github.com/atomicgo/event/blob/main/event.go#L6-L9>)

Event represents an event system that can handle multiple listeners.

```go
type Event[T any] struct {
// contains filtered or unexported fields
}
```

<a name="New"></a>
### func [New](<https://github.com/atomicgo/event/blob/main/event.go#L11>)

```go
func New[T any]() *Event[T]
```



<a name="Event[T].Close"></a>
### func \(\*Event\[T\]\) [Close](<https://github.com/atomicgo/event/blob/main/event.go#L44>)

```go
func (e *Event[T]) Close()
```



<a name="Event[T].Listen"></a>
### func \(\*Event\[T\]\) [Listen](<https://github.com/atomicgo/event/blob/main/event.go#L30>)

```go
func (e *Event[T]) Listen(f func(T))
```

Listen gets called when the event is triggered.

<a name="Event[T].Trigger"></a>
### func \(\*Event\[T\]\) [Trigger](<https://github.com/atomicgo/event/blob/main/event.go#L18>)

```go
func (e *Event[T]) Trigger(value T)
```

Trigger triggers the event and notifies all listeners.

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

Expand Down

0 comments on commit 7c575b5

Please sign in to comment.