From c8da493a9d13172b04b67b1f1dc9a6d2674badc9 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 31 May 2020 00:56:10 +0200 Subject: [PATCH] Synchronize save messages --- file.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/file.go b/file.go index d3213c4..fe59390 100644 --- a/file.go +++ b/file.go @@ -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 {