Documentation
¶
Overview ¶
Package events provides a generic, type-keyed in-process event bus so domain event producers and handlers are decoupled within a process. It complements the durable, cross-process outbox package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Publish ¶
Publish delivers evt to all handlers registered for type T. In sync mode (default) it runs them in registration order and returns their joined errors; in async mode it dispatches each in a goroutine and returns nil.
Example ¶
Subscribe handlers for a typed event, then publish one. In the default sync mode handlers run inline and Publish returns their joined errors.
package main
import (
"context"
"fmt"
"github.com/rahmadafandi/fibr/events"
)
func main() {
type OrderCreated struct{ ID int }
bus := events.New()
events.Subscribe(bus, func(_ context.Context, e OrderCreated) error {
fmt.Println("email: order", e.ID)
return nil
})
events.Subscribe(bus, func(_ context.Context, e OrderCreated) error {
fmt.Println("analytics: order", e.ID)
return nil
})
_ = events.Publish(context.Background(), bus, OrderCreated{ID: 7})
}
Output: email: order 7 analytics: order 7
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus is an in-process event bus. Handlers are keyed by event type.
type Option ¶
type Option func(*Bus)
Option configures a Bus.
func WithAsync ¶
func WithAsync() Option
WithAsync dispatches each handler in its own goroutine, so Publish returns immediately and handler errors are logged rather than returned.
func WithLogger ¶
WithLogger sets the logger used for async handler errors.