mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-25 17:52:02 +02:00
Make the loop API a thin wrapper around internal methods
This commit is contained in:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user