Documentation
¶
Overview ¶
Purpose: This file implements the Spark EventDispatcher subsystem for the GoStack framework. It provides a synchronous and asynchronous Pub/Sub communication channel.
Philosophy: Ecosystem decoupling is critical for clean application code. By dispatching events instead of coupling business components directly, we preserve single-responsibility rules. The dispatcher is written using standard mutexes to ensure high-performance, concurrent-safe registrations and publishing.
Architecture: A standalone framework package (`github.com/Charledeon77/gostack/framework/events`). Implements the `contract.EventDispatcher` interface.
Choice: We chose a map-of-slices design with read/write mutexes rather than a channel-based multiplexer to guarantee immediate synchronous dispatch order when requested (which is crucial for database transactions) while offering clean `DispatchAsync` hooks for non-blocking processes.
Implementation: - Dispatcher: coordinates event listening and firing.
- Listen(): registers a Listener to a specific event category.
- Dispatch(): fires the event executing all handlers synchronously in the same thread.
- DispatchAsync(): fires all registered event handlers in separate background goroutines.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher manages event-listener mappings safely across concurrent HTTP handlers.
func NewDispatcher ¶
func NewDispatcher() *Dispatcher
NewDispatcher initializes an empty, thread-safe Dispatcher instance.
func (*Dispatcher) Dispatch ¶
func (d *Dispatcher) Dispatch(eventName string, event any) error
Dispatch executes all listeners registered to eventName synchronously. If any listener returns an error, execution stops and that error is returned.
func (*Dispatcher) DispatchAsync ¶
func (d *Dispatcher) DispatchAsync(eventName string, event any, errHandler func(error))
DispatchAsync fires all registered listeners in their own concurrent background goroutines. It returns immediately and executes asynchronously, logging errors via a user-definable callback or standard runtime recovery.