Simplify timer removal

This commit is contained in:
Kovid Goyal 2022-08-31 21:37:43 +05:30
parent dcec926590
commit 457aab7c41
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 6 additions and 14 deletions

View File

@ -41,7 +41,7 @@ type Loop struct {
keep_going bool
death_signal unix.Signal
exit_code int
timers []*timer
timers, timers_temp []*timer
timer_id_counter, write_msg_id_counter IdType
wakeup_channel chan byte
pending_writes []*write_msg
@ -72,7 +72,7 @@ type Loop struct {
}
func New() (*Loop, error) {
l := Loop{controlling_term: nil}
l := Loop{controlling_term: nil, timers_temp: make([]*timer, 4)}
l.terminal_options.alternate_screen = true
l.escape_code_parser.HandleCSI = l.handle_csi
l.escape_code_parser.HandleOSC = l.handle_osc

View File

@ -9,8 +9,9 @@ import (
func (self *Loop) dispatch_timers(now time.Time) error {
updated := false
remove := make(map[IdType]bool, 0)
for _, t := range self.timers {
self.timers_temp = self.timers_temp[:0]
self.timers_temp = append(self.timers_temp, self.timers...)
for i, t := range self.timers_temp {
if now.After(t.deadline) {
err := t.callback(t.id)
if err != nil {
@ -20,19 +21,10 @@ func (self *Loop) dispatch_timers(now time.Time) error {
t.update_deadline(now)
updated = true
} else {
remove[t.id] = true
self.timers = append(self.timers[:i], self.timers[i+1:]...)
}
}
}
if len(remove) > 0 {
timers := make([]*timer, len(self.timers)-len(remove))
for _, t := range self.timers {
if !remove[t.id] {
timers = append(timers, t)
}
}
self.timers = timers
}
if updated {
self.sort_timers()
}