Documentation
¶
Overview ¶
Package eventually provides a flexible event handling library that enables applications to react to various types of events dynamically. The core functionality revolves around defining, publishing, and handling events.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithPub ¶
ContextWithPub wraps the pub and makes it available to the returned context.Context.
Types ¶
type Event ¶
type Event any
Event represents specific event. It must be a struct type.
Example:
type OrderCompleted struct {
OrderID string
}
type EventHandler ¶
type EventHandler any
EventHandle is a function for event handling.
The form of function is:
func(event Event)
where the Event represent the event (struct).
Event handling never fails, no error is returned.
Example:
pubMux := &eventually.PubMux{}
pubMux.React(func(event OrderCompleted) {
// handle the event
})
type PubMux ¶
type PubMux struct {
// contains filtered or unexported fields
}
PubMux is a multiplexer for events. It will route events to its handlers.
Example ¶
// Event definition
type OrderCompleted struct {
OrderID string
}
// Setup the PubMux
mux := new(eventually.PubMux)
ctx := eventually.ContextWithPub(context.Background(), mux)
// React to the events
mux.React(func(event OrderCompleted) {
fmt.Printf("OrderCompleted{OrderID:%s} \n", event.OrderID)
})
// Publish event through Reactor inside the context
eventually.Publish(ctx, OrderCompleted{OrderID: "123"})
Output: OrderCompleted{OrderID:123}
func (*PubMux) Publish ¶
Publish the event.
The event needs to be a valid Event, otherwise it will panic.
func (*PubMux) React ¶
func (pm *PubMux) React(fn EventHandler)
React will handle the published event.
It will handle only the event type defined by the fn. The fn needs to be a valid EventHandler, otherwise it will panic. Multiple handlers can be registered for the same event type.
type Publisher ¶
type Publisher interface {
// Publish event.
Publish(Event)
}
Publisher publishes Event.
type Recorder ¶
type Recorder struct {
Events []Event
}
Recorder records the events.
The events will be recorded the the Events slice.
Example ¶
// Event definition
type OrderCompleted struct {
OrderID string
}
// Setup Recorder
rec := new(eventually.Recorder)
ctx := eventually.ContextWithPub(context.Background(), rec)
// Publish event through Recorder inside the context
eventually.Publish(ctx, OrderCompleted{OrderID: "123"})
eventually.Publish(ctx, OrderCompleted{OrderID: "456"})
// Print the recorded events
fmt.Printf("Record %d events\n", len(rec.Events))
for i, event := range rec.Events {
fmt.Printf("%d. %+v\n", i+1, event)
}
Output: Record 2 events 1. {OrderID:123} 2. {OrderID:456}