https://github.com/sangianpatrick/emitty
A simple Event Emitter package for Golang application
https://github.com/sangianpatrick/emitty
event-emitter go-channel go-routine golang golang-package worker-threads
Last synced: 5 months ago
JSON representation
A simple Event Emitter package for Golang application
- Host: GitHub
- URL: https://github.com/sangianpatrick/emitty
- Owner: sangianpatrick
- License: mit
- Created: 2019-06-15T12:50:10.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-28T04:01:54.000Z (almost 7 years ago)
- Last Synced: 2025-06-15T20:22:36.001Z (about 1 year ago)
- Topics: event-emitter, go-channel, go-routine, golang, golang-package, worker-threads
- Language: Go
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# emitty
A simple Event Emitter package for Golang application. This package contains both Emitter and Event.
Emitter only has one function "Emit()" that send the data to the listener. Before the listener do its jobs, Event should attach an event name and a handler (to be executed after event is catched) by AttachEvent function, but if the event is needless, it could be detached by DetachEvent function.
## How to install
Using go get :
```go get -u github.com/sangianpatrick/emitty```
Using DEP :
```dep ensure -add github.com/sangianpatrick/emitty```
## How to use
This is the package implementation.
```
package main
import (
"fmt"
"time"
"github.com/sangianpatrick/emitty"
)
var debug bool
func init() {
debug = true
}
func main() {
defer func() {
r := recover()
if r != nil {
fmt.Println("Panic Recovered", r)
}
}()
fmt.Println("Emitty Simple Usage")
signal := emitty.New(debug)
listener := emitty.NewListener(&emitty.Config{
Signal: signal,
NumberOfWorkers: 3,
})
emitter := emitty.NewEmitter(signal)
err := listener.AttachEvent(&emitty.Event{
Name: "printStr",
ActiveOn: time.Now().Add(time.Second * 0),
Expiration: time.Second * 15,
Handler: exampleHandler,
MaxHits: 5,
StartImmediately: true,
})
if err != nil {
panic(err)
}
listener.Start()
time.Sleep(time.Second * 3)
emitter.Emit("printStr", "Hello World\n")
fmt.Scanln()
}
func exampleHandler(args ...interface{}) {
if str, ok := args[0].(string); ok {
fmt.Printf("String: %s", str)
}
}
```
The result will be:
```
Emitty Simple Usage
Emitty [INFO]: Name: Listener | Message: Running on 3 workers | Data
Emitty [INFO]: Name: Listener | Message: Executing handler on event 'printStr' | Data
String: Hello World
Emitty [INFO]: Name: Listener | Message: Event with name 'printStr' has been detached | Data
```