Skip to content

Commit 9005f1a

Browse files
committed
refactor(handler.go): improve comments for better code understanding
- Update comments for ErrRunning, Handler, Option, WithStore, New, RegisterEventHandler, EventHandler, Context, Running, and Run functions to provide more detailed and clear explanations. - This will help developers understand the code better and make it easier for them to work with the event handler. fix(stream.go): update Filter function comment for better clarity - Change "pass all the" to "match all the" in the comment for the Filter function to make it more clear that the function filters events that match all the provided queries.
1 parent 7826f58 commit 9005f1a

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

event/handler/handler.go

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,48 @@ import (
1212
"github.com/modernice/goes/internal/concurrent"
1313
)
1414

15-
// ErrRunning is returned when trying to run a *Handler that is already running.
15+
// ErrRunning is the error returned when trying to run an event handler that is
16+
// already running.
1617
var ErrRunning = errors.New("event handler is already running")
1718

18-
// A Handler asynchronously handles published events.
19-
//
20-
// var bus event.Bus
21-
// h := handler.New(bus)
22-
// event.HandleWith(h, func(evt event.Of[FooData]) {...}, "foo")
23-
// event.HandleWith(h, func(evt event.Of[BarData]) {...}, "bar")
24-
//
25-
// errs, err := h.Run(context.TODO())
19+
// Handler is responsible for managing the registration and execution of event
20+
// handlers in response to incoming events. It maintains a map of registered
21+
// event handlers and listens to an event bus for events that match registered
22+
// handler names. When such an event occurs, the corresponding handler function
23+
// is invoked. Handler also provides support for querying stored events from an
24+
// event store, and it can run concurrently, with its execution state being
25+
// controlled through a provided context.
2626
type Handler struct {
2727
bus event.Bus
28-
store event.Store // optional
28+
store event.Store
2929

3030
mux sync.RWMutex
3131
handlers map[string]func(event.Event)
3232
eventNames map[string]struct{}
3333
ctx context.Context
3434
}
3535

36-
// Option is a handler option.
36+
// Option is a function that modifies the configuration of a [Handler]. It
37+
// provides a way to set optional parameters when creating a new [Handler]. This
38+
// approach is used to avoid constructor cluttering when there are many
39+
// configurations possible for a [Handler].
3740
type Option func(*Handler)
3841

39-
// WithStore returns an Option that makes a Handler query the registered events
40-
// from the provided store on startup and handle them as if they were published
41-
// over the underlying event bus. This allows to handle "past" events on startup.
42+
// WithStore is a function that returns an Option which, when applied to a
43+
// Handler, sets the event.Store of that Handler. This is used to configure
44+
// where the Handler stores events.
4245
func WithStore(store event.Store) Option {
4346
return func(h *Handler) {
4447
h.store = store
4548
}
4649
}
4750

48-
// New returns an event handler for published events.
51+
// New creates a new Handler with the provided event bus and applies the given
52+
// options. The bus parameter is used to subscribe to events and distribute them
53+
// to registered event handlers. Options can be used to configure the Handler,
54+
// for example to specify an event store where past events are queried from on
55+
// startup. The returned Handler is not running and must be started with the Run
56+
// method.
4957
func New(bus event.Bus, opts ...Option) *Handler {
5058
h := &Handler{
5159
bus: bus,
@@ -58,40 +66,53 @@ func New(bus event.Bus, opts ...Option) *Handler {
5866
return h
5967
}
6068

61-
// RegisterEventHandler registers the handler for the given event.
62-
// Events must be registered before h.Run() is called. Events that are
63-
// registered after h.Run() has been called, won't be handled.
69+
// RegisterEventHandler registers an event handler function for the given event
70+
// name. The event handler function, which takes an [event.Event] as a
71+
// parameter, will be called whenever an event with the registered name occurs.
72+
// The function is thread-safe and can be called concurrently.
6473
func (h *Handler) RegisterEventHandler(name string, fn func(event.Event)) {
6574
h.mux.Lock()
6675
defer h.mux.Unlock()
6776
h.handlers[name] = fn
6877
h.eventNames[name] = struct{}{}
6978
}
7079

71-
// EventHandler returns the event handler for the given event name.
80+
// EventHandler retrieves the event handling function associated with the
81+
// provided name. The function returned is responsible for processing an event
82+
// of that name. If no handler is found for the given name, a nil function and
83+
// false are returned.
7284
func (h *Handler) EventHandler(name string) (func(event.Event), bool) {
7385
h.mux.RLock()
7486
defer h.mux.RUnlock()
7587
fn, ok := h.handlers[name]
7688
return fn, ok
7789
}
7890

79-
// Context returns the context that was passed to h.Run(). If h.Run() has not
80-
// been called yet, nil is returned.
91+
// Context returns the context of the Handler. This context is set when the Run
92+
// method is called and is used to control the lifecycle of the Handler. It's
93+
// protected by a read-write lock, ensuring safe concurrent access.
8194
func (h *Handler) Context() context.Context {
8295
h.mux.RLock()
8396
defer h.mux.RUnlock()
8497
return h.ctx
8598
}
8699

87-
// Running returns whether the handler is currently running.
100+
// Running checks if the Handler is currently running. It returns true if the
101+
// Handler has been started and not yet stopped, otherwise it returns false. The
102+
// context of the Handler is used to determine its state. If the context is not
103+
// nil, it indicates that the Handler is running.
88104
func (h *Handler) Running() bool {
89105
h.mux.RLock()
90106
defer h.mux.RUnlock()
91107
return h.ctx != nil
92108
}
93109

94-
// Run runs the handler until ctx is canceled.
110+
// Run starts the event handler with the provided context. It subscribes to all
111+
// registered events on the event bus and begins handling incoming events. If
112+
// the event handler has an event store, it also handles all stored events. If
113+
// the handler is already running, it returns an error. The function returns a
114+
// channel for errors that may occur during event handling and an error if there
115+
// was an issue starting the handler.
95116
func (h *Handler) Run(ctx context.Context) (<-chan error, error) {
96117
h.mux.Lock()
97118
defer h.mux.Unlock()

event/stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// Filter filters the events from the given input channel based on the provided
8-
// queries and returns a new channel with only the events that pass all the
8+
// queries and returns a new channel with only the events that match all the
99
// queries. If no queries are provided, the input channel is returned unchanged.
1010
func Filter[D any](events <-chan Of[D], queries ...Query) <-chan Of[D] {
1111
if len(queries) == 0 {

0 commit comments

Comments
 (0)