goevents

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

README

go-events

A lightweight, thread-safe event bus for Go, supporting asynchronous event handling and handler management.

Features

  • 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.Mutex and sync.WaitGroup

Installation

go get github.com/DoniLite/go-events

Usage

package 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
}

API

Types
  • Event: Represents an event with a name.
  • EventData: Data passed to handlers.
  • EventHandler: Handler function signature.
  • EventFactory: The event bus.
Functions
  • 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 events
  • NewEventBus() *EventFactory: Create a new event bus instance.
  • DecodeDataPayload[T any](data *EventData) (T, bool): Decode an event data payload to the target type

Testing

Run all tests:

go test ./...

License

Apache License

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeDataPayload added in v0.1.2

func DecodeDataPayload[T any](data *EventData) (T, bool)

Decodes the payload of an EventData into the specified type

Types

type Event

type Event struct {
	Name string
}

A single event in the event bus

type EventData

type EventData struct {
	Message string
	Payload any
}

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 NewEventBus

func NewEventBus() *EventFactory

Creates a new instance of 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

type EventHandler func(event *EventData, args ...string)

A function that will be called when an event is emitted

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL