Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithMeta ¶
func WithMeta(key, value string) baseEventOption
WithMeta adds a metadata key-value pair.
func WithSource ¶
func WithSource(source string) baseEventOption
Types ¶
type BaseEvent ¶
type BaseEvent struct {
// contains filtered or unexported fields
}
BaseEvent provides a default implementation of the Event interface. Custom events can embed this struct to inherit the base functionality. Fields are unexported to prevent modification after creation.
func NewBaseEvent ¶
NewBaseEvent creates a new BaseEvent with the specified type. It automatically generates a unique ID and sets the current time. Optional source and metadata can be set using WithSource and WithMeta options.
func (BaseEvent) MarshalJSON ¶
MarshalJSON implements custom JSON marshaling for BaseEvent.
func (*BaseEvent) UnmarshalJSON ¶
UnmarshalJSON implements custom JSON unmarshaling for BaseEvent.
type Bus ¶
type Bus interface {
Publisher
Subscriber
// Start initializes the event bus and begins processing events.
Start() error
// Shutdown gracefully shuts down the event bus.
Shutdown(ctx context.Context) error
}
Bus combines Publisher and Subscriber interfaces along with lifecycle management.
type Event ¶
type Event interface {
// Id returns a unique identifier for this specific event instance.
Id() string
// Type returns a unique string identifier for the event type.
// This is used for routing and filtering events.
Type() string
// Source returns the source that generated this event.
Source() string
// Time returns when the event occurred.
Time() time.Time
// Meta returns the metadata for the event.
Meta() map[string]string
}
Event represents the base interface for all events in the system. All custom events should embed this interface to be compatible with the event bus.
type HandlerFunc ¶
HandlerFunc represents a function that can handle events. The handler receives the event and a context for cancellation/timeout control.
type Middleware ¶
type Middleware interface {
// Process is called for each event before it's delivered to handlers.
// It can modify the event, context, or prevent delivery by returning an error.
Process(ctx context.Context, event Event, next MiddlewareFunc) error
}
Middleware defines an interface for event processing middleware. Middleware can intercept and modify events before they reach handlers.
type MiddlewareFunc ¶
MiddlewareFunc is a function type for middleware processing.
type Publisher ¶
type Publisher interface {
// Publish sends an event to all registered subscribers asynchronously.
Publish(event Event)
}
Publisher defines the interface for publishing events to the event bus.
type Subscriber ¶
type Subscriber interface {
// Subscribe registers a handler for events of a specific type.
// Returns an unsubscribe function that can be called to remove the subscription.
Subscribe(eventType string, handler HandlerFunc) UnsubscribeFunc
}
Subscriber defines the interface for subscribing to events.
type UnsubscribeFunc ¶
type UnsubscribeFunc func()
UnsubscribeFunc is a function that can be called to unsubscribe from an event.