Use goroutines for the keks
Anirudh Oppiliappan x@icyphox.sh
Fri, 04 Feb 2022 22:31:47 +0530
1 files changed,
35 insertions(+),
4 deletions(-)
jump to
M
commands/build.go
→
commands/build.go
@@ -1,10 +1,12 @@
package commands import ( + "fmt" "os" "path/filepath" "sort" "strings" + "sync" "time" "git.icyphox.sh/vite/atom"@@ -195,6 +197,7 @@
// Core builder function. Converts markdown to html, // copies over non .md files, etc. func Build() error { + fmt.Print("vite: building... ") pages := Pages{} if err := pages.initPages(); err != nil { return err@@ -205,15 +208,42 @@ if err := util.Clean(BUILD); err != nil {
return err } + wg := sync.WaitGroup{} + wg.Add(2) + wgDone := make(chan bool) + + ec := make(chan error) + // Deal with files. // ex: pages/{_index,about,etc}.md - if err := pages.processFiles(); err != nil { - return err - } + go func() { + err := pages.processFiles() + if err != nil { + ec <- err + } + wg.Done() + }() // Deal with dirs -- i.e. dirs of markdown files. // ex: pages/{blog,travel}/*.md - if err := pages.processDirs(); err != nil { + go func() { + err := pages.processDirs() + if err != nil { + ec <- err + } + wg.Done() + }() + + go func() { + wg.Wait() + close(wgDone) + }() + + select { + case <-wgDone: + break + case err := <-ec: + close(ec) return err }@@ -225,5 +255,6 @@ if err := util.CopyDir(STATIC, buildStatic); err != nil {
return err } + fmt.Print("done\n") return nil }