Pure nodejs EventEmmiter for the Go Programming Language.


Build Status Awesome GoLang Report A+ License Releases Read me docs Chat

Simple EventEmmiter for Go Programming Language. Inspired by Nodejs EventEmitter.

Overview

New() EventEmmiter // New returns a new, empty, EventEmmiter

// AddListener is an alias for .On(eventName, listener).
AddListener(EventName, ...Listener)
// Emit fires a particular event,
// Synchronously calls each of the listeners registered for the event named
// eventName, in the order they were registered,
// passing the supplied arguments to each.
Emit(EventName, ...interface{})
// EventNames returns an array listing the events for which the emitter has registered listeners.
// The values in the array will be strings.
EventNames() []EventName
// GetMaxListeners returns the max listeners for this emmiter
// see SetMaxListeners
GetMaxListeners() int
// ListenerCount returns the length of all registered listeners to a particular event
ListenerCount(EventName) int
// Listeners returns a copy of the array of listeners for the event named eventName.
Listeners(EventName) []Listener
// On registers a particular listener for an event, func receiver parameter(s) is/are optional
On(EventName, ...Listener)
// Once adds a one time listener function for the event named eventName.
// The next time eventName is triggered, this listener is removed and then invoked.
Once(EventName, ...Listener)
// RemoveAllListeners removes all listeners, or those of the specified eventName.
// Note that it will remove the event itself.
// Returns an indicator if event and listeners were found before the remove.
RemoveAllListeners(EventName) bool
// Clear removes all events and all listeners, restores Events to an empty value
Clear()
// SetMaxListeners obviously this function allows the MaxListeners
// to be decrease or increase. Set to zero for unlimited
SetMaxListeners(int)
// Len returns the length of all registered events
Len() int
import "github.com/kataras/go-events"

// initialize a new EventEmmiter to use
e := events.New()

// register an event with name "my_event" and one listener
e.On("my_event", func(payload ...interface{}) {
  message := payload[0].(string)
  print(message) // prints "this is my payload"
})

// fire the 'my_event' event
e.Emit("my_event", "this is my payload")

Default/global EventEmmiter

// register an event with name "my_event" and one listener to the global(package level) default EventEmmiter
events.On("my_event", func(payload ...interface{}) {
  message := payload[0].(string)
  print(message) // prints "this is my payload"
})

// fire the 'my_event' event
events.Emit("my_event", "this is my payload")

Remove an event

events.On("my_event", func(payload ...interface{}) {
  // first listener...
},func (payload ...interface{}){
  // second listener...
})

println(events.Len()) // prints 1
println(events.ListenerCount("my_event")) // prints 2

// Remove our event, when/if we don't need this or we want to clear all of its listeners
events.RemoveAllListeners("my_event")

println(events.Len()) // prints 0
println(events.ListenerCount("my_event")) // prints 0

Installation

The only requirement is the Go Programming Language.

$ go get -u github.com/kataras/go-events

FAQ

Explore these questions or navigate to the community chat.

Versioning

Current: v0.0.2

Read more about Semantic Versioning 2.0.0

People

The author of go-events is @kataras.

If you're willing to donate, feel free to send any amount through paypal

Contributing

If you are interested in contributing to the go-events project, please make a PR.

License

This project is licensed under the MIT License.

License can be found here.

