diff --git a/ext/common.go b/ext/common.go index 70604cd..1fc00ec 100644 --- a/ext/common.go +++ b/ext/common.go @@ -12,3 +12,11 @@ func logError(l *slog.Logger, text string, err error) { } l.Error(text, "error", err) } + +// ternary operator approximation. +func iftrue[T any](b bool, t T, f T) T { + if b { + return t + } + return f +} diff --git a/ext/dispatcher.go b/ext/dispatcher.go index 5cd0c84..de2527c 100644 --- a/ext/dispatcher.go +++ b/ext/dispatcher.go @@ -131,17 +131,13 @@ func NewDispatcher(opts *DispatcherOpts) *Dispatcher { processor := Processor(BaseProcessor{}) if opts != nil { - if opts.MaxRoutines != 0 { - maxRoutines = opts.MaxRoutines - } - if opts.Processor != nil { - processor = opts.Processor - } + maxRoutines = iftrue(opts.MaxRoutines != 0, opts.MaxRoutines, maxRoutines) + processor = iftrue(opts.Processor != nil, opts.Processor, processor) + logger = iftrue(opts.Logger != nil, opts.Logger, logger) errHandler = opts.Error panicHandler = opts.Panic unhandledErrFunc = opts.UnhandledErrFunc - logger = opts.Logger } var limiter chan struct{} diff --git a/ext/updater.go b/ext/updater.go index 86efd9e..fbb7c5b 100644 --- a/ext/updater.go +++ b/ext/updater.go @@ -62,8 +62,8 @@ func NewUpdater(dispatcher UpdateDispatcher, opts *UpdaterOpts) *Updater { logger := slog.Default() if opts != nil { + logger = iftrue(opts.Logger == nil, logger, opts.Logger) unhandledErrFunc = opts.UnhandledErrFunc - logger = opts.Logger } return &Updater{