go-vitess.v1: gopkg.in/src-d/go-vitess.v1/event Index | Files | Directories

package event

import "gopkg.in/src-d/go-vitess.v1/event"

Package event provides a reflect-based framework for low-frequency global dispatching of events, which are values of any arbitrary type, to a set of listener functions, which are usually registered by plugin packages during init().

Listeners should do work in a separate goroutine if it might block. Dispatch should be called synchronously to make sure work enters the listener's work queue before moving on. After Dispatch returns, the listener is responsible for arranging to flush its work queue before program termination if desired.

For example, any package can define an event type:

package mypackage

type MyEvent struct {
	field1, field2 string
}

Then, any other package (e.g. a plugin) can listen for those events:

package myplugin

import (
	"event"
	"mypackage"
)

func onMyEvent(ev mypackage.MyEvent) {
	// do something with ev
}

func init() {
	event.AddListener(onMyEvent)
}

Any registered listeners that accept a single argument of type MyEvent will be called when a value of type MyEvent is dispatched:

package myotherpackage

import (
	"event"
	"mypackage"
)

func InMediasRes() {
	ev := mypackage.MyEvent{
		field1: "foo",
		field2: "bar",
	}

	event.Dispatch(ev)
}

In addition, listener functions that accept an interface type will be called for any dispatched value that implements the specified interface. A listener that accepts interface{} will be called for every event type. Listeners can also accept pointer types, but they will only be called if the dispatch site calls Dispatch() on a pointer.

Index

Package Files

event.go hooks.go

func AddListener Uses

func AddListener(fn interface{})

AddListener registers a listener function that will be called when a matching event is dispatched. The type of the function's first (and only) argument declares the event type (or interface) to listen for.

func Dispatch Uses

func Dispatch(ev interface{})

Dispatch sends an event to all registered listeners that were declared to accept values of the event's type, or interfaces that the value implements.

func DispatchUpdate Uses

func DispatchUpdate(ev Updater, update interface{})

DispatchUpdate calls Update() on the event and then dispatches it. This is a shortcut for combining updates and dispatches into a single call.

type BadListenerError Uses

type BadListenerError string

BadListenerError is raised via panic() when AddListener is called with an invalid listener function.

func (BadListenerError) Error Uses

func (why BadListenerError) Error() string

type Hooks Uses

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

Hooks holds a list of parameter-less functions to call whenever the set is triggered with Fire().

func (*Hooks) Add Uses

func (h *Hooks) Add(f func())

Add appends the given function to the list to be triggered.

func (*Hooks) Fire Uses

func (h *Hooks) Fire()

Fire calls all the functions in a given Hooks list. It launches a goroutine for each function and then waits for all of them to finish before returning. Concurrent calls to Fire() are serialized.

type Updater Uses

type Updater interface {
    // Update is called by DispatchUpdate() before the event is dispatched.
    Update(update interface{})
}

Updater is an interface that events can implement to combine updating and dispatching into one call.

Directories

PathSynopsis
sysloggerPackage syslogger uses the event package to listen for any event that implements the Syslogger interface.

Package event imports 3 packages (graph) and is imported by 12 packages. Updated 2019-06-13. Refresh now. Tools for package owners.