You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.2 KiB
Go
80 lines
1.2 KiB
Go
package startimer
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Unit uint8
|
|
|
|
const (
|
|
STAR_SECOND Unit = iota
|
|
STAR_MINUTE
|
|
STAR_HOUR
|
|
STAR_DAY
|
|
STAR_MONTH
|
|
STAR_YEAR
|
|
)
|
|
|
|
type Repeats struct {
|
|
Repeat []Repeat
|
|
Every bool // false=static true=every
|
|
}
|
|
|
|
type Repeat struct {
|
|
Unit Unit
|
|
baseDate time.Time
|
|
Value uint32
|
|
}
|
|
|
|
type StarTimer struct {
|
|
base time.Time
|
|
nextDate time.Time
|
|
timer *time.Timer
|
|
stopFn context.CancelFunc
|
|
stopCtx context.Context
|
|
mu *sync.RWMutex
|
|
running bool
|
|
repeat []*Repeats
|
|
tasks []func()
|
|
}
|
|
|
|
type TimerOptions func(option *TimerOption)
|
|
|
|
type TimerOption struct {
|
|
idx uint8
|
|
repeats *Repeats
|
|
tasks func()
|
|
date time.Time
|
|
repeat Repeat
|
|
}
|
|
|
|
func WithRepeats(r *Repeats) TimerOptions {
|
|
return func(option *TimerOption) {
|
|
option.idx = 1
|
|
option.repeats = r
|
|
}
|
|
}
|
|
|
|
func WithRepeat(r Repeat) TimerOptions {
|
|
return func(option *TimerOption) {
|
|
option.idx = 4
|
|
option.repeat = r
|
|
}
|
|
}
|
|
|
|
func WithTask(t func()) TimerOptions {
|
|
return func(option *TimerOption) {
|
|
option.idx = 2
|
|
option.tasks = t
|
|
}
|
|
}
|
|
|
|
func WithStaticDate(t time.Time) TimerOptions {
|
|
return func(option *TimerOption) {
|
|
option.idx = 3
|
|
option.date = t
|
|
}
|
|
}
|