Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/obgnail/observer
简单的可定制obsevrer
https://github.com/obgnail/observer
golang observer
Last synced: 9 days ago
JSON representation
简单的可定制obsevrer
- Host: GitHub
- URL: https://github.com/obgnail/observer
- Owner: obgnail
- Created: 2021-09-06T04:03:10.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-06T04:04:30.000Z (over 3 years ago)
- Last Synced: 2024-11-07T02:12:53.099Z (about 2 months ago)
- Topics: golang, observer
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Use example
```go
// Custom observer0
type OperationObserver struct{}
// implement Emit() function
func (ob *OperationObserver) Emit(e *event.Event) (err error) {
switch e.Action {
case event.ActionAdd:
fmt.Println("action observer receive add action", e)
case event.ActionUpdate:
fmt.Println("action observer receive update action", e)
case event.ActionDelete:
fmt.Println("action observer receive delete action", e)
}
return nil
}// Custom observer1
type TypeObserver struct{}
func (ob *TypeObserver) Emit(e *event.Event) (err error) {
fmt.Println("type observer receive event:", e)
return nil
}func main() {
eng := engine.Default() // no delay
// eng := engine.NewTickerEngine(5 * time.Second) // events in five seconds will be delayedeng.AddObserver(&TypeObserver{})
eng.AddObserver(&OperationObserver{})ev0 := event.Default()
ev1 := event.NewDeleteEvent("myType1", nil) // Custom event
ev2 := event.NewEvent("myType3", "myAction", nil)
eng.PutEvent(ev0, ev1) // no delay
eng.PutDelayEvent(time.Second, ev2) // ev3 will push queue after one second delay// dummy sleep
time.Sleep(10 * time.Second)
}
```