eventbus

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

eventtbus is a package for a simple event bus.

Index

Constants

This section is empty.

Variables

View Source
var (
	WithMaxConcurrencyBusOpt = func(c int64) busOpt {
		return func(b *bus) {
			if c < 1 {
				c = 1
			}
			b.concurrency = c
		}
	}
	WithContinueOnErrorBusOpt = func() busOpt {
		return func(b *bus) {
			b.continueOnError = true
		}
	}
)

Bus options

View Source
var (
	WithHandlerTimeoutEventOpt = func(d time.Duration) eventOpt {
		return func(e *Event) {
			e.handlerTimeout = d
		}
	}
	WithPublishTimeoutEventOpt = func(d time.Duration) eventOpt {
		return func(e *Event) {
			e.publishTimeout = d
		}
	}
)

Event options

View Source
var (
	ErrBusClosed = errors.New("bus is closed")
)
View Source
var (
	WithTimeoutObserverOpt = func(d time.Duration) observerOpt {
		return func(o *observerOptions) {
			o.timeout = d
		}
	}
)

Observer options

Functions

func AddObserver

func AddObserver(o observer, opts ...observerOpt) string

Adds an observer. Observers are notified of all published events, and are executed in parallel.

func Close

func Close()

Signals the bus to close.

func Flush

func Flush(ctx context.Context)

Waits for all published events to finish processing.

func New

func New(opts ...busOpt) *bus

func On

func On(name Stringer) *subscription

On subscribes to an event by name in the default event bus.

func Publish

func Publish(ctx context.Context, name Stringer, data interface{}, opts ...eventOpt) error

Publishes an event with the provided name and data.

func RegexMatcher

func RegexMatcher(s string) (regexMatcher, error)

RegexMatcher is a string that utilizes a regular expression to match events.

func RemoveObserver

func RemoveObserver(id string) bool

Removes an observer.

func SetDefault

func SetDefault(eb *bus)

SetDefault sets the default event bus.

func Wait

func Wait(ctx context.Context)

Waits for the bus to be closed and then flushes.

func When

func When(matchers ...Matcher) *subscription

When subscribes to an event by arbitrary matchers in the default event bus.

func WildcardMatcher

func WildcardMatcher(s string) regexMatcher

WildcardMatcher is a string that utilizes the asterisk (*) as a wildcard character. It can match all events with "*", all events with a prefix "foo*", all events with a suffix "*bar", all events with a substring "foo*bar", or a combination of the above. A question mark (?) can be used to match a single character.

Types

type Errors

type Errors []error

func (Errors) Error

func (e Errors) Error() string

type Event

type Event struct {
	ID        string      `json:"id"`
	Name      Stringer    `json:"name"`
	Data      interface{} `json:"data"`
	Timestamp time.Time   `json:"timestamp"`
	// contains filtered or unexported fields
}

type Matcher

type Matcher interface {
	Match(Stringer, interface{}) bool
	String() string
}

Matcher is an interface that matches events.

type PredicateMatcher

type PredicateMatcher func(Stringer, interface{}) bool

PredicateMatcher is a function that accepts an event name and data and returns true if the event matches the predicate.

func ExactMatcher

func ExactMatcher(thisName Stringer) PredicateMatcher

ExactMatcher is matcher that matches events by equality.

func (PredicateMatcher) Match

func (m PredicateMatcher) Match(name Stringer, data interface{}) bool

func (PredicateMatcher) String

func (m PredicateMatcher) String() string

type StringMatcher

type StringMatcher string

StringMatcher is a string that matches events by name, ignoring case and type.

func (StringMatcher) Match

func (m StringMatcher) Match(name Stringer, data interface{}) bool

func (StringMatcher) String

func (m StringMatcher) String() string

type Stringer

type Stringer interface {
	String() string
}

Jump to

Keyboard shortcuts

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