From f53eea9c22b30966367bc3cc5a9f4e9e1f22b34e Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Tue, 28 Sep 2021 20:59:54 +0800 Subject: [PATCH] doc(event): update event doc --- doc/pkg/event/事件驱动使用文档.md | 92 +++++++++++------------ 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/doc/pkg/event/事件驱动使用文档.md b/doc/pkg/event/事件驱动使用文档.md index d490667..37d5127 100644 --- a/doc/pkg/event/事件驱动使用文档.md +++ b/doc/pkg/event/事件驱动使用文档.md @@ -90,63 +90,59 @@ func TestNewConsumer(t *testing.T) { ```go func TestNewConsumer(t *testing.T) { - // 定义一个图书馆 - type Library struct { - driver DriverInterface - } - library := &Library{ - driver: NewDriver(100), - } - ctx := context.Background() +// 定义一个图书馆 +type Library struct { +driver DriverInterface +} +library := &Library{ +driver: NewDriver(100), +} +ctx := context.Background() - // 定义 A - type A struct { - produce ProduceInterface - } +// 定义 A +type A struct { +produce ProduceInterface +} - a := &A{ - produce: NewProduce(library.driver), - } +a := &A{ +produce: NewProduce(library.driver), +} - // 定义 B - type B struct { - consumer ConsumerInterface - } +// 定义 B +type B struct { +consumer ConsumerInterface +} - b := &B{ - consumer: NewConsumer(library.driver), - } +b := &B{ +consumer: NewConsumer(library.driver), +} - // 定义书 S 并且添加一些描述 - book := NewEvent("S") - book.SetMsg("title", "hello world") - book.SetCtxValue("pages", 120) +// 定义书 S 并且添加一些描述 +book := NewEvent("S") +book.SetMsg("title", "hello world") +book.SetValue("pages", 120) - // A 把书 S 放到图书馆 - go func() { - waitMsg, err := book.UpdateWaitEvent(2 * time.Hour) - require.NoError(t, err) - a.produce.Call(ctx, book) +// A 把书 S 放到图书馆 +go func() { +book.InitWaitEvent() +a.produce.Call(ctx, book) - // A 等待 B 的回复, 但是他最多只会等待 B 2个小时 - ttlTime, err := book.GetTtlTimer() - require.NoError(t, err) - select { - case msg := <-waitMsg: - fmt.Println(msg) - case <-ttlTime.C: - fmt.Println("过期不等了") - book.Close() - } - }() +// A 等待 B 的回复, 但是他最多只会等待 B 2个小时 +res, err := book.StartWaitEvent(2 * time.Hour) +require.NoError(t, err) +fmt.Println(res) +}() - // 模拟 B 去图书馆拿书 - book = b.consumer.Receive(ctx) - fmt.Println(book.GetMsg("title")) +// 模拟 B 去图书馆拿书 +book = b.consumer.Receive(ctx) +fmt.Println(book.GetMsg("title")) - // 书完好 - book.SetWaitResult("书完好") - time.Sleep(50 * time.Millisecond) +// 书完好 +book.ExecWorkAndSendResult(func() (interface{}, error) { +// b 检查书 +return "OK", nil +}) +time.Sleep(50 * time.Millisecond) } ```