From bd2c76a5c8a766890b1a1d30707503ee16157cb3 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Tue, 15 Aug 2023 13:45:03 +0200 Subject: [PATCH] fix: #6 --- change-handler.go | 6 +++--- error-handler.go | 23 +++++++++++++++++++---- error.go | 4 ++-- postprocessing-handler.go | 6 +++--- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/change-handler.go b/change-handler.go index 721a745..6568215 100644 --- a/change-handler.go +++ b/change-handler.go @@ -22,13 +22,13 @@ func (s *Settings[C]) OnChange(hook ChangeHook) *Settings[C] { } // HasOnChangeHook returns true if there are registered hooks. -func (s *Settings[C]) HasOnChangeHook(hook ChangeHook) *Settings[C] { +func (s *Settings[C]) HasOnChangeHook(hook ChangeHook) bool { for _, h := range s.hooks.change { if h == hook { - break + return true } } - return s + return false } // RemoveOnChangeHook removes a change hook from the list of hooks. diff --git a/error-handler.go b/error-handler.go index 92af915..ebcb52c 100644 --- a/error-handler.go +++ b/error-handler.go @@ -18,13 +18,13 @@ func (s *Settings[C]) OnError(hook ErrorHook) *Settings[C] { } // HasOnErrorHook returns true if there are registered hooks. -func (s *Settings[C]) HasOnErrorHook(hook ErrorHook) *Settings[C] { +func (s *Settings[C]) HasOnErrorHook(hook ErrorHook) bool { for _, h := range s.hooks.error { if h == hook { - break + return true } } - return s + return false } // RemoveOnErrorHook removes a change hook from the list of hooks. @@ -39,8 +39,23 @@ func (s *Settings[C]) RemoveOnErrorHook(hook ErrorHook) *Settings[C] { } func (s *Settings[C]) notifyErrorHooks() *Settings[C] { + + if len(s.errors) == 0 { + return s + } + + if len(s.hooks.error) == 0 { + return s + } + + errors := make([]error, len(s.errors)) + copy(errors, s.Errors()) + s.ResetErrors() + for _, h := range s.hooks.error { - go h.Handle(ErrorEvent{}) + go h.Handle(ErrorEvent{ + Errors: errors, + }) } return s } diff --git a/error.go b/error.go index 4ee39a6..d097951 100644 --- a/error.go +++ b/error.go @@ -8,14 +8,14 @@ import ( "reflect" ) -// ResetError is used to reset the error to nil +// ResetErrors is used to reset the error to nil // After calling this function, the call HasErrors() will return false func (s *Settings[C]) ResetErrors() *Settings[C] { s.errors = []error{} return s } -// Check if the setting contains errors +// HasErrors checks if the setting contains errors func (s *Settings[C]) HasErrors() bool { return len(s.errors) > 0 } diff --git a/postprocessing-handler.go b/postprocessing-handler.go index 912750a..92e72a8 100644 --- a/postprocessing-handler.go +++ b/postprocessing-handler.go @@ -22,13 +22,13 @@ func (s *Settings[C]) OnPostprocessing(hook PostprocessingHook) *Settings[C] { } // HasOnPostprocessingHook returns true if there are registered hooks. -func (s *Settings[C]) HasOnPostprocessingHook(hook PostprocessingHook) *Settings[C] { +func (s *Settings[C]) HasOnPostprocessingHook(hook PostprocessingHook) bool { for _, h := range s.hooks.postprocessing { if h == hook { - break + return true } } - return s + return false } // RemoveOnPostprocessingHook removes a change hook from the list of hooks. -- GitLab