forked from things-labs/wheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
60 lines (50 loc) · 1.34 KB
/
timer.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package wheel
import (
"time"
)
// Timer consists of a schedule and the func to execute on that schedule.
type Timer struct {
// Next and previous pointers in the doubly-linked list of elements.
// To simplify the implementation, internally a list l is implemented
// as a ring, such that &l.root is both the next element of the last
// list element (l.Back()) and the previous element of the first list
// element (l.Front()).
next, prev *Timer
// The list to which this element belongs.
list *list
// next time the job will run, or the zero time if Base has not been
// started or this entry is unsatisfiable
nextTime time.Time
// job is the thing that want to run.
job Job
// use goroutine
useGoroutine bool
}
// NewTimer new a timer with a empty job,
func NewTimer() *Timer {
return &Timer{
job: emptyJob{},
}
}
// NewJob new timer with job.
func NewJob(job Job) *Timer {
return NewTimer().WithJob(job)
}
// NewJobFunc new timer with job function.
func NewJobFunc(f func()) *Timer {
return NewTimer().WithJobFunc(f)
}
// WithGoroutine with goroutine
func (sf *Timer) WithGoroutine() *Timer {
sf.useGoroutine = true
return sf
}
// WithJob with job.
func (sf *Timer) WithJob(job Job) *Timer {
sf.job = job
return sf
}
// WithJobFunc with job function
func (sf *Timer) WithJobFunc(f func()) *Timer {
return sf.WithJob(JobFunc(f))
}