Skip to content

baderkha/goevent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goevent

goevent is an event emitter similar to javascript's custom events.

Note :

This is not a pattern you should have inside your code all over the place . You should use this package for specific use cases. Some of those use cases are listed below.

Use Case :

  • Init / Shutdown events for your rest api
  • Async Processing
  • PUB->SUB Local events
  • etc ...

Contents

Installation

go get github.com/baderkha/goevent

Usage

This section will cover exemplar scenarios for this package. This package can be used to emit global events you can subscribe to anywhere in the code , or local events if you want to manage / orchestrate the events.

Global

For Global Events you first need to init the global emitter as such

  1. Init

    // somewhere in your main func
    func main() {
        // IF YOU DO NOT DO THIS , THIS WILL CRASH
    
        // choose to have your panics handled gracefully
        handlePanics := true 
        goevent.InitGlobal(handlePanics)
    }
  2. Add Event Listener

    // somewhere else
    listenerHash := goevent.
            Global().
            AddListener("slim_shady", func(d interface{}) {
                name := d.(string)
                fmt.Println("BAND NAME => " + name)
            })
  3. Emit Event

    // somewhere else
    goevent.
            Global().
            Emit("slim_shady","d12")
    
    // with the event emitted above this will  output
    // "BAND NAME => d12"
  4. Remove Event Listener

    // somewhere else
    hasBeenRemoved := goevent.
           Global().
           RemoveListener(listenerHash)
    // will be false if it doesn't exist
    fmt.Println(hasBeenRemoved)

Local

For Local events you can construct them from any where and attach them to structs

  1. Init

    // from anywhere
    
    
    handlePanics := true // choose to have your panics handled 
    
    // this object is local ie everytime you call new you get a new one
    ev := goevent.New(handlePanics) 
  2. Add Event Listener

    listenerHash:= ev.AddListener("slim_shady", func(d interface{}) {
                    name := d.(string)
                    fmt.Println("BAND NAME => " + name)
                })
  3. Emit Event

    ev.Emit("slim_shady","d12")
  4. Remove Event Listener

    ev.RemoveListener(listenerHash)

About

Event Emitter Similar to JS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages