micro/internal/config/autosave.go

46 lines
600 B
Go
Raw Normal View History

2019-08-05 00:22:24 +03:00
package config
import (
2019-08-05 00:32:42 +03:00
"sync"
2019-08-05 00:22:24 +03:00
"time"
)
var Autosave chan bool
2019-08-05 00:32:42 +03:00
var autotime int
// lock for autosave
var autolock sync.Mutex
2019-08-05 00:22:24 +03:00
func init() {
Autosave = make(chan bool)
}
2019-08-05 00:32:42 +03:00
func SetAutoTime(a int) {
autolock.Lock()
autotime = a
autolock.Unlock()
}
func GetAutoTime() int {
autolock.Lock()
a := autotime
autolock.Unlock()
return a
}
2019-08-05 00:22:24 +03:00
func StartAutoSave() {
go func() {
for {
if autotime < 1 {
break
}
2019-08-05 00:32:42 +03:00
time.Sleep(time.Duration(autotime) * time.Second)
// it's possible autotime was changed while sleeping
if autotime < 1 {
break
}
2019-08-05 00:22:24 +03:00
Autosave <- true
}
}()
}