From d496f2a11377a9dc6fc2922bd8393d5a3af5ff54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Tue, 30 Jan 2024 19:53:22 +0800 Subject: [PATCH] docs(event): Added README.md (#84) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flcă‚› --- event/README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 event/README.md diff --git a/event/README.md b/event/README.md new file mode 100644 index 00000000..147dc875 --- /dev/null +++ b/event/README.md @@ -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 +```