initial commit

This commit is contained in:
dave 2024-06-15 12:41:13 -07:00
commit b1de6c9246
2 changed files with 49 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.davepedu.com/dave/callticker
go 1.22.0

46
ticker.go Normal file
View 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()
}
}
}