Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ type HouseWasSold struct{}
func main() {
// Create a dispatcher mapping projections (string or struct) to handlers
dispatcher := GoEventBus.Dispatcher{
"user_created": func(ctx context.Context, args map[string]any) (GoEventBus.Result, error) {
userID := args["id"].(string)
"user_created": func(ctx context.Context, ev GoEventBus.Event) (GoEventBus.Result, error) {
userID := ev.Args["id"].(string)
fmt.Println("User created with ID:", userID)
return GoEventBus.Result{Message: "handled user_created"}, nil
},
HouseWasSold{}: func(ctx context.Context, args map[string]any) (GoEventBus.Result, error) {
address := args["address"].(string)
price := args["price"].(int)
HouseWasSold{}: func(ctx context.Context, ev GoEventBus.Event) (GoEventBus.Result, error) {
address := ev.Args["address"].(string)
price := ev.Args["price"].(int)
fmt.Printf("House sold at %s for $%d\n", address, price)
return GoEventBus.Result{Message: "handled HouseWasSold"}, nil
},
Expand Down Expand Up @@ -140,10 +140,10 @@ func main() {

// Create a dispatcher mapping projections to handlers
dispatcher := GoEventBus.Dispatcher{
"user_created": func(ctx context.Context, args map[string]any) (GoEventBus.Result, error) {
"user_created": func(ctx context.Context, ev GoEventBus.Event) (GoEventBus.Result, error) {
return GoEventBus.Result{Message: "handled user_created"}, nil
},
"send_email": func(ctx context.Context, args map[string]any) (GoEventBus.Result, error) {
"send_email": func(ctx context.Context, ev GoEventBus.Event) (GoEventBus.Result, error) {
log.Println("Hello")
return GoEventBus.Result{Message: "handled send_email"}, nil
},
Expand Down
9 changes: 5 additions & 4 deletions eventstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const cacheLine = 64
type pad [cacheLine - unsafe.Sizeof(uint64(0))]byte

// HandlerFunc is the signature for event handlers and middleware.
type HandlerFunc func(context.Context, map[string]any) (Result, error)
type HandlerFunc func(context.Context, Event) (Result, error)

// Middleware wraps a HandlerFunc, returning a new HandlerFunc.
type Middleware func(HandlerFunc) HandlerFunc
Expand All @@ -52,7 +52,8 @@ type Dispatcher map[interface{}]HandlerFunc
type Event struct {
ID string
Projection interface{}
Args map[string]any
Data any // Type-safe payload (preferred)
Args map[string]any // Legacy payload (deprecated)
Ctx context.Context // carried context from Subscribe
}

Expand Down Expand Up @@ -220,7 +221,7 @@ func (es *EventStore) execute(h HandlerFunc, ev Event) {
if ctx == nil {
ctx = context.Background()
}
// override with explicit __ctx if set
// override with explicit __ctx if set in legacy Args
if c, ok := ev.Args["__ctx"].(context.Context); ok && c != nil {
ctx = c
}
Expand All @@ -232,7 +233,7 @@ func (es *EventStore) execute(h HandlerFunc, ev Event) {
for i := len(es.middlewares) - 1; i >= 0; i-- {
wrapped = es.middlewares[i](wrapped)
}
res, err := wrapped(ctx, ev.Args)
res, err := wrapped(ctx, ev)
atomic.AddUint64(&es.processedCount, 1)

for _, hook := range es.afterHooks {
Expand Down
Loading