Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agoalofalife/event
The implementation of the pattern observer
https://github.com/agoalofalife/event
event event-go events go go-event golang
Last synced: about 1 month ago
JSON representation
The implementation of the pattern observer
- Host: GitHub
- URL: https://github.com/agoalofalife/event
- Owner: agoalofalife
- License: mit
- Created: 2017-07-02T12:19:56.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-19T12:11:32.000Z (almost 7 years ago)
- Last Synced: 2024-10-16T00:29:36.469Z (about 2 months ago)
- Topics: event, event-go, events, go, go-event, golang
- Language: Go
- Homepage:
- Size: 31.3 KB
- Stars: 58
- Watchers: 4
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - event - Implementation of the pattern observer. (Messaging / Search and Analytic Databases)
- zero-alloc-awesome-go - event - Implementation of the pattern observer. (Messaging / Search and Analytic Databases)
- awesome-go - event - The implementation of the pattern observer - ★ 16 (Messaging)
- awesome-go-extra - event - 07-02T12:19:56Z|2018-02-19T12:11:32Z| (Messaging / Advanced Console UIs)
- awesome-go-zh - event
README
## Event
[![Build Status](https://travis-ci.org/agoalofalife/event.svg?branch=master)](https://travis-ci.org/agoalofalife/event)
[![codecov](https://codecov.io/gh/agoalofalife/event/branch/master/graph/badge.svg)](https://codecov.io/gh/agoalofalife/event)
[![Go Report Card](https://goreportcard.com/badge/github.com/agoalofalife/event)](https://goreportcard.com/report/github.com/agoalofalife/event)
[![GoDoc](http://godoc.org/github.com/agoalofalife/event?status.svg)](http://godoc.org/github.com/agoalofalife/event)This is package implements [pattern-observer](https://en.wikipedia.org/wiki/Observer_pattern)
### Fast example
```go
import (
"github.com/agoalofalife/event"
)func main() {
// create struct
e := event.New()// subscriber
e.Add("push.email", func(text string){
// some logic
}, "text")
// init event
e.Fire("push.email") // or e.Go("push.email")
}
```let us consider an example:
* You must first create the structure
* Next, the first argument declares the name of the event (string type), second argument executes when the event occurs, the third argument is passed a list of arguments, which are substituted in the parameters of the second argument.
* In the end you need to run the event. There are two methods available "Go" and his alias "Fire"### The subscriber function method
```go
type Email struct {
count int
}
func (e *Email) Push() {
e.count += 1
fmt.Printf("Push email again, count %d \n", e.count)
}
func main() {
e := event.New()
email := new(Email)
e.Add(email, email.Push)
e.Fire(email)
e.Fire(email)
}
// output
// Push email again, count 1
// Push email again, count 2
```## Bench
```
// create struct and add new event handler
BenchmarkAdd-8 1000000 1482 ns/op// create struct and add new event handler and N run this handler
BenchmarkGo-8 5000000 339 ns/op```
* In this example we sign the event method structure
Read more information in [examples](https://github.com/agoalofalife/event/tree/master/examples) :+1: