47 lines
761 B
Go
47 lines
761 B
Go
package callticker
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type CallTicker struct {
|
|
manualTrigger chan interface{}
|
|
function func()
|
|
ticker *time.Ticker
|
|
duration time.Duration
|
|
}
|
|
|
|
func NewCallTicker(d time.Duration, function func()) *CallTicker {
|
|
ticker := &CallTicker{
|
|
manualTrigger: make(chan interface{}, 100),
|
|
function: function,
|
|
ticker: time.NewTicker(d),
|
|
duration: d,
|
|
}
|
|
return ticker
|
|
}
|
|
|
|
func (c *CallTicker) Stop() {
|
|
c.ticker.Stop()
|
|
}
|
|
|
|
func (c *CallTicker) Reset(d time.Duration) {
|
|
c.ticker.Reset(d)
|
|
}
|
|
|
|
func (c *CallTicker) Force() {
|
|
c.manualTrigger <- true
|
|
c.Reset(c.duration)
|
|
}
|
|
|
|
func (c *CallTicker) Run() {
|
|
for {
|
|
select {
|
|
case _ = <-c.ticker.C:
|
|
c.function()
|
|
case _ = <-c.manualTrigger:
|
|
c.function()
|
|
}
|
|
}
|
|
}
|