Make the loop API a thin wrapper around internal methods

This commit is contained in:
Kovid Goyal
2022-09-01 12:14:03 +05:30
parent 7325921070
commit 67115530b4
3 changed files with 56 additions and 35 deletions

View File

@@ -3,10 +3,36 @@
package loop
import (
"fmt"
"sort"
"time"
)
func (self *Loop) add_timer(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, nil
}
func (self *Loop) remove_timer(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:]...)
return true
}
}
return false
}
func (self *Loop) dispatch_timers(now time.Time) error {
updated := false
self.timers_temp = self.timers_temp[:0]