event

package
Version: v0.0.0-...-931966b Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: EPL-2.0 Imports: 1 Imported by: 7

Documentation

Overview

Package event provides lightweight primitives for event-bus-consumer communication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Bus is a event-consumer communication layer. It manages events publications and handling, its synchronous and safe to use from multiple goroutines. Use NewBus() func to get an instance of it.

func NewBus

func NewBus() *Bus

NewBus creates a new bus.

func (*Bus) Clear

func (bus *Bus) Clear() map[string][]Consumer

Clear assigns new map value to bus consumers and returns old ones. The modification of returned map won't affect bus consumers.

func (*Bus) Pub

func (bus *Bus) Pub(e E)

Pub publishes an event to the interested consumers.

func (*Bus) Rm

func (bus *Bus) Rm(consumer Consumer) bool

Rm removes given consumer from the bus, == operator is used to determine what elements should be removed. Returns true if consumer is removed.

func (*Bus) RmIf

func (bus *Bus) RmIf(predicate func(c Consumer) bool) bool

RmIf removes consumers for which predicate func returns true. Returns true if any consumer was removed.

func (*Bus) Sub

func (bus *Bus) Sub(consumer Consumer, eType string)

Sub subscribes to the events of given type so given consumer will receive them.

func (*Bus) SubAny

func (bus *Bus) SubAny(consumer Consumer, types ...string)

SubAny does the same as Sub func, but for multiple event types.

type Consumer

type Consumer interface {

	// Accept handles given event.
	Accept(event E)
}

Consumer is an interface for event handlers.

type ConsumerF

type ConsumerF func(event E)

ConsumerF is a function which is also consumer, allows to use anonymous functions as consumers.

func (ConsumerF) Accept

func (f ConsumerF) Accept(event E)

Accept calls consumer function passing event to it.

type E

type E interface {

	// Type returns a type of this event.
	// Bus uses returned type to retrieve and notify consumers.
	Type() string
}

E is an interface for event. Only instances of this interface can be published in Bus.

type TmpConsumer

type TmpConsumer interface {
	Consumer

	// IsDone called by bus after accept, if func returns true
	// this consumer will be removed from bus.
	IsDone() bool
}

TmpConsumer allows consumer to be removed from bus each time after accept. Accept is called at least once for such consumers.

Source Files

Jump to

Keyboard shortcuts

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