Debian Codegraph @

internal/config/autosave.go

raw
1 package config
2
3 import (
4 "time"
5 )
6
7 var Autosave chan bool
8 var autotime chan float64
9
10 func init() {
11 Autosave = make(chan bool)
12 autotime = make(chan float64)
13 }
14
15 func SetAutoTime(a float64) {
16 autotime <- a
17 }
18
19 func StartAutoSave() {
20 go func() {
21 var a float64
22 var t *time.Timer
23 var elapsed <-chan time.Time
24 for {
25 select {
26 case a = <-autotime:
27 if t != nil {
28 t.Stop()
29 for len(elapsed) > 0 {
30 <-elapsed
31 }
32 }
33 if a > 0 {
34 if t != nil {
35 t.Reset(time.Duration(a * float64(time.Second)))
36 } else {
37 t = time.NewTimer(time.Duration(a * float64(time.Second)))
38 elapsed = t.C
39 }
40 }
41 case <-elapsed:
42 if a > 0 {
43 t.Reset(time.Duration(a * float64(time.Second)))
44 Autosave <- true
45 }
46 }
47 }
48 }()
49 }