Skip to content

Commit

Permalink
Immediately notify observer when registered
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Oct 5, 2023
1 parent 7f06caa commit 838f19b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 12 additions & 7 deletions internal/pkg/agent/application/upgrade/details/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ func (d *Details) Fail(err error) {
}

// RegisterObserver allows an interested consumer of Details to register
// themselves as an Observer. When NotifyObservers is called, all registered
// observers are sent the current upgrade details.
// themselves as an Observer. The registered observer is immediately notified
// of the current upgrade details.
func (d *Details) RegisterObserver(observer Observer) {
d.mu.Lock()
defer d.mu.Unlock()

d.observers = append(d.observers, observer)
d.notifyObserver(observer)
}

// NotifyObservers sends the current upgrade details to all registered
Expand All @@ -81,10 +82,14 @@ func (d *Details) NotifyObservers() {

func (d *Details) notifyObservers() {
for _, observer := range d.observers {
if d.State == StateCompleted {
observer(nil)
} else {
observer(d)
}
d.notifyObserver(observer)
}
}

func (d *Details) notifyObserver(observer Observer) {
if d.State == StateCompleted {
observer(nil)
} else {
observer(d)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func TestDetailsObserver(t *testing.T) {

var observedDetails *Details
obs := func(updatedDetails *Details) { observedDetails = updatedDetails }

det.RegisterObserver(obs)
require.Nil(t, observedDetails)
require.Len(t, det.observers, 1)
require.NotNil(t, observedDetails)
require.Equal(t, StateRequested, observedDetails.State)

det.SetState(StateDownloading)
require.Equal(t, StateDownloading, det.State)
Expand Down

0 comments on commit 838f19b

Please sign in to comment.