events

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Type      EventType              `json:"type"`
	Timestamp time.Time              `json:"timestamp"`
	Data      interface{}            `json:"data"`
	Source    string                 `json:"source"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
	ID        string                 `json:"id,omitempty"`
}

Event represents an event that occurs in the system

func CreateEvent

func CreateEvent(eventType EventType, data interface{}, source string) Event

CreateEvent is a helper function to create a new event

func CreateEventWithID

func CreateEventWithID(eventType EventType, data interface{}, source, id string) Event

CreateEventWithID creates a new event with a specific ID

type EventEmitter

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

EventEmitter manages event listeners and emits events

func NewEventEmitter

func NewEventEmitter() *EventEmitter

NewEventEmitter creates a new event emitter

func (*EventEmitter) Close

func (ee *EventEmitter) Close()

Close closes the event emitter and prevents new listeners from being added

func (*EventEmitter) Emit

func (ee *EventEmitter) Emit(event Event)

Emit emits an event to all registered listeners

func (*EventEmitter) EmitSync

func (ee *EventEmitter) EmitSync(event Event)

EmitSync emits an event synchronously (waits for all listeners to complete)

func (*EventEmitter) EmitWithContext

func (ee *EventEmitter) EmitWithContext(ctx context.Context, event Event) error

EmitWithContext emits an event with a context for cancellation

func (*EventEmitter) EventTypes

func (ee *EventEmitter) EventTypes() []EventType

EventTypes returns all event types that have listeners

func (*EventEmitter) IsClosed

func (ee *EventEmitter) IsClosed() bool

IsClosed returns whether the event emitter is closed

func (*EventEmitter) ListenerCount

func (ee *EventEmitter) ListenerCount(eventType EventType) int

ListenerCount returns the number of listeners for a specific event type

func (*EventEmitter) Off

func (ee *EventEmitter) Off(eventType EventType, listener EventListener)

Off removes the most recently added listener for the specified event type Note: Due to Go's limitations with function comparison, this removes the last added listener

func (*EventEmitter) On

func (ee *EventEmitter) On(eventType EventType, listener EventListener)

On adds an event listener for the specified event type

func (*EventEmitter) Once

func (ee *EventEmitter) Once(eventType EventType, listener EventListener)

Once adds an event listener that will only be called once

func (*EventEmitter) RemoveAllListeners

func (ee *EventEmitter) RemoveAllListeners(eventType ...EventType)

RemoveAllListeners removes all listeners for a specific event type, or all listeners if no event type is specified

func (*EventEmitter) WaitForEvent

func (ee *EventEmitter) WaitForEvent(ctx context.Context, eventType EventType) (Event, error)

WaitForEvent waits for a specific event type to be emitted

type EventFilter

type EventFilter func(event Event) bool

EventFilter represents a function that can filter events

type EventListener

type EventListener func(event Event)

EventListener is a function that handles events

type EventType

type EventType string

EventType represents different types of events in the system

const (
	// Download events
	EventDownloadStarted   EventType = "download_started"
	EventDownloadProgress  EventType = "download_progress"
	EventDownloadCompleted EventType = "download_completed"
	EventDownloadFailed    EventType = "download_failed"
	EventDownloadPaused    EventType = "download_paused"
	EventDownloadResumed   EventType = "download_resumed"
	EventDownloadCancelled EventType = "download_cancelled"

	// Chunk events
	EventChunkStarted   EventType = "chunk_started"
	EventChunkCompleted EventType = "chunk_completed"
	EventChunkFailed    EventType = "chunk_failed"

	// Storage events
	EventStorageSaved   EventType = "storage_saved"
	EventStorageLoaded  EventType = "storage_loaded"
	EventStorageDeleted EventType = "storage_deleted"
	EventStorageFailed  EventType = "storage_failed"

	// Protocol events
	EventProtocolConnected    EventType = "protocol_connected"
	EventProtocolDisconnected EventType = "protocol_disconnected"
	EventProtocolError        EventType = "protocol_error"

	// Plugin events
	EventPluginLoaded   EventType = "plugin_loaded"
	EventPluginUnloaded EventType = "plugin_unloaded"
	EventPluginError    EventType = "plugin_error"

	// System events
	EventSystemStarted  EventType = "system_started"
	EventSystemShutdown EventType = "system_shutdown"
	EventSystemError    EventType = "system_error"

	// Cache events
	EventCacheHit   EventType = "cache_hit"
	EventCacheMiss  EventType = "cache_miss"
	EventCacheEvict EventType = "cache_evict"

	// Authentication events
	EventAuthSuccess EventType = "auth_success"
	EventAuthFailure EventType = "auth_failure"
	EventAuthExpired EventType = "auth_expired"
)

type FilteredEventEmitter

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

FilteredEventEmitter wraps an EventEmitter with filtering capabilities

func NewFilteredEventEmitter

func NewFilteredEventEmitter(emitter *EventEmitter, filter EventFilter) *FilteredEventEmitter

NewFilteredEventEmitter creates a new filtered event emitter

func (*FilteredEventEmitter) Emit

func (fee *FilteredEventEmitter) Emit(event Event)

Emit emits an event only if it passes the filter

func (*FilteredEventEmitter) Off

func (fee *FilteredEventEmitter) Off(eventType EventType, listener EventListener)

Off removes a listener from the underlying emitter

func (*FilteredEventEmitter) On

func (fee *FilteredEventEmitter) On(eventType EventType, listener EventListener)

On adds a listener to the underlying emitter

func (*FilteredEventEmitter) Once

func (fee *FilteredEventEmitter) Once(eventType EventType, listener EventListener)

Once adds a once listener to the underlying emitter

Jump to

Keyboard shortcuts

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