Documentation
¶
Index ¶
Constants ¶
const EventBufferSize = 16
EventBufferSize is the size of the event buffer of the channel for each subscribers.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event struct { Type EventType `json:"type"` Message string `json:"message"` Payload any `json:"payload"` }
Event is the event that is published to the subscription.
type EventService ¶
type EventService struct {
// contains filtered or unexported fields
}
EventService is a service for publishing and subscribing to events.
func NewEventService ¶
func NewEventService() *EventService
NewEventService creates a new event service.
func (*EventService) PublishEvent ¶
func (s *EventService) PublishEvent(ctx context.Context, event Event)
PublishEvent publishes the given event to all subscribers.
func (*EventService) Subscribe ¶
func (s *EventService) Subscribe(ctx context.Context, event EventType) (sub *Subscription)
Subscribe subscribes to the given event. Context is used to automatically unsubscribe the subscription when it is canceled.
func (*EventService) Unsubscribe ¶
func (s *EventService) Unsubscribe(sub *Subscription)
Unsubscribe unsubscribes the given subscription. It is safe to call this method concurrently.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription is a subscription to an event service.
func (*Subscription) C ¶
func (s *Subscription) C() <-chan Event
C returns the channel of the subscription.
func (*Subscription) Ctx ¶
func (s *Subscription) Ctx() context.Context
Ctx returns the context of the subscription.