Skip to content

Commit

Permalink
async render (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO authored Jun 15, 2022
1 parent 50adb4b commit f4cfaa7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/holiday-jp/holiday_jp-go v0.0.0-20201220151532-3a823f759834
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.2
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/rakyll/statik v0.1.1/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
41 changes: 32 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package main

import (
"bytes"
"flag"
"fmt"
"os"
"strings"
"sync"
"text/template"
"time"

holiday "github.com/holiday-jp/holiday_jp-go"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

type calendar struct {
weekTemplate *template.Template
target time.Time
month *month
buf *strings.Builder
buf *bytes.Buffer
}

func newCalendar(target time.Time) (*calendar, error) {
Expand All @@ -29,7 +31,7 @@ func newCalendar(target time.Time) (*calendar, error) {
weekTemplate: tmpl,
target: target,
month: &month{},
buf: &strings.Builder{},
buf: &bytes.Buffer{},
}, nil
}

Expand Down Expand Up @@ -150,12 +152,33 @@ func (c *calendar) calculate() {
}

func (c *calendar) render() error {
for _, w := range c.month.weeks {
if err := c.weekTemplate.Execute(c.buf, w); err != nil {
return errors.WithStack(err)
}
if _, err := c.buf.WriteString("\n"); err != nil {
return errors.WithStack(err)
m := sync.Map{}
eg := errgroup.Group{}
for i, w := range c.month.weeks {
i, w := i, w
eg.Go(func() error {
buf := &bytes.Buffer{}
if err := c.weekTemplate.Execute(buf, w); err != nil {
return errors.WithStack(err)
}
if _, err := buf.WriteString("\n"); err != nil {
return errors.WithStack(err)
}

m.Store(i, buf)
return nil
})
}
if err := eg.Wait(); err != nil {
return errors.WithStack(err)
}

for i := 0; i < len(c.month.weeks); i++ {
if v, ok := m.Load(i); ok {
b := v.(*bytes.Buffer)
if _, err := b.WriteTo(c.buf); err != nil {
return errors.WithStack(err)
}
}
}

Expand Down

0 comments on commit f4cfaa7

Please sign in to comment.