diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-13 19:42:30 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-13 19:42:30 +0300 |
| commit | 16e413799363871c1efd73527fba299dfdfadfd3 (patch) | |
| tree | 36627063d7d0b80f824cc9abe6848d860b082c3e /internal/eventloop_output.go | |
| parent | 0b454f367374e8cb97927627dacd0f1b216fe5ad (diff) | |
refactor: extract outputFormatter collaborator from eventLoop
The printCb and warningCb function fields on eventLoop bundled two
distinct concerns (pair emission and warning delivery) directly on the
event-processing struct. This commit extracts them into a dedicated
outputFormatter type that owns these callbacks plus emit() and
notifyWarning() helper methods.
outputFormatter is embedded (not pointed-to) in eventLoop so that
existing call sites — including tests that write el.printCb = ... and
el.warningCb = ... directly — require no changes beyond the three
struct-literal sites in eventloop_filter_test.go that used field
initialiser syntax.
fdTracker and commResolver were already proper collaborator types; only
the output concern needed extraction.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/eventloop_output.go')
| -rw-r--r-- | internal/eventloop_output.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/eventloop_output.go b/internal/eventloop_output.go new file mode 100644 index 0000000..9a5a0f9 --- /dev/null +++ b/internal/eventloop_output.go @@ -0,0 +1,44 @@ +package internal + +import "ior/internal/event" + +// outputFormatter bundles the pair-emission and warning-notification callbacks +// used by the event loop. Extracting these two concerns into a dedicated type +// separates "what to do with a completed event pair" and "how to report +// non-fatal problems" from the core event-matching and FD-tracking logic. +// +// The struct is embedded (not pointed-to) inside eventLoop so that existing +// call sites — including tests — can still write e.printCb = ... and +// e.warningCb = ... without any changes. +type outputFormatter struct { + // printCb is called for each completed, filter-passing event pair. + // The callback owns the pair after the call: it must either recycle it + // (ep.Recycle) or hand it off to another owner. + printCb func(ep *event.Pair) + + // warningCb is an optional callback for non-fatal event-processing + // warnings (e.g. malformed events, unresolved comms). nil means silent. + warningCb func(message string) +} + +// emit invokes printCb for the given pair, falling back to a safe recycle-only +// callback when printCb has not been set. This prevents a nil-pointer dereference +// during early initialisation or in tests that do not configure printCb. +func (f *outputFormatter) emit(ep *event.Pair) { + if f.printCb != nil { + f.printCb(ep) + return + } + // Fallback: recycle the pair so it is not leaked even when no callback is wired. + ep.Recycle() +} + +// notifyWarning delivers message to warningCb if one is registered and the +// message is non-empty. Silently drops the message otherwise so callers do not +// need to guard every warning site. +func (f *outputFormatter) notifyWarning(message string) { + if f.warningCb == nil || message == "" { + return + } + f.warningCb(message) +} |
