Emitter

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: MIT Imports: 3 Imported by: 9

README

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)
	
    // listen to all events
	emitter.On("**", fn)

    // listen to events based on widlcard
	emitter.On("my*", 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
	}
}

Documentation

Overview

An EventEmitter package for go 1.2 + Author: Mohammed Al Ashaal License: MIT License Version: v1.0.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Emitter

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

Emitter - our listeners container

func Construct

func Construct() *Emitter

Construct() - create a new instance of Emitter

func (*Emitter) AddListener

func (self *Emitter) AddListener(event string, callback func(...interface{})) *Emitter

AddListener() - register a new listener on the specified event

func (*Emitter) Destruct

func (self *Emitter) Destruct()

Destruct() - free memory from an emitter instance

func (*Emitter) EmitAsync

func (self *Emitter) EmitAsync(event string, args []interface{}) *Emitter

EmitAsync() - run all listeners of the specified event in asynchronous mode using goroutines

func (*Emitter) EmitSync

func (self *Emitter) EmitSync(event string, args ...interface{}) *Emitter

EmitSync() - run all listeners of the specified event in synchronous mode

func (*Emitter) Listeners

func (self *Emitter) Listeners(event string) []Listener

Listeners() - return an array with the registered listeners in the specified event

func (*Emitter) ListenersCount

func (self *Emitter) ListenersCount(event string) int

ListenersCount() - return the count of listeners in the speicifed event

func (*Emitter) On

func (self *Emitter) On(event string, callback func(...interface{})) *Emitter

On() - register a new listener on the specified event

func (*Emitter) Once

func (self *Emitter) Once(event string, callback func(...interface{})) *Emitter

Once() - register a new one-time listener on the specified event

func (*Emitter) RemoveAllListeners

func (self *Emitter) RemoveAllListeners(event interface{}) *Emitter

RemoveAllListeners() - remove all listeners from (all/event)

func (*Emitter) RemoveListener

func (self *Emitter) RemoveListener(event string, callback func(...interface{})) *Emitter

RemoveListeners() - remove the specified callback from the specified events' listeners

type Listener

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

Listener - our callback container and whether it will run once or not

Jump to

Keyboard shortcuts

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