-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Flc゛ <[email protected]>
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Event | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-kratos-ecosystem/components/v2/event" | ||
"github.com/go-kratos-ecosystem/components/v2/feature" | ||
) | ||
|
||
type testListener struct { | ||
feature.AsyncFeature // async feature | ||
} | ||
|
||
func (l *testListener) Listen() []event.Event { | ||
return []event.Event{ | ||
"test", | ||
"test2", | ||
} | ||
} | ||
|
||
func (l *testListener) Handle(e event.Event, data interface{}) { | ||
if s, ok := data.(string); ok { | ||
fmt.Println("event:", e, "data:", s) | ||
} else { | ||
panic("invalid data") | ||
} | ||
} | ||
|
||
type test2Listener struct{} | ||
|
||
func (l *test2Listener) Listen() []event.Event { | ||
return []event.Event{ | ||
"test", | ||
} | ||
} | ||
|
||
func (l *test2Listener) Handle(event event.Event, data interface{}) { | ||
fmt.Println("event:", event, "data:", data) | ||
} | ||
|
||
func main() { | ||
d := event.NewDispatcher(event.WithRecovery(func(err interface{}, listener event.Listener, event event.Event, data interface{}) { //nolint:lll | ||
fmt.Println("err:", err, "listener:", listener, "event:", event, "data:", data) | ||
})) | ||
|
||
d.AddListener(new(testListener), new(test2Listener)) | ||
|
||
d.Dispatch("test", "test data") | ||
d.Dispatch("test2", "test2 data") | ||
d.Dispatch("test3", "test3 data") | ||
|
||
time.Sleep(time.Second) | ||
} | ||
``` | ||
|
||
Output: | ||
|
||
```bash | ||
event: test data: test data | ||
event: test2 data: test2 data | ||
event: test data: test data | ||
``` |