Documentation
¶
Overview ¶
Package goevents provides a lightweight, thread-safe event bus for Go.
It allows you to register named events, attach multiple handlers to each event, emit events asynchronously with data and arguments, and manage handlers dynamically.
Features:
- Register and emit named events
- Attach and remove handlers for events
- Pass data and arguments to handlers
- Asynchronous handler execution
- Thread-safe with sync primitives
Typical usage:
package main
import (
"github.com/DoniLite/go-events"
"fmt"
)
func main() {
bus := goevents.NewEventBus()
event := bus.CreateEvent("example:event")
bus.On(event, func(data *goevents.EventData, args ...string) {
fmt.Println("Event received:", data.Message, args)
})
bus.Emit(event, &goevents.EventData{Message: "Hello"}, "arg1")
bus.Wait()
}
Index ¶
- func DecodeDataPayload[T any](data *EventData) (T, bool)
- type Event
- type EventData
- type EventFactory
- func (bus *EventFactory) CreateEvent(eventName string) *Event
- func (bus *EventFactory) Emit(event *Event, data *EventData, args ...string)
- func (bus *EventFactory) Off(event *Event, handler EventHandler)
- func (bus *EventFactory) On(event *Event, handler EventHandler)
- func (bus *EventFactory) Subscribe(fn EventHandler, targetEvents ...*Event)
- func (bus *EventFactory) Wait()
- type EventHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeDataPayload ¶ added in v0.1.2
Decodes the payload of an EventData into the specified type
Types ¶
type EventData ¶
The data associated with an event This data will be passed to the event handlers when the event is emitted
type EventFactory ¶
type EventFactory struct {
// contains filtered or unexported fields
}
EventFactory is responsible for creating and managing events
var (
EventBus *EventFactory
)
func (*EventFactory) CreateEvent ¶
func (bus *EventFactory) CreateEvent(eventName string) *Event
Create an event that can be fire with the event bus
func (*EventFactory) Emit ¶
func (bus *EventFactory) Emit(event *Event, data *EventData, args ...string)
Emit the provided event and call all the registered handler
func (*EventFactory) Off ¶
func (bus *EventFactory) Off(event *Event, handler EventHandler)
Unregister an event handler
func (*EventFactory) On ¶
func (bus *EventFactory) On(event *Event, handler EventHandler)
register an event to the event bus based on the provided event notice that the provided handler will not be replaced or register again if it already exist if you want to replace any function use the replace method or unregistered the handler before
func (*EventFactory) Subscribe ¶ added in v0.1.2
func (bus *EventFactory) Subscribe(fn EventHandler, targetEvents ...*Event)
Subscribe a function to the event bus if you provide some events your handler will be called for all the events But if no event list is provided the handler will be called for all the events
func (*EventFactory) Wait ¶
func (bus *EventFactory) Wait()
Wait until all the registered handler func to be called an executed Notice that this method is not async and calling the internal
WaitGroup.Wait()
Avoid calling this function as possible due to the fact that all handler are called in goroutines But this method can be helpful for testing purposes or if you want to get the result of the handlers
type EventHandler ¶
A function that will be called when an event is emitted