From 37bb460561234025395021f955bcb9c35bb67c2b Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 20:17:55 +0000 Subject: [PATCH] Import event handling --- event/msg.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/event/msg.go b/event/msg.go index c3f8337..05c0e0b 100644 --- a/event/msg.go +++ b/event/msg.go @@ -2,6 +2,7 @@ package event import ( "context" + "fmt" ) const ( @@ -37,3 +38,28 @@ type EventTokenMint struct { TxHash string VoucherAddress string } + +type EventsHandlerFunc func(context.Context, any) error + +type EventsHandler struct { + handlers map[string]EventsHandlerFunc +} + +func NewEventsHandler() *EventsHandler { + return &EventsHandler{ + handlers: make(map[string]EventsHandlerFunc), + } +} + +func (eh *EventsHandler) WithHandler(tag string, fn EventsHandlerFunc) *EventsHandler { + eh.handlers[tag] = fn + return eh +} + +func (eh *EventsHandler) Handle(ctx context.Context, tag string, o any) error { + fn, ok := eh.handlers[tag] + if !ok { + fmt.Errorf("Handler not registered for tag: %s", tag) + } + return fn(ctx, o) +}