Import event handling

This commit is contained in:
lash 2025-01-13 20:17:55 +00:00
parent 5511955438
commit 37bb460561
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -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)
}