event

package
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2016 License: GPL-3.0 Imports: 5 Imported by: 0

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 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