event

package
v5.5.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2018 License: GPL-3.0 Imports: 5 Imported by: 62

Documentation

Overview

Package event implements an event multiplexer.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMuxClosed = errors.New("event: mux closed")

ErrMuxClosed is returned when Posting on a closed TypeMux.

Functions

This section is empty.

Types

type Event added in v1.3.1

type Event struct {
	Time time.Time
	Data interface{}
}

Event is a time-tagged notification pushed to subscribers.

type Feed

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

Feed implements one-to-many subscriptions where the carrier of events is a channel. Values sent to a Feed are delivered to all subscribed channels simultaneously.

Feeds can only be used with a single type. The type is determined by the first Send or Subscribe operation. Subsequent calls to these methods panic if the type does not match.

The zero value is ready to use.

func (*Feed) Send

func (f *Feed) Send(value interface{}) (nsent int)

Send delivers to all subscribed channels simultaneously. It returns the number of subscribers that the value was sent to.

func (*Feed) Subscribe

func (f *Feed) Subscribe(channel interface{}) Subscription

Subscribe adds a channel to the feed. Future sends will be delivered on the channel until the subscription is canceled. All channels added must have the same element type.

The channel should have ample buffer space to avoid blocking other subscribers. Slow subscribers are not dropped.

type Subscription

type Subscription interface {
	// Chan returns a channel that carries events.
	// Implementations should return the same channel
	// for any subsequent calls to Chan.
	Chan() <-chan *Event

	// Unsubscribe stops delivery of events to a subscription.
	// The event channel is closed.
	// Unsubscribe can be called more than once.
	Unsubscribe()
}

Subscription is implemented by event subscriptions.

type TypeMux

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

A TypeMux dispatches events to registered receivers. Receivers can be registered to handle events of certain type. Any operation called after mux is stopped will return ErrMuxClosed.

The zero value is ready to use.

Example
type someEvent struct{ I int }
type otherEvent struct{ S string }
type yetAnotherEvent struct{ X, Y int }

var mux TypeMux

// Start a subscriber.
done := make(chan struct{})
sub := mux.Subscribe(someEvent{}, otherEvent{})
go func() {
	for event := range sub.Chan() {
		fmt.Printf("Received: %#v\n", event.Data)
	}
	fmt.Println("done")
	close(done)
}()

// Post some events.
mux.Post(someEvent{5})
mux.Post(yetAnotherEvent{X: 3, Y: 4})
mux.Post(someEvent{6})
mux.Post(otherEvent{"whoa"})

// Stop closes all subscription channels.
// The subscriber goroutine will print "done"
// and exit.
mux.Stop()

// Wait for subscriber to return.
<-done
Output:

Received: event.someEvent{I:5}
Received: event.someEvent{I:6}
Received: event.otherEvent{S:"whoa"}
done

func (*TypeMux) Post

func (mux *TypeMux) Post(ev interface{}) error

Post sends an event to all receivers registered for the given type. It returns ErrMuxClosed if the mux has been stopped.

func (*TypeMux) Stop

func (mux *TypeMux) Stop()

Stop closes a mux. The mux can no longer be used. Future Post calls will fail with ErrMuxClosed. Stop blocks until all current deliveries have finished.

func (*TypeMux) Subscribe

func (mux *TypeMux) Subscribe(types ...interface{}) Subscription

Subscribe creates a subscription for events of the given types. The subscription's channel is closed when it is unsubscribed or the mux is closed.

Directories

Path Synopsis
Package filter implements event filters.
Package filter implements event filters.

Jump to

Keyboard shortcuts

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