Synchronize save messages
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-05-31 00:56:10 +02:00
parent dc5784c33e
commit c8da493a9d

29
file.go
View File

@ -17,12 +17,25 @@ import (
"github.com/sergi/go-diff/diffmatchpatch"
)
type saveMessage struct {
p string
page Page
summary string
author string
}
type FilePages struct {
dirname string
saveC chan saveMessage
}
func NewFilePages(dirname string) PagesRepository {
fp := &FilePages{dirname}
fp := &FilePages{dirname, make(chan saveMessage)}
go func () {
for msg := range fp.saveC {
fp.save(msg)
}
}()
return fp
}
@ -62,6 +75,16 @@ func (fp *FilePages) Get(title string) Page {
}
func (fp *FilePages) Save(p string, page Page, summary, author string) error {
fp.saveC <- saveMessage{p, page, summary, author}
return nil
}
func (fp *FilePages) save(msg saveMessage) error {
p := msg.p
page := msg.page
summary := msg.summary
author := msg.author
f, err := os.Create(filepath.Join(fp.dirname, strings.Replace(p, " ", "_", -1)))
if err != nil {
return err
@ -87,7 +110,9 @@ func (fp *FilePages) Save(p string, page Page, summary, author string) error {
return fmt.Errorf("while processing backrefs: %s", err)
}
return saveWithGit(fp, p, summary, author)
err = saveWithGit(fp, p, summary, author)
return err
}
func saveWithGit(fp *FilePages, p string, summary, author string) error {