callback

package
v0.0.0-...-bbde943 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2013 License: BSD-3-Clause Imports: 4 Imported by: 27

Documentation

Overview

Package callback provides a mechanism for registering and dispatching arbitrary callbacks.

Index

Constants

View Source
const (
	// Call handlers synchronously on the current goroutine
	DispatchSerial = iota
	// Call each handler on its own goroutine
	DispatchParallel
	// Call each handler on its own goroutine, but wait until all handlers
	// have finished before returning.
	DispatchParallelAndWait
)

Various dispatch behaviors

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackIdentifier

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

type Registry

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

A Registry keeps track of registered callbacks. Callbacks registered with one Registry will not fire when another Registry is dispatched.

func NewRegistry

func NewRegistry(behavior int) *Registry

NewRegistry returns a new *Registry. The passed behavior must be one of the Dispatch* behavior constants.

func (*Registry) AddCallback

func (r *Registry) AddCallback(name string, f interface{}) CallbackIdentifier

AddCallback registers a callback function for a given name and returns an identifier that can be used to remove the callback later. The callback function is an interface{} but a runtime error will be thrown if it is not a func with no return values.

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all callbacks.

func (*Registry) Dispatch

func (r *Registry) Dispatch(name string, args ...interface{}) bool

Dispatch dispatches the given callback with the given args. If a registered handler is incompatible with the args, a panic is thrown. The rationale is the error lies in the code that registers the callback, not the code that dispatches it, but the error can't be detected until dispatch. This assumes, of course, that the same event isn't dispatched with disparate argument types.

If a registered handler has too few arguments, it is only given the arguments it can handle If it has too many arguments, the extra arguments are zero-initialized.

The return value is true if any handlers were invoked, or false otherwise.

func (*Registry) DispatchWithBehavior

func (r *Registry) DispatchWithBehavior(name string, behavior int, args ...interface{}) bool

DispatchWithBehavior is the same as Dispatch, but it dispatches using the given behavior instead of the one provided to New().

func (*Registry) RemoveAllCallbacks

func (r *Registry) RemoveAllCallbacks(name string)

RemoveAllCallbacks removes all callbacks for the given name.

func (*Registry) RemoveCallback

func (r *Registry) RemoveCallback(ident CallbackIdentifier)

RemoveCallback removes a previously-registered callback function using the identifier returned from AddCallback.

Jump to

Keyboard shortcuts

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