events

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseEvent

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

The base event implements the Event interface adding some basic fields

func NewBaseEvent

func NewBaseEvent(name string) BaseEvent

func (*BaseEvent) CreatedAt

func (b *BaseEvent) CreatedAt() time.Time

func (*BaseEvent) Ctx

func (b *BaseEvent) Ctx() context.Context

func (*BaseEvent) Payload

func (b *BaseEvent) Payload() any

func (*BaseEvent) Tag

func (b *BaseEvent) Tag() string

func (*BaseEvent) WithCtx added in v0.1.1

func (b *BaseEvent) WithCtx(ctx context.Context) Event

func (*BaseEvent) WithPayload

func (b *BaseEvent) WithPayload(payload any) *BaseEvent

type ErrorHandler

type ErrorHandler func(error) error

type Event

type Event interface {
	// Tag returns the event name
	Tag() string
	// CreatedAt returns the event creation time
	CreatedAt() time.Time
	// Payload returns the event payload
	Payload() any
	// WithCtx sets context for event.
	// This method will be used by the
	// events service when you emit an event.
	// This means that if you put a context this will be overrited.
	WithCtx(context.Context) Event
	// Ctx returns event context
	Ctx() context.Context
}

Event is the main interface where the library turns around the service expects an Event interface and you can implement your own events

type EventListener

type EventListener func(Event) error

type EventService

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

func NewEventService

func NewEventService(config ...EventServiceConfig) *EventService

Returns a new event service with default config or custom one

func (*EventService) Emit

func (e *EventService) Emit(event Event)

Emit will add a provided event to the event queue and all listeners that match with event name will react to this event

func (*EventService) IsClosed

func (e *EventService) IsClosed() bool

IsClosed cheks if event service is cosed

func (*EventService) Listen

func (e *EventService) Listen(tag string, listener EventListener) ListenerDestroyer

Register a new event listener and returns the destroyer for that listener. Remember tag accepts glob regex. Ej.

- listen all events: "*"

- listen all events under a namespace: "users.*"

- listen all created events: "*.created"

For more information see https://github.com/gobwas/glob

func (*EventService) Start

func (e *EventService) Start()

Start turn up the event listener engine. At this moment listeners will start reacting to the emitted events

func (*EventService) Stop

func (e *EventService) Stop()

Stop ends event emitting and listening. At this moment every emit or listen will panic

type EventServiceConfig

type EventServiceConfig struct {
	ErrorHandler ErrorHandler
}

func GetDefaultConfig

func GetDefaultConfig() EventServiceConfig

Returns default config for events service

type ListenerDestroyer

type ListenerDestroyer func()

Jump to

Keyboard shortcuts

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