This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepeat.go
More file actions
46 lines (39 loc) · 1.26 KB
/
repeat.go
File metadata and controls
46 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package gowait
import (
"time"
)
func ScheduleJobLoop(job func() *time.Time, t time.Time, opts ...repeatOpt) *time.Timer {
d := t.Sub(time.Now())
cfg := (&repeatConfig{}).applyOpts(opts...)
return scheduleJobLoop(job, d, *cfg)
}
// hidden from external
func scheduleJobLoop(job func() *time.Time, t time.Duration, cfg repeatConfig) *time.Timer {
return durationJob(repeatScheduleJob(job, cfg), t, cfg.waitConfig)
}
func repeatScheduleJob(job func() *time.Time, cfg repeatConfig) func() {
return func() {
nextTime := job()
if nextTime != nil {
interval := cfg.applyDuration(nextTime.Sub(time.Now()))
scheduleJobLoop(job, interval, cfg)
}
}
}
func DurationJobLoop(job func() *time.Duration, duration time.Duration, opts ...repeatOpt) *time.Timer {
cfg := (&repeatConfig{}).applyOpts(opts...)
return durationJobLoop(job, duration, *cfg)
}
// hidden from external
func durationJobLoop(job func() *time.Duration, duration time.Duration, r repeatConfig) *time.Timer {
return durationJob(repeatDurationFunc(job, r), duration, r.waitConfig)
}
func repeatDurationFunc(job func() *time.Duration, r repeatConfig) func() {
return func() {
nextTime := job()
if nextTime != nil {
*nextTime = r.applyDuration(*nextTime)
durationJobLoop(job, *nextTime, r)
}
}
}