========
An event-emitter package for Go
versions >= 1.2, this package is inspired by javascript
event-emitter library .
=======
Mohammed Al Ashaal, just a full-stack developer
.
i'm on twitter, facebook and github
============
go get github.com/alash3al/goemitter
====================
package main
import(
"fmt"
"github.com/alash3al/goemitter"
)
func main(){
// just a shortcut
echo := fmt.Println
// initialize a new instance ?
emitter := Emitter.Construct()
// register a new listener for an event
// Yep, the listener must be in this template "func(...interface{})"
// the args are the arguments passed to the listener
emitter.AddListener("myevent", func(args ... interface{}){
echo("this is the listener argument -> ", args[0])
})
// just an alias of AddListener
emitter.On(/* ... */)
// how about registering a one time listener
// that will be removed after the first run
emitter.Once("myevent", func(args ...interface{}){
echo("this will run just once")
})
// run all event-listeners 'in synchronous mode'
// and pass nil as arguments list
// anything else, the arguments must be []interface{}
emitter.EmitSync("myevent", nil)
// anything else, the arguments must be []interface{}
// i.e
emitter.EmitSync("myevent", []interface{}{"first arg", "second arg"})
// but i want to emit listeners using goroutines !
// ok, don't panic() ;)
emitter.EmitAsync(/* the same as EmitSync, just change to EmitAsync :) */)
// defining a listener
fn := func(args ...interface{}){
// your code
}
// register
emitter.On("myevent", fn)
// now remove it
emitter.RemoveListener("myevent", fn)
// remove all listeners from an event ?
emitter.RemoveAllListeners("myevent")
// remove all listeners from all events ?
emitter.RemoveAllListeners()
// return a slice with "Listener" struct of an event ?
// What is Listener struct? - just continue, don't worry
emitter.Listeners("myevent") // nil if empty
// return the count of listeners of an event
emitter.ListenersCount("myevent")
// now lets know about the internal structs
// 1)- Emitter
// It contains a map of event => listeners
// src ? - ok
type Emitter struct {
listeners map[interface{}][]Listener
}
// 2)- Listener
// When you register a callback
// its automatically passed in a new `Listener`
// and whether it is a one-time listener or not
type Listener struct {
callback func(...interface{})
once bool
}
}