Thanks to visit codestin.com
Credit goes to github.com

Skip to content

burgrp/go-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

EventBus

EventBus is a lightweight, type-safe event dispatching library for Go. It allows registering listeners for specific event types and dispatching events in a structured, concurrency-safe way.

Features

  • Type-matching listeners using reflect.Type
  • Supports multi-parameter listener functions
  • Safe event dispatching via locking
  • Optional debug logging using slog
  • Lightweight and dependency-free

Installation

go get github.com/yourusername/event

API

NewEventBus(name string) *EventBus

Creates a new event bus with the given name.

SetLogger(logger *slog.Logger)

Sets an optional logger for debug output.

Unlock()

Unlocks the bus, preventing further listener registration and allowing event dispatching.

Listen(fn interface{})

Registers a listener function. Panics if registration fails.

MayListen(fn interface{}) error

Safe variant of Listen that returns an error instead of panicking.

Send(event interface{})

Dispatches an event. Only listeners matching the event type will be invoked.

Design Notes

  • Listeners can be multi-parameter functions; they will be triggered only when all required event types have been received.
  • The event bus must be unlocked before sending events. Listener registration is disabled afterward to ensure consistency.

License

MIT

About

Event dispatching library for Go

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages