events

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: AGPL-3.0 Imports: 5 Imported by: 0

README

events

import "github.com/agentstation/starmap/internal/server/events"

Package events provides a unified event system for real-time catalog updates.

This package implements a broker pattern that connects Starmap's hooks system to multiple transport mechanisms (WebSocket, SSE, etc.) through a common event pipeline. This eliminates code duplication and provides a single point for event distribution.

Index

type Broker

Broker manages event distribution to multiple subscribers. It provides a central hub for catalog events, fanning them out to all registered subscribers (WebSocket, SSE, etc.) concurrently.

type Broker struct {
    // contains filtered or unexported fields
}

func NewBroker
func NewBroker(logger *zerolog.Logger) *Broker

NewBroker creates a new event broker.

func (*Broker) Publish
func (b *Broker) Publish(eventType EventType, data any)

Publish sends an event to all subscribers.

func (*Broker) Run
func (b *Broker) Run(ctx context.Context)

Run starts the broker's event loop. Should be called in a goroutine. The broker will run until the context is cancelled.

func (*Broker) Subscribe
func (b *Broker) Subscribe(sub Subscriber)

Subscribe registers a new subscriber to receive events.

func (*Broker) SubscriberCount
func (b *Broker) SubscriberCount() int

SubscriberCount returns the current number of subscribers.

func (*Broker) Unsubscribe
func (b *Broker) Unsubscribe(sub Subscriber)

Unsubscribe removes a subscriber from receiving events.

type Event

Event represents a catalog event with type, timestamp, and data.

type Event struct {
    Type      EventType `json:"type"`
    Timestamp time.Time `json:"timestamp"`
    Data      any       `json:"data"`
}

type EventType

EventType represents the type of catalog event.

type EventType string

Event types for catalog changes.

const (
    // Model events (from Starmap hooks)
    ModelAdded   EventType = "model.added"
    ModelUpdated EventType = "model.updated"
    ModelDeleted EventType = "model.deleted"

    // Sync events (from sync operations)
    SyncStarted   EventType = "sync.started"
    SyncCompleted EventType = "sync.completed"

    // Client events (from transport layers)
    ClientConnected EventType = "client.connected"
)

type Subscriber

Subscriber is an interface for event consumers. Implementations adapt the unified event stream to specific transport mechanisms (WebSocket, SSE, MQTT, webhooks, etc.).

type Subscriber interface {
    // Send delivers an event to the subscriber.
    // Implementations should be non-blocking and handle errors gracefully.
    Send(Event) error

    // Close cleanly shuts down the subscriber.
    Close() error
}

Generated by gomarkdoc

Documentation

Overview

Package events provides a unified event system for real-time catalog updates.

This package implements a broker pattern that connects Starmap's hooks system to multiple transport mechanisms (WebSocket, SSE, etc.) through a common event pipeline. This eliminates code duplication and provides a single point for event distribution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker struct {
	// contains filtered or unexported fields
}

Broker manages event distribution to multiple subscribers. It provides a central hub for catalog events, fanning them out to all registered subscribers (WebSocket, SSE, etc.) concurrently.

func NewBroker

func NewBroker(logger *zerolog.Logger) *Broker

NewBroker creates a new event broker.

func (*Broker) EventsDropped

func (b *Broker) EventsDropped() uint64

EventsDropped returns the total number of events dropped.

func (*Broker) EventsPublished

func (b *Broker) EventsPublished() uint64

EventsPublished returns the total number of events published.

func (*Broker) Publish

func (b *Broker) Publish(eventType EventType, data any)

Publish sends an event to all subscribers.

func (*Broker) QueueDepth

func (b *Broker) QueueDepth() int

QueueDepth returns the current number of events in the queue.

func (*Broker) Run

func (b *Broker) Run(ctx context.Context)

Run starts the broker's event loop. Should be called in a goroutine. The broker will run until the context is cancelled.

func (*Broker) Subscribe

func (b *Broker) Subscribe(sub Subscriber)

Subscribe registers a new subscriber to receive events.

func (*Broker) SubscriberCount

func (b *Broker) SubscriberCount() int

SubscriberCount returns the current number of subscribers.

func (*Broker) Unsubscribe

func (b *Broker) Unsubscribe(sub Subscriber)

Unsubscribe removes a subscriber from receiving events.

type Event

type Event struct {
	Type      EventType `json:"type"`
	Timestamp time.Time `json:"timestamp"`
	Data      any       `json:"data"`
}

Event represents a catalog event with type, timestamp, and data.

type EventType

type EventType string

EventType represents the type of catalog event.

const (
	// Model events (from Starmap hooks).
	ModelAdded   EventType = "model.added"
	ModelUpdated EventType = "model.updated"
	ModelDeleted EventType = "model.deleted"

	// Sync events (from sync operations).
	SyncStarted   EventType = "sync.started"
	SyncCompleted EventType = "sync.completed"

	// Client events (from transport layers).
	ClientConnected EventType = "client.connected"
)

Event types for catalog changes.

type Subscriber

type Subscriber interface {
	// Send delivers an event to the subscriber.
	// Implementations should be non-blocking and handle errors gracefully.
	Send(Event) error

	// Close cleanly shuts down the subscriber.
	Close() error
}

Subscriber is an interface for event consumers. Implementations adapt the unified event stream to specific transport mechanisms (WebSocket, SSE, MQTT, webhooks, etc.).

Directories

Path Synopsis
Package adapters provides transport-specific implementations of the Subscriber interface.
Package adapters provides transport-specific implementations of the Subscriber interface.

Jump to

Keyboard shortcuts

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