https://github.com/valsov/notifier
Implementation of a notifier library in Go. Supports middlewares
https://github.com/valsov/notifier
Last synced: about 1 year ago
JSON representation
Implementation of a notifier library in Go. Supports middlewares
- Host: GitHub
- URL: https://github.com/valsov/notifier
- Owner: valsov
- Created: 2023-10-11T17:10:32.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-14T16:08:51.000Z (over 2 years ago)
- Last Synced: 2025-01-26T05:41:34.553Z (about 1 year ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Notifier
Implementation of a Notifier library in Go, it supports middlewares and runs handlers in goroutines. The API is thread safe.
## Uses
### Base use
```go
package main
import (
"fmt"
"github.com/valsov/notifier"
)
func main() {
notifier.RegisterHandler(handler)
notifier.RegisterMiddleware(logger)
notifier.RegisterMiddleware(sample)
notifier.Publish("data")
// [...] Omitted: Need to wait here to see prints
}
func handler(x string) {
fmt.Printf("h() => %s")
}
func logger(ctx *notifier.ExecutionContext) {
fmt.Printf("START => %s")
ctx.Next() // Call next handler in execution chain
fmt.Printf("END")
}
func sample(ctx *notifier.ExecutionContext) {
fmt.Printf("sample middleware")
ctx.Next()
}
```
Running the code above outputs (note that middlewares registration order matters):
```
START => data
sample middleware
h() => data
END
```
It is possible to register multiple handlers for the same message type. They will all be executed following the registered middleware chain.
### Supported handler types
```go
func main() {
sample := sampleStruct{}
notifier.RegisterHandler(function)
notifier.RegisterHandler(sample.method)
notifier.RegisterHandler(func(parameter int){
// [...]
})
}
func function(parameter int) {
// [...]
}
type sampleStruct struct {}
func (s *sampleStruct) method(){
// [...]
}
```