]> git.lizzy.rs Git - micro.git/blob - internal/config/autosave.go
Fix minor autosave race condition
[micro.git] / internal / config / autosave.go
1 package config
2
3 import (
4         "sync"
5         "time"
6 )
7
8 var Autosave chan bool
9 var autotime int
10
11 // lock for autosave
12 var autolock sync.Mutex
13
14 func init() {
15         Autosave = make(chan bool)
16 }
17
18 func SetAutoTime(a int) {
19         autolock.Lock()
20         autotime = a
21         autolock.Unlock()
22 }
23
24 func GetAutoTime() int {
25         autolock.Lock()
26         a := autotime
27         autolock.Unlock()
28         return a
29 }
30
31 func StartAutoSave() {
32         go func() {
33                 for {
34                         if autotime < 1 {
35                                 break
36                         }
37                         time.Sleep(time.Duration(autotime) * time.Second)
38                         // it's possible autotime was changed while sleeping
39                         if autotime < 1 {
40                                 break
41                         }
42                         Autosave <- true
43                 }
44         }()
45 }