Wait for store jobs to finish when exiting.

This fixes #144 #147.
This commit is contained in:
Qi Xiao 2016-02-20 02:32:12 +01:00
parent 098965cfac
commit 5c03407a8a
3 changed files with 15 additions and 5 deletions

View File

@ -16,8 +16,10 @@ func (h *historyState) jump(i int, line string) {
func (ed *Editor) appendHistory(line string) {
if ed.store != nil {
go func() {
ed.store.AddCmd(line)
ed.store.Waits.Add(1)
// TODO(xiaq): Report possible error
ed.store.AddCmd(line)
ed.store.Waits.Done()
}()
}
}

View File

@ -370,8 +370,13 @@ func cdInner(dir string, ec *EvalCtx) {
// XXX Error ignored.
pwd, err := os.Getwd()
if err == nil {
// XXX Error ignored.
go ec.store.AddDir(pwd)
store := ec.store
go func() {
store.Waits.Add(1)
// XXX Error ignored.
store.AddDir(pwd)
store.Waits.Done()
}()
}
}
}

View File

@ -5,13 +5,15 @@ import (
"database/sql"
"fmt"
"net/url"
"sync"
_ "github.com/elves/go-sqlite/sqlite3" // enable the "sqlite3" SQL driver
)
// Store is the permanent storage backend for elvish.
type Store struct {
db *sql.DB
db *sql.DB
Waits sync.WaitGroup
}
var initTable = map[string](func(*sql.DB) error){}
@ -35,7 +37,7 @@ func NewStore(dbname string) (*Store, error) {
// NewStoreDB creates a new Store with a custom database. The database must be
// a SQLite database.
func NewStoreDB(db *sql.DB) (*Store, error) {
st := &Store{db}
st := &Store{db, sync.WaitGroup{}}
for name, fn := range initTable {
err := fn(db)
@ -51,5 +53,6 @@ func (s *Store) Close() error {
if s == nil || s.db == nil {
return nil
}
s.Waits.Wait()
return s.db.Close()
}