Documentation
¶
Overview ¶
Package eventually provides a flexible event handling framework that enables applications to react to various types of events dynamically. The core functionality revolves around defining, raising, and handling events that are structured as Go structs.
The Eventually acts as the central hub for event management, allowing events to be raised, handlers to be registered or removed, and events to be dispatched to the appropriate handlers based on the event type. This package leverages Go's reflection capabilities to dynamically handle events according to their type at runtime.
Event ¶
An event in this context is defined as any Go struct. The name and fields of the struct describe the event. Events are used to carry data relevant to each occurrence that handlers might react to.
Events are defined as structs. For example, an event to represent a completed order might look like:
type OrderCompleted struct { OrderID string }
EventHandler ¶
An event handler is a function designed to respond to an Event. Handlers must accept a single parameter that is a struct type representing the event they intend to handle.
Event handlers are functions that take a specific event struct as an argument. Here is how you might handle the OrderCompleted event:
func handleOrderCompleted(event OrderCompleted) { fmt.Println("Order completed:", event.OrderID) }
Eventually ¶
The central struct in the package, responsible for managing the lifecycle of events and their handlers. It provides methods to raise events, add or remove event handlers, and process events through their respective handlers.
Event handlers are registered to an instance of Eventually using the Eventually.React method. This setup allows the Eventually instance to call the handler when the corresponding event type is raised.
Events are raised using the Eventually.RaiseEvent method on an Eventually instance. When an event is raised, all handlers registered for that event type are invoked with the event as a parameter.
The package also provides functionality to store and retrieve an Eventually instance from a context.Context object, facilitating the passing of the event handling framework through request or operation contexts in more complex applications.
This package is ideal for applications that require a modular and dynamic approach to handling events where the types of events are not known at compile time.
Example:
func main() { evtl := &eventually.Eventually{} // Register the event handler evtl.React(handleOrderCompleted) // Raise an event evtl.RaiseEvent(OrderCompleted{OrderID: "1234"}) }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleEvent ¶
func HandleEvent(ctx context.Context, fn EventHandler) error
HandleEvent handle the raised events with the given handler in the eventually in the ctx.
Example ¶
package main import ( "context" "fmt" "github.com/uudashr/eventually" ) func main() { type OrderCompleted struct { OrderID string } var sendOrderCompleteNotification = func(orderID string) { fmt.Printf("Your order %q has been completed\n", orderID) } evtl := &eventually.Eventually{} ctx := eventually.WithContext(context.Background(), evtl) // react immediately eventually.React(ctx, func(event OrderCompleted) { fmt.Printf("Reacting to OrderCompleted event: %q\n", event.OrderID) }) eventually.RaiseEvent(ctx, OrderCompleted{OrderID: "order-123"}) // handle the raised events eventually.HandleEvent(ctx, func(event OrderCompleted) { sendOrderCompleteNotification(event.OrderID) }) }
Output: Reacting to OrderCompleted event: "order-123" Your order "order-123" has been completed
func RaiseEvent ¶
RaiseEvent raise the event to the eventually in the ctx.
func React ¶
func React(ctx context.Context, fn EventHandler) error
React reacting to the event with the given fn as the handler of Eventually in the ctx.
func WithContext ¶
func WithContext(ctx context.Context, evtl *Eventually) context.Context
WithContext return a new context with the evtl.
Types ¶
type Event ¶
type Event any
Event is a type of event. It can be struct of anything represent the event. The struct name describe the event name, the struct fields describe the event data.
Example:
type OrderCompleted struct { OrderID string }
type EventHandler ¶
type EventHandler any
EventHandle is a function type that handle the event.
The form of function is:
func(event Event)
where the Event is the event type (struct) that will be handled.
Example:
eventually.HandleEvent(func(event OrderCompleted) { // handle the event })
type Eventually ¶
type Eventually struct {
// contains filtered or unexported fields
}
Eventually is responsible for managing the lifecycle of events and their handlers.
Example ¶
package main import ( "fmt" "github.com/uudashr/eventually" ) func main() { type OrderCompleted struct { OrderID string } var sendOrderCompleteNotification = func(orderID string) { fmt.Printf("Your order %q has been completed\n", orderID) } evtl := &eventually.Eventually{} // ReaiseEvent accept any kind of struct type evtl.RaiseEvent(OrderCompleted{OrderID: "order-123"}) // HandleEvent accept any kind of func with any struct type as the argument evtl.HandleEvent(func(event OrderCompleted) { sendOrderCompleteNotification(event.OrderID) }) }
Output: Your order "order-123" has been completed
func (*Eventually) Events ¶
func (e *Eventually) Events() []Event
Events return the list of events that have been raised.
func (*Eventually) HandleEvent ¶
func (e *Eventually) HandleEvent(fn EventHandler) error
HandleEvent handle the raised events with the given handler. The fn handler will not be registerd, it will not be called for the new raised event.
func (*Eventually) RaiseEvent ¶
func (e *Eventually) RaiseEvent(event Event) error
RaiseEvent add the event to the list of events.
func (*Eventually) React ¶
func (e *Eventually) React(fn EventHandler)
React reacting to the event with the given fn as the handler. It will only react to the new raised event. Past events will not be handled by the fn handler.
func (*Eventually) RemoveHandler ¶
func (e *Eventually) RemoveHandler(fn EventHandler)
RemoveHandler remove the handler from the list of handlers which previously registed via Eventually.React. Subsequent raised events will not longer be handled by the fn handler.