Very simple event library in go.
- Import the library
import ( maple "github.com/weavc/maple/pkg" )
- Create a new Event
event := maple.NewEvent("maple.events.foo")
- Register your handlers
event.Register(func(ev maple.Event, v interface{}) { // do something })
- Start emitting events
event.Emit(payload)
When you register functions to an event, these must implement our HandleEventFunc
type.
type HandleEventFunc func(event Event, args interface{})
When registered to an event, the function is added to a stack of methods that will be called everytime the event is triggered. When called it will be passed the Event
structure/interface calling it and the value that is provided when the event is triggered.
The library leaves how the event is handled to the handlers themselves. This allows the handler to decide how it wants to handle the event. i.e.
- Spawn a go routine
func eventHandler(event pkg.Event, v interface{}) {
go func() {
// do stuff
}()
}
- Type the args
func eventHandler(event pkg.Event, v interface{}) {
s, valid := v.(string)
if !valid {
panic(fmt.Errorf("invalid args provided. Expected %s", "*ApiEventArgs"))
}
}