Do not allow adding timers before loop is run

This commit is contained in:
Kovid Goyal 2022-08-31 21:24:13 +05:30
parent 0a2455c8be
commit dcec926590
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 3 deletions

View File

@ -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:]...)

View File

@ -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 := ""