mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-25 18:07:07 +09:00
config: Rework autosave to be rearmed upon change
This commit is contained in:
@@ -356,9 +356,9 @@ func main() {
|
|||||||
log.Println(clipErr, " or change 'clipboard' option")
|
log.Println(clipErr, " or change 'clipboard' option")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.StartAutoSave()
|
||||||
if a := config.GetGlobalOption("autosave").(float64); a > 0 {
|
if a := config.GetGlobalOption("autosave").(float64); a > 0 {
|
||||||
config.SetAutoTime(a)
|
config.SetAutoTime(a)
|
||||||
config.StartAutoSave()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
screen.Events = make(chan tcell.Event)
|
screen.Events = make(chan tcell.Event)
|
||||||
|
|||||||
@@ -553,7 +553,6 @@ func doSetGlobalOptionNative(option string, nativeValue interface{}) error {
|
|||||||
} else if option == "autosave" {
|
} else if option == "autosave" {
|
||||||
if nativeValue.(float64) > 0 {
|
if nativeValue.(float64) > 0 {
|
||||||
config.SetAutoTime(nativeValue.(float64))
|
config.SetAutoTime(nativeValue.(float64))
|
||||||
config.StartAutoSave()
|
|
||||||
} else {
|
} else {
|
||||||
config.SetAutoTime(0)
|
config.SetAutoTime(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +1,49 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Autosave chan bool
|
var Autosave chan bool
|
||||||
var autotime float64
|
var autotime chan float64
|
||||||
|
|
||||||
// lock for autosave
|
|
||||||
var autolock sync.Mutex
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Autosave = make(chan bool)
|
Autosave = make(chan bool)
|
||||||
|
autotime = make(chan float64)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetAutoTime(a float64) {
|
func SetAutoTime(a float64) {
|
||||||
autolock.Lock()
|
autotime <- a
|
||||||
autotime = a
|
|
||||||
autolock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartAutoSave() {
|
func StartAutoSave() {
|
||||||
go func() {
|
go func() {
|
||||||
|
var a float64
|
||||||
|
var t *time.Timer
|
||||||
|
var elapsed <-chan time.Time
|
||||||
for {
|
for {
|
||||||
autolock.Lock()
|
select {
|
||||||
a := autotime
|
case a = <-autotime:
|
||||||
autolock.Unlock()
|
if t != nil {
|
||||||
if a <= 0 {
|
t.Stop()
|
||||||
break
|
for len(elapsed) > 0 {
|
||||||
|
<-elapsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if a > 0 {
|
||||||
|
if t != nil {
|
||||||
|
t.Reset(time.Duration(a * float64(time.Second)))
|
||||||
|
} else {
|
||||||
|
t = time.NewTimer(time.Duration(a * float64(time.Second)))
|
||||||
|
elapsed = t.C
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-elapsed:
|
||||||
|
if a > 0 {
|
||||||
|
t.Reset(time.Duration(a * float64(time.Second)))
|
||||||
|
Autosave <- true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(time.Duration(a * float64(time.Second)))
|
|
||||||
Autosave <- true
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user