eventemitter

package module
v0.0.0-...-3e42a20 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2012 License: MIT Imports: 1 Imported by: 7

README

An EventEmitter for Go

Build Status: Build Status by goci.me

Install

With go get:

% go get github.com/CHH/eventemitter

Usage

For more information please also see the Package Docs.

A new EventEmitter is created by the New function.

import "github.com/CHH/eventemitter"

func main() {
    emitter := eventemitter.New()
}

A listener is of type func (event *ee.Event) Listeners can be bound to event names with the On method:

emitter.On("foo", func(name string) {
    fmt.Printf("Hello World %s", name)
})

An event can be triggered by calling the Emit method:

<- emitter.Emit("foo", "John")

When Emit is called, each registered listener is called in its own Goroutine. They all share a common channel, which is returned by the Emit function.

var c chan interface{} 

c = emitter.Emit("foo", "John")

This channel can be used to wait until all listeners have finished, by using the <- operator without variable:

<- emitter.Emit("foo", "John")

Each listener yields a reference to eventemitter.Response on the channel once it has finished:

c := emitter.Emit("foo", "John")

for resp := <- c {
    // Do something
}
Embedding

EventEmitters can also be embedded in other types. When embedding you've to call the Init function on the EventEmitter, so the memory is correctly allocated:

type Server struct {
    eventemitter.EventEmitter
}

func NewServer() *Server {
    s := new(Server)

    // Allocates the EventEmitter's memory.
    s.EventEmitter.Init()

    // All functions of the EventEmitter are available:
    s.On("foo", func() {
        
    })
}

License

EventEmitter is distributed under the Terms of the MIT License. See the bundled file LICENSE.txt for more information.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventEmitter

type EventEmitter struct {
	// contains filtered or unexported fields
}
Example
// Construct a new EventEmitter instance
emitter := New()

emitter.On("hello", func() {
	fmt.Println("Hello World")
})

emitter.On("hello", func() {
	fmt.Println("Hello Hello World")
})

// Wait until all handlers have finished
<-emitter.Emit("hello")
Output:

Hello World
Hello Hello World

func New

func New() *EventEmitter

func (*EventEmitter) AddListener

func (self *EventEmitter) AddListener(event string, listener interface{})

AddListener adds an event listener on the given event name.

func (*EventEmitter) Emit

func (self *EventEmitter) Emit(event string, argv ...interface{}) <-chan *Response

Emits the given event. Puts all arguments following the event name into the Event's `Argv` member. Returns a channel if listeners were called, nil otherwise.

Example
emitter := New()

emitter.On("hello", func(name string) {
	fmt.Printf("Hello World %s\n", name)
})

<-emitter.Emit("hello", "John")
Output:

Hello World John

func (*EventEmitter) Init

func (self *EventEmitter) Init()

Allocates the EventEmitters memory. Has to be called when embedding an EventEmitter in another Type.

Example
package main

type SampleServer struct {
	EventEmitter
}

func NewServer() *Server {
	s := new(Server)

	// Initialize Maps
	s.EventEmitter.Init()
	return s
}

func main() {
	s := NewServer()

	// Do something

	s.Emit("connect" /*, conn */)
}
Output:

func (*EventEmitter) Listeners

func (self *EventEmitter) Listeners(event string) []reflect.Value

func (*EventEmitter) On

func (self *EventEmitter) On(event string, listener interface{})

Alias to AddListener.

func (*EventEmitter) RemoveListeners

func (self *EventEmitter) RemoveListeners(event string)

Removes all listeners from the given event.

type Response

type Response struct {
	// Name of the Event
	EventName string

	// Slice of all the handler's return values
	Ret []interface{}
}

Jump to

Keyboard shortcuts

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