From dcec92659053a4de8837f20d3fa94ee69f617bbf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 31 Aug 2022 21:24:13 +0530 Subject: [PATCH] Do not allow adding timers before loop is run --- tools/tui/loop/api.go | 12 +++++++++--- tools/tui/loop/run.go | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tools/tui/loop/api.go b/tools/tui/loop/api.go index 576412c8c..e0fb996cc 100644 --- a/tools/tui/loop/api.go +++ b/tools/tui/loop/api.go @@ -72,7 +72,7 @@ type Loop struct { } func New() (*Loop, error) { - l := Loop{controlling_term: nil, timers: make([]*timer, 0)} + l := Loop{controlling_term: nil} l.terminal_options.alternate_screen = true l.escape_code_parser.HandleCSI = l.handle_csi l.escape_code_parser.HandleOSC = l.handle_osc @@ -84,16 +84,22 @@ func New() (*Loop, error) { return &l, nil } -func (self *Loop) AddTimer(interval time.Duration, repeats bool, callback TimerCallback) IdType { +func (self *Loop) AddTimer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) { + if self.timers == nil { + return 0, fmt.Errorf("Cannot add timers before starting the run loop, add them in OnInitialize instead") + } self.timer_id_counter++ t := timer{interval: interval, repeats: repeats, callback: callback, id: self.timer_id_counter} t.update_deadline(time.Now()) self.timers = append(self.timers, &t) self.sort_timers() - return t.id + return t.id, nil } func (self *Loop) RemoveTimer(id IdType) bool { + if self.timers == nil { + return false + } for i := 0; i < len(self.timers); i++ { if self.timers[i].id == id { self.timers = append(self.timers[:i], self.timers[i+1:]...) diff --git a/tools/tui/loop/run.go b/tools/tui/loop/run.go index 4ab769d1a..dd4f28f95 100644 --- a/tools/tui/loop/run.go +++ b/tools/tui/loop/run.go @@ -203,6 +203,7 @@ func (self *Loop) run() (err error) { self.death_signal = SIGNULL self.escape_code_parser.Reset() self.exit_code = 0 + self.timers = make([]*timer, 0, 1) no_timeout_channel := make(<-chan time.Time) finalizer := ""