mirror of
https://github.com/ChuckNorrison/LightningTipBot.git
synced 2026-08-13 12:33:14 +02:00
* speed up tx lock * add shops * shop * scrolling * 🚀 add state stateCallbackMessage 🚀 * no shop * scroll * add shop browser * scroll bug * add buttons.go * scrolling * shop * lnurl print after serving * shop deletion * adding and removing shops * display photo captions * refactor * shops * shops * files work * rename shop * error handling * fixes * payments * confirm delete all shops * adding new items fix * own bunt db for shop * unlock transaction * remove double print * add transaction.Lock(id) * unlock getShop * add default interceptors to shop * more files * go sleep * add fileStateResetTicker * async sleep * add runtime.ResettableFunctionTicker * remove default duration * generalized concurrent mutex * simplify function ticker * status message ticker queue * add start index to shopViewDeleteAllStatusMsgs Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> Co-authored-by: lngohumble <lngohumble@gmail.com>
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package runtime
|
|
|
|
import (
|
|
cmap "github.com/orcaman/concurrent-map"
|
|
"time"
|
|
)
|
|
|
|
var tickerMap cmap.ConcurrentMap
|
|
|
|
func init() {
|
|
tickerMap = cmap.New()
|
|
}
|
|
|
|
var defaultTickerCoolDown = time.Second * 10
|
|
|
|
// ResettableFunctionTicker will reset the user state as soon as tick is delivered.
|
|
type ResettableFunctionTicker struct {
|
|
Ticker *time.Ticker
|
|
ResetChan chan struct{} // channel used to reset the ticker
|
|
duration time.Duration
|
|
Started bool
|
|
name string
|
|
}
|
|
type ResettableFunctionTickerOption func(*ResettableFunctionTicker)
|
|
|
|
func WithDuration(d time.Duration) ResettableFunctionTickerOption {
|
|
return func(a *ResettableFunctionTicker) {
|
|
a.duration = d
|
|
}
|
|
}
|
|
func RemoveTicker(name string) {
|
|
tickerMap.Remove(name)
|
|
}
|
|
func GetTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker {
|
|
|
|
if t, ok := tickerMap.Get(name); ok {
|
|
return t.(*ResettableFunctionTicker)
|
|
} else {
|
|
t := NewResettableFunctionTicker(name, option...)
|
|
tickerMap.Set(name, t)
|
|
return t
|
|
}
|
|
}
|
|
func NewResettableFunctionTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker {
|
|
t := &ResettableFunctionTicker{
|
|
ResetChan: make(chan struct{}, 1),
|
|
name: name,
|
|
}
|
|
|
|
for _, opt := range option {
|
|
opt(t)
|
|
}
|
|
if t.duration == 0 {
|
|
t.duration = defaultTickerCoolDown
|
|
}
|
|
t.Ticker = time.NewTicker(t.duration)
|
|
return t
|
|
}
|
|
|
|
func (t *ResettableFunctionTicker) Do(f func()) {
|
|
t.Started = true
|
|
tickerMap.Set(t.name, t)
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-t.Ticker.C:
|
|
// ticker delivered signal. do function f
|
|
f()
|
|
return
|
|
case <-t.ResetChan:
|
|
// reset signal received. creating new ticker.
|
|
t.Ticker = time.NewTicker(t.duration)
|
|
}
|
|
}
|
|
}()
|
|
}
|