event

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

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

func NewBaseEvent(eventType string, opts ...baseEventOption) BaseEvent

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) Id

func (e BaseEvent) Id() string

func (BaseEvent) MarshalJSON

func (e BaseEvent) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for BaseEvent.

func (BaseEvent) Meta

func (e BaseEvent) Meta() map[string]string

func (BaseEvent) Source

func (e BaseEvent) Source() string

func (BaseEvent) Time

func (e BaseEvent) Time() time.Time

func (BaseEvent) Type

func (e BaseEvent) Type() string

func (*BaseEvent) UnmarshalJSON

func (e *BaseEvent) UnmarshalJSON(data []byte) error

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

type HandlerFunc func(ctx context.Context, event Event)

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

type MiddlewareFunc func(ctx context.Context, event Event) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL