https://github.com/leonsteinhaeuser/observer
This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.
https://github.com/leonsteinhaeuser/observer
event events generics go golang notify notify-all pub-sub subscribe
Last synced: 9 months ago
JSON representation
This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.
- Host: GitHub
- URL: https://github.com/leonsteinhaeuser/observer
- Owner: leonsteinhaeuser
- License: mit
- Created: 2022-04-09T19:24:48.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-19T10:13:58.000Z (over 3 years ago)
- Last Synced: 2024-10-28T09:46:05.553Z (over 1 year ago)
- Topics: event, events, generics, go, golang, notify, notify-all, pub-sub, subscribe
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 9
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# observer
[](https://github.com/leonsteinhaeuser/observer/actions/workflows/tests.yml)
[](https://codecov.io/gh/leonsteinhaeuser/observer)
[](https://img.shields.io/github/release-date/leonsteinhaeuser/observer)
[](https://img.shields.io/github/commits-since/leonsteinhaeuser/observer/latest)
[](https://img.shields.io/github/issues/leonsteinhaeuser/observer/bug)
[](https://img.shields.io/github/issues/leonsteinhaeuser/observer/feature%20request)
[](https://img.shields.io/github/issues-closed/leonsteinhaeuser/observer)
[](https://img.shields.io/github/license/leonsteinhaeuser/observer)
This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.
## Differences between v1 and v2
The v2 release contains breaking changes. The most important ones are:
- The `Observable` interface does not provide a `Clients() int64` method anymore.
- The constructor `NewObserver()` has been removed. Instead, use `new(observer.Observer[T])`.
- The `Observer` has become a `NotifyTimeout` that can be used to set a timeout for the `NotifyAll` method. The default value is `5 * time.Second`.
## Usage
```bash
go get github.com/leonsteinhaeuser/observer/v2
```
## Example
```go
package main
import (
"fmt"
"github.com/leonsteinhaeuser/observer/v2"
)
type Event struct {
ID int
Message string
}
var (
obsrv *observer.Observer[Event] = new(observer.Observer[Event])
)
func main() {
rspCh, cancelFunc := obsrv.Subscribe()
defer cancelFunc()
go func() {
for {
fmt.Printf("Received event: %v\n", <-rspCh)
}
}()
fmt.Println("Registered Clients: ", obsrv.Clients())
obsrv.NotifyAll(Event{
ID: i,
Message: "Hello World",
})
}
```
If you would like to see a more detailed example, please take a look at the [observer](_example/handler/main.go) example.