A lightweight, thread-safe event bus for Go, supporting asynchronous event handling and handler management.
- Register and emit named events
- Attach multiple handlers to events
- Remove handlers dynamically
- Pass data and arguments to handlers
- Asynchronous handler execution
- Thread-safe with
sync.Mutexandsync.WaitGroup
go get github.com/DoniLite/go-eventspackage main
import (
"github.com/DoniLite/go-events"
"fmt"
)
func main() {
// Create or use the default event bus
bus := goevents.NewEventBus()
// Create an event
event := bus.CreateEvent("user:created")
// Register a handler
bus.On(event, func(data *goevents.EventData, args ...string) {
fmt.Println("User created:", data.Message, args)
})
// Emit the event
bus.Emit(event, &goevents.EventData{Message: "Alice"}, "extraArg")
bus.Wait() // Wait for all handlers to finish
}Event: Represents an event with a name.EventData: Data passed to handlers.EventHandler: Handler function signature.EventFactory: The event bus.
CreateEvent(name string) *Event: Register or get an event.On(event *Event, handler EventHandler): Register a handler.Off(event *Event, handler EventHandler): Remove a handler.Emit(event *Event, data *EventData, args ...string): Emit an event asynchronously.Wait(): Wait for all handlers to complete.Subscribe(fn EventHandler, targetEvents ...*Event): Subscribe an event handler for the target events if no targets is provided the handler is registered for all eventsNewEventBus() *EventFactory: Create a new event bus instance.DecodeDataPayload[T any](data *EventData) (T, bool): Decode an event data payload to the target type
Run all tests:
go test ./...