event

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2020 License: GPL-3.0 Imports: 3 Imported by: 1

Documentation

Overview

Package event implements an event bus. for a great introduction to the event bus pattern in go, see: https://levelup.gitconnected.com/lets-write-a-simple-event-bus-in-go-79b9480d8997

Example
const (
	ETMainSaidHello   = Topic("main:SaidHello")
	ETMainOpSucceeded = Topic("main:OperationSucceeded")
	ETMainOpFailed    = Topic("main:OperationFailed")
)

ctx, done := context.WithCancel(context.Background())
bus := NewBus(ctx)
ch1 := bus.Subscribe(ETMainSaidHello)
ch2 := bus.Subscribe(ETMainSaidHello)
ch3 := bus.Subscribe(ETMainSaidHello)

go bus.Publish(ETMainSaidHello, "hello")

tasks := 3

for {
	select {
	case d := <-ch1:
		fmt.Println(d.Payload)
	case d := <-ch2:
		fmt.Println(d.Payload)
	case d := <-ch3:
		fmt.Println(d.Payload)
	}

	tasks--
	if tasks == 0 {
		break
	}
}

opCh := bus.SubscribeOnce(ETMainOpSucceeded, ETMainOpFailed)

go bus.Publish(ETMainOpFailed, fmt.Errorf("it didn't work?"))

event := <-opCh
fmt.Println(event.Payload)
done()
Output:

hello
hello
hello
it didn't work?

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ETFSICreateLinkEvent type for when FSI creates a link between a dataset and working directory
	ETFSICreateLinkEvent = Topic("fsi:createLinkEvent")
)

Functions

This section is empty.

Types

type Bus

type Bus interface {
	// Publish an event to the bus
	Publish(t Topic, data interface{})
	// Subscribe to one or more topics
	Subscribe(topics ...Topic) <-chan Event
	// Unsubscribe cleans up a channel that no longer need to receive events
	Unsubscribe(<-chan Event)
	// SubscribeOnce to one or more topics. the returned channel will only fire
	// once, when the first event that matches any of the given topics
	// the common use case for multiple subscriptions is subscribing to both
	// success and error events
	SubscribeOnce(types ...Topic) <-chan Event
	// NumSubscriptions returns the number of subscribers to the bus's events
	NumSubscribers() int
}

Bus is a central coordination point for event publication and subscription zero or more subscribers register topics to be notified of, a publisher writes a topic event to the bus, which broadcasts to all subscribers of that topic

func NewBus

func NewBus(ctx context.Context) Bus

NewBus creates a new event bus. Event busses should be instantiated as a singleton. If the passed in context is cancelled, the bus will stop emitting events and close all subscribed channels

TODO (b5) - finish context-closing cleanup

type Event

type Event struct {
	Topic
	Payload interface{}
}

Event is a topic & data payload

type FSICreateLinkEvent

type FSICreateLinkEvent struct {
	FSIPath  string
	Username string
	Dsname   string
}

FSICreateLinkEvent describes an FSI created link

type NilPublisher added in v0.9.6

type NilPublisher struct {
}

NilPublisher replaces a nil value, does nothing

func (*NilPublisher) Publish added in v0.9.6

func (n *NilPublisher) Publish(t Topic, data interface{})

Publish does nothing with the event

type Publisher added in v0.9.6

type Publisher interface {
	Publish(t Topic, data interface{})
}

Publisher is an interface that can only publish an event

type Topic

type Topic string

Topic is the set of all topics emitted by the bus. Use the topic type to distinguish event names. Event emitters should declare Topics as constants and document the expected data payload type

Jump to

Keyboard shortcuts

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