From a5d5d38606a394fc0c2c95300a5e5836a25d78e6 Mon Sep 17 00:00:00 2001 From: Florian Thienel Date: Sun, 15 Sep 2024 15:50:21 +0200 Subject: [PATCH] make sure SetView is called only once, otherwise fail fast --- core/bandmap/bandmap.go | 6 ++++-- core/callinfo/callinfo.go | 7 +++++-- core/entry/entry.go | 11 +++++------ core/newcontest/newcontest.go | 7 +++++-- core/parrot/parrot.go | 11 ++++++++--- core/rate/rate.go | 7 +++++-- core/score/score.go | 7 +++++-- core/settings/settings.go | 7 +++++-- 8 files changed, 42 insertions(+), 21 deletions(-) diff --git a/core/bandmap/bandmap.go b/core/bandmap/bandmap.go index 2f1662c..45c4d37 100644 --- a/core/bandmap/bandmap.go +++ b/core/bandmap/bandmap.go @@ -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 diff --git a/core/callinfo/callinfo.go b/core/callinfo/callinfo.go index 721f55c..a1866f5 100644 --- a/core/callinfo/callinfo.go +++ b/core/callinfo/callinfo.go @@ -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) } diff --git a/core/entry/entry.go b/core/entry/entry.go index 4b4a7b9..30981e7 100644 --- a/core/entry/entry.go +++ b/core/entry/entry.go @@ -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() @@ -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")) }) diff --git a/core/newcontest/newcontest.go b/core/newcontest/newcontest.go index 0a8eb90..3619bb1 100644 --- a/core/newcontest/newcontest.go +++ b/core/newcontest/newcontest.go @@ -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 } diff --git a/core/parrot/parrot.go b/core/parrot/parrot.go index 451f147..913bb56 100644 --- a/core/parrot/parrot.go +++ b/core/parrot/parrot.go @@ -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) { diff --git a/core/rate/rate.go b/core/rate/rate.go index d21be31..5e7c252 100644 --- a/core/rate/rate.go +++ b/core/rate/rate.go @@ -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) diff --git a/core/score/score.go b/core/score/score.go index f928baa..77bd6ff 100644 --- a/core/score/score.go +++ b/core/score/score.go @@ -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 diff --git a/core/settings/settings.go b/core/settings/settings.go index 26bc7ef..8d31309 100644 --- a/core/settings/settings.go +++ b/core/settings/settings.go @@ -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() }