Skip to content

alash3al/goemitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emitter

========

An event-emitter package for Go versions >= 1.2, this package is inspired by javascript event-emitter library .

Author

======= Mohammed Al Ashaal, just a full-stack developer . i'm on twitter, facebook and github

Installation

============

go get github.com/alash3al/goemitter

Learn By Examples ?

====================

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

About

an event-emitter for golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages