diff --git a/CHANGELOG.md b/CHANGELOG.md index 84f4af6..adc4cc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - +## [0.2.3] - 2023-05-12 + +### Added in 0.2.3 + +- Fix bug (nil observer) + ## [0.2.2] - 2023-05-10 ### Added in 0.2.2 diff --git a/subject/subject_impl.go b/subject/subject_impl.go index 35c138e..2478540 100644 --- a/subject/subject_impl.go +++ b/subject/subject_impl.go @@ -103,10 +103,12 @@ Input */ func (subject *SubjectImpl) RegisterObserver(ctx context.Context, observer observer.Observer) error { var err error = nil - lock.RLock() - defer lock.RUnlock() - if !contains(ctx, subject.observerList, observer) { - subject.observerList = append(subject.observerList, observer) + if observer != nil { + lock.RLock() + defer lock.RUnlock() + if !contains(ctx, subject.observerList, observer) { + subject.observerList = append(subject.observerList, observer) + } } return err } @@ -121,8 +123,10 @@ Input */ func (subject *SubjectImpl) UnregisterObserver(ctx context.Context, observer observer.Observer) error { var err error = nil - lock.RLock() - defer lock.RUnlock() - subject.observerList = removeFromSlice(ctx, subject.observerList, observer) + if observer != nil { + lock.RLock() + defer lock.RUnlock() + subject.observerList = removeFromSlice(ctx, subject.observerList, observer) + } return err }