initial commit
This commit is contained in:
commit
b1de6c9246
46
ticker.go
Normal file
46
ticker.go
Normal file
@ -0,0 +1,46 @@
|
||||
package callticker
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type CallTicker struct {
|
||||
manualTrigger chan interface{}
|
||||
function func(...interface{}) interface{}
|
||||
ticker *time.Ticker
|
||||
duration time.Duration
|
||||
}
|
||||
|
||||
func NewCallTicker(d time.Duration, function func(...interface{}) interface{}) *CallTicker {
|
||||
ticker := &CallTicker{
|
||||
manualTrigger: make(chan interface{}),
|
||||
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) Trigger() {
|
||||
c.manualTrigger <- true
|
||||
c.Reset(c.duration)
|
||||
}
|
||||
|
||||
func (c *CallTicker) Run() {
|
||||
for {
|
||||
select {
|
||||
case _ = <-c.ticker.C:
|
||||
c.function()
|
||||
case _ = <-c.manualTrigger:
|
||||
c.function()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user