Owner
Gerasimos (Makis) Maropoulos
🥇 That Greek Gopher | 💨 Senior Backend Engineer at PNOĒ | 🎓My dream is to create an international IT university that will produce flawless developers!
Gerasimos (Makis) Maropoulos
Comments
  • Fix map race condition in Emit

    Fix map race condition in Emit

    This fixes the race condition in Emit. Because Emit now read locks the map I also had to change Once so that it doesn't deadlock. If you don't like the remove in a separate go-routine, another option would be check for the oneTime type in Emit and remove it at the end.

  • Emit reads eventlisteners in a non-thread-safe way

    Emit reads eventlisteners in a non-thread-safe way

    Even though AddListener protects the map on write, Emit does not protect it on read. This can result in panics, as seen below:

    fatal error: concurrent map read and map write
    
    goroutine 2929 [running]:
    runtime.throw(0xf7c620, 0x21)
            /home/plorenz/.gvm/gos/go1.14.7/src/runtime/panic.go:1116 +0x72 fp=0xc001757c60 sp=0xc001757c30 pc=0x438c82
    runtime.mapaccess1_faststr(0xe453a0, 0xc0003aa360, 0xf6d2b3, 0x13, 0x10)
            /home/plorenz/.gvm/gos/go1.14.7/src/runtime/map_faststr.go:21 +0x43c fp=0xc001757cd0 sp=0xc001757c60 pc=0x41717c
    github.com/kataras/go-events.(*emmiter).Emit(0xc0003ddc60, 0xf6d2b3, 0x13, 0xc00174a260, 0x1, 0x1)
            /home/plorenz/work/nf/build/pkg/mod/github.com/kataras/[email protected]/events.go:148 +0x5c fp=0xc001757d20 sp=0xc001757cd0 pc=0xaba60c
    github.com/openziti/edge/gateway/internal/fabric.(*StateManagerImpl).AddSession(0xc000136360, 0xc0015c1480)
            /home/plorenz/work/nf/build/pkg/mod/github.com/openziti/[email protected]/gateway/internal/fabric/manager.go:149 +0x3ad fp=0xc001757e50 sp=0xc001757d20 pc=0xd0604d
    github.com/openziti/edge/gateway/handler_edge_ctrl.(*sessionAddedHandler).HandleReceive.func1(0xc0019404b0, 0xc0003b01e0)
            /home/plorenz/work/nf/build/pkg/mod/github.com/openziti/[email protected]/gateway/handler_edge_ctrl/sessionAdded.go:48 +0x1af fp=0xc001757fd0 sp=0xc001757e50 pc=0xd0bbff
    runtime.goexit()
            /home/plorenz/.gvm/gos/go1.14.7/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc001757fd8 sp=0xc001757fd0 pc=0x4699f1
    created by github.com/openziti/edge/gateway/handler_edge_ctrl.(*sessionAddedHandler).HandleReceive
            /home/plorenz/work/nf/build/pkg/mod/github.com/openziti/[email protected]/gateway/handler_edge_ctrl/sessionAdded.go:43 +0x49
    
  • Fix wrong payload for Once handler.

    Fix wrong payload for Once handler.

    To Once's listeners, there are all arguments passed as array in first argument, what is obviously wrong.

    Although this issue was introduced in 6e0eeb3dcfa58085193392580e9db163f08c5304, it was never part of a release until recently.

    Please after merging this fix also create new tag so that it can be properly distributed using golang dependencies.

  • Calling the RemoveListener event in the Emit event will cause a deadlock

    Calling the RemoveListener event in the Emit event will cause a deadlock

    Calling the RemoveListener event in the Emit event will cause a deadlock:

    foo := func(...any){
    }
    event.On("test", func(...any){
        event.RemoveListener("other",foo)
    })
    
    event.Emit("test")
    
  • Add RemoveListener for root emitter

    Add RemoveListener for root emitter

    This'll add the 'RemoveListener' function for the root emitter. You can remove listeners when you create a new emitter, but this library supports an emitter out of the box, I like to call it the 'root' emitter.

    There was no way to remove listeners on that emitter in a simple way. And most functions have a wrapper foor the root emitter. So I've created one for the RemoveListener.

A serverless cluster computing system for the Go programming language

Bigslice Bigslice is a serverless cluster data processing system for Go. Bigslice exposes composable API that lets the user express data processing ta

Dec 14, 2022
Becca - A simple dynamic language for exploring language design

Becca A simple dynamic language for exploring language design What is Becca Becc

Aug 15, 2022
Extract text from plaintext, .docx, .odt and .rtf files. Pure go.

cat This is a simple libary to extract text from plaintext, .docx, .odt, .pdf and .rtf files. Install go get -u github.com/lu4p/cat Basic Usage packag

Nov 18, 2022
Chronos - A static race detector for the go language
Chronos - A static race detector for the go language

Chronos Chronos is a static race detector for the Go language written in Go. Quick Start: Download the package go get -v github.com/amit-davidson/Chro

Dec 12, 2022
A template repository to quickly scaffold a Kubewarden policy written with Go language

go-policy-template This is a template repository that can be used to to quickly scaffold a Kubewarden policy written with Go language. Don't forget to

Sep 7, 2022
Not another markup language. Framework for replacing Kubernetes YAML with Go.

Not another markup language. Replace Kubernetes YAML with raw Go! Say so long ?? to YAML and start using the Go ?? programming language to represent a

Jan 3, 2023
Common Expression Language -- specification and binary representation

The Common Expression Language (CEL) implements common semantics for expression evaluation, enabling different applications to more easily interoperate.

Jan 8, 2023
A cross-language distributed transaction manager
A cross-language distributed transaction manager

English Docs 跨语言分布式事务管理器 DTM是一款golang开发的分布式事务管理器,解决了跨数据库、跨服务、跨语言栈更新数据的一致性问题。 他优雅

Dec 31, 2022
🥑 Language focused docker images, minus the operating system.

"Distroless" Docker Images "Distroless" images contain only your application and its runtime dependencies. They do not contain package managers, shell

Jan 9, 2023
A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

Mar 9, 2022
Github-language-trends - Github trending languages API

Github trending languages API This API provides list of most popular github lang

Feb 15, 2022
helm-lint-ls is helm lint language server protocol LSP.

helm-lint-ls is helm lint language server protocol LSP.

Dec 27, 2022
An Oracle Cloud (OCI) Pulumi resource package, providing multi-language access to OCI

Oracle Cloud Infrastructure Resource Provider The Oracle Cloud Infrastructure (OCI) Resource Provider lets you manage OCI resources. Installing This p

Dec 2, 2022
Gopherscript is a secure and minimal scripting language written in Go.
Gopherscript is a secure and minimal scripting language written in Go.

Gopherscript Gopherscript is a secure scripting/configuration language written in Go. It features a fined-grain permission system and enforces a stron

Oct 2, 2022
T# Programming Language. Something like Porth, Forth but written in Go. Stack-oriented programming language.

The T# Programming Language WARNING! THIS LANGUAGE IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! Something like Forth a

Jun 29, 2022
Yayx programming language is begginer friendly programming language.
Yayx programming language is begginer friendly programming language.

Yayx Yayx programming language is begginer friendly programming language. What have yayx: Easy syntax Dynamic types Can be compiled to outhers program

Dec 27, 2021
Yayx programming language is begginer friendly programming language.

Yayx Yayx programming language is begginer friendly programming language. What have yayx: Easy syntax Dynamic types Can be compiled to outhers program

May 20, 2022
GoRequest -- Simplified HTTP client ( inspired by nodejs SuperAgent )
GoRequest -- Simplified HTTP client ( inspired by nodejs SuperAgent )

GoRequest GoRequest -- Simplified HTTP client ( inspired by famous SuperAgent lib in Node.js ) "Shooting Requests like a Machine Gun" - Gopher Sending

Jan 1, 2023
go-playground-converter is formatter error response inspiration like express-validator in nodejs build on top go-playground-validator.

Go Playground Converter go-playground-converter is formatter error response inspiration like express-validator in nodejs build on top in go-playground

Dec 9, 2022
Nada is a JS runtime, just like Nodejs. The difference is that Nada allows JS developers to easily achieve millions of concurrent applications.

Nada is a JS runtime, just like Nodejs. The difference is that Nada allows JS developers to easily achieve millions of concurrent applications. It also adds some new enhancements to THE JS syntax (types, interfaces, generics) that fundamentally address JS's perennial complaints.

Jul 11, 2022