fix timer drifting off and delte old code
This commit is contained in:
+8
-31
@@ -2,6 +2,7 @@ package event
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"log/slog"
|
||||||
"reflect"
|
"reflect"
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
@@ -21,33 +22,6 @@ type OnceTask struct {
|
|||||||
type Timer struct {
|
type Timer struct {
|
||||||
Once []OnceTask
|
Once []OnceTask
|
||||||
Interval []IntervalTask
|
Interval []IntervalTask
|
||||||
Active bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Timer) Tick() (err error) {
|
|
||||||
if !t.Active {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i, task := range t.Interval {
|
|
||||||
if time.Since(task.LastTrigger) >= task.Interval {
|
|
||||||
task.LastTrigger = time.Now()
|
|
||||||
err = task.F()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Interval[i] = task
|
|
||||||
}
|
|
||||||
for i, task := range t.Once {
|
|
||||||
if task.Trigger.After(time.Now()) {
|
|
||||||
err = task.F()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Once = slices.Delete(t.Once, i, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timer) Trigger(ch chan any) {
|
func (t *Timer) Trigger(ch chan any) {
|
||||||
@@ -76,23 +50,26 @@ func (t *Timer) Trigger(ch chan any) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
time.Sleep(triggerAt.Sub(now))
|
sleepFor := triggerAt.Sub(now)
|
||||||
|
//slog.Debug("sleep next", "dur", sleepFor)
|
||||||
|
time.Sleep(sleepFor)
|
||||||
if iOnce >= 0 {
|
if iOnce >= 0 {
|
||||||
ch <- CallFunc{F: reflect.ValueOf(t.Once[iOnce].F), Args: []reflect.Value{}}
|
ch <- CallFunc{F: reflect.ValueOf(t.Once[iOnce].F), Args: []reflect.Value{}}
|
||||||
t.Once = slices.Delete(t.Once, iOnce, iOnce+1)
|
t.Once = slices.Delete(t.Once, iOnce, iOnce+1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if iInterval >= 0 {
|
if iInterval >= 0 {
|
||||||
|
t.Interval[iInterval].LastTrigger = triggerAt
|
||||||
ch <- CallFunc{F: reflect.ValueOf(t.Interval[iInterval].F), Args: []reflect.Value{}}
|
ch <- CallFunc{F: reflect.ValueOf(t.Interval[iInterval].F), Args: []reflect.Value{}}
|
||||||
t.Interval[iInterval].LastTrigger = now
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
func (t *Timer) Every(interval time.Duration, f func() error) {
|
func (t *Timer) Every(interval time.Duration, f func() error) {
|
||||||
t.Interval = append(t.Interval, IntervalTask{
|
t.Interval = append(t.Interval, IntervalTask{
|
||||||
F: f,
|
F: f,
|
||||||
Interval: interval,
|
Interval: interval,
|
||||||
|
LastTrigger: time.Now(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user