Skip to content

Commit

Permalink
make sure SetView is called only once, otherwise fail fast
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Sep 15, 2024
1 parent 8b09ef0 commit a5d5d38
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 21 deletions.
6 changes: 4 additions & 2 deletions core/bandmap/bandmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ func (m *Bandmap) Close() {

func (m *Bandmap) SetView(view View) {
if view == nil {
m.view = new(nullView)
return
panic("bandmap.Bandmap.SetView must not be called with nil")
}
if _, ok := m.view.(*nullView); !ok {
panic("bandmap.Bandmap.SetView was already called")
}

m.view = view
Expand Down
7 changes: 5 additions & 2 deletions core/callinfo/callinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ type View interface {

func (c *Callinfo) SetView(view View) {
if view == nil {
c.view = new(nullView)
return
panic("callinfo.Callinfo.SetView must not be called with nil")
}
if _, ok := c.view.(*nullView); !ok {
panic("callinfo.Callinfo.SetView was already called")
}

c.view = view
c.view.SetPredictedExchangeFields(c.theirExchangeFields)
}
Expand Down
11 changes: 5 additions & 6 deletions core/entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ func (c *Controller) emitCallsignLogged(callsign string, frequency core.Frequenc

func (c *Controller) SetView(view View) {
if view == nil {
c.view = &nullView{}
return
panic("entry.Controller.SetView must not be called with nil")
}
if _, ok := c.view.(*nullView); !ok {
panic("entry.Controller.SetView was already called")
}

c.view = view
c.Clear()
c.refreshUTC()
Expand Down Expand Up @@ -301,10 +304,6 @@ func (c *Controller) StartAutoRefresh() {

func (c *Controller) refreshUTC() {
c.asyncRunner(func() {
if c.view == nil {
return
}

utc := c.clock.Now().UTC()
c.view.SetUTC(utc.Format("15:04"))
})
Expand Down
7 changes: 5 additions & 2 deletions core/newcontest/newcontest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ type Controller struct {

func (c *Controller) SetView(view View) {
if view == nil {
c.view = new(nullview)
return
panic("newcontest.Controller.SetView must not be called with nil")
}
if c.view != nil {
panic("newcontest.Controller.SetView was already called")
}

c.view = view
}

Expand Down
11 changes: 8 additions & 3 deletions core/parrot/parrot.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ func (p *Parrot) Notify(listener any) {
}

func (p *Parrot) SetView(view View) {
p.view = view
if view != nil {
view.SetParrotActive(p.active)
if view == nil {
panic("parrot.Parrot.SetView must not be called with nil")
}
if p.view != nil {
panic("parrot.Parrot.SetView was already called")
}

p.view = view
view.SetParrotActive(p.active)
}

func (p *Parrot) SetInterval(interval time.Duration) {
Expand Down
7 changes: 5 additions & 2 deletions core/rate/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ func (c *Counter) StartAutoRefresh() {

func (c *Counter) SetView(view View) {
if view == nil {
c.view = new(nullView)
return
panic("rate.Counter.SetView must not be called with nil")
}
if _, ok := c.view.(*nullView); !ok {
panic("rate.Counter.SetView was already called")
}

c.view = view
c.view.SetGoals(c.qsosGoal, c.pointsGoal, c.multisGoal)
c.view.ShowRate(c.QSORate)
Expand Down
7 changes: 5 additions & 2 deletions core/score/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ func (c *Counter) Result() int {

func (c *Counter) SetView(view View) {
if view == nil {
c.view = new(nullView)
return
panic("score.Counter.SetView must not be called with nil")
}
if _, ok := c.view.(*nullView); !ok {
panic("score.Counter.SetView was already called")
}

c.view = view
c.view.SetGoals(c.contestPointsGoal, c.contestMultisGoal)
c.view.ShowScore(c.readScore) // READ
Expand Down
7 changes: 5 additions & 2 deletions core/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,12 @@ func (s *Settings) SetWriter(writer Writer) {

func (s *Settings) SetView(view View) {
if view == nil {
s.view = new(nullView)
return
panic("settings.Settings.SetView must not be called with nil")
}
if _, ok := s.view.(*nullView); !ok {
panic("settings.Settings.SetView was already called")
}

s.view = view
s.showSettings()
}
Expand Down

0 comments on commit a5d5d38

Please sign in to comment.