event

package
v1.1.16 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

The event package implements events, an event source and an event receiver. You should embed the event.Source and event.Receiver instances in your instance in order to provide facilities for subscribing to events and receiving events. For example,

type Task struct {
	event.Source
	event.Receiver
}

func main() {
	a,b  := new(Task), new(Task)

	// Receive events from task b in task a
	go a.Recv(context.TODO(), process, b)

	// Emit an event in task b which is processed in task a
	evt := event.New(context.TODO(), "key", "value")
	evt.Emit(b.C())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(ctx context.Context, err error) Event

Create a new error with context. Returns nil if the err parameter is nil.

func Infof

func Infof(ctx context.Context, key any, format string, a ...any) Event

Create an informational event

func New

func New(ctx context.Context, key, value any) Event

Create a new event with context, key and value. Returns nil if the key is nil.

Types

type Receiver

type Receiver struct {
}

func (*Receiver) Rcv

func (r *Receiver) Rcv(ctx context.Context, fn func(Event) error, src ...EventSource) error

Receive events from one or more sources and process them using the callback. If a callback returns an error or the context is cancelled, then the function ends.

func (*Receiver) String

func (s *Receiver) String() string

Return receiver as a string object

type Source

type Source struct {
	Cap int // Capacity of any created channels, setting to zero means unbuffered
	// contains filtered or unexported fields
}

Source of events, which can be subscribed to, or unsubscribed from

func (*Source) Close

func (s *Source) Close() error

Close all subscribed channels. Returns any errors (usually nil)

func (*Source) Emit

func (s *Source) Emit(e Event) bool

Emit the event to subscribers. Returns false if any channel failed, due to buffered channel being full or some other channel issue.

func (*Source) Len

func (s *Source) Len() int

Return number of subscribers

func (*Source) String

func (s *Source) String() string

Return source as a string object

func (*Source) Sub

func (s *Source) Sub() <-chan Event

Subscribe to the source of events. Returns the channel.

func (*Source) Unsub

func (s *Source) Unsub(ch <-chan Event)

Unsubscribe a channel and close it. Removes the channel from the array of channels.

Jump to

Keyboard shortcuts

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