Skip to content

Commit

Permalink
perf(list,trash): async get
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Oct 23, 2021
1 parent 9567943 commit 764bc96
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (c listCmd) Run(g globalCmd, args []string) error {

wg.Done()
}(msg)
wg.Wait()
}
wg.Wait()
}

sortListItems(list, c.Sort)
Expand Down
90 changes: 52 additions & 38 deletions cmd_trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"strings"
"sync"
"time"

"github.com/shu-go/gli"
Expand Down Expand Up @@ -83,56 +84,69 @@ func (c trashCmd) Run(g globalCmd, args []string) error {
}
}

wg := sync.WaitGroup{}
mut := sync.Mutex{}

for id := range idset {
//m, err := msgService.Get(c.LoginID, msg.Id).Format("metadata").Do()
m, err := msgService.Get(g.UserID, id).Format("full").Do()
if err != nil {
return err
}
wg.Add(1)
go func(id string) {
//m, err := msgService.Get(c.LoginID, msg.Id).Format("metadata").Do()
m, err := msgService.Get(g.UserID, id).Format("full").Do()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
return
}

content := c.Format
content := c.Format

if strings.Index(c.Format, "{id}") != -1 {
content = strings.Replace(content, "{id}", id, -1)
}
if strings.Index(c.Format, "{id}") != -1 {
content = strings.Replace(content, "{id}", id, -1)
}

if strings.Index(c.Format, "{subject}") != -1 {
content = strings.Replace(content, "{subject}", getHeader(m.Payload.Headers, "Subject"), -1)
}
if strings.Index(c.Format, "{subject}") != -1 {
content = strings.Replace(content, "{subject}", getHeader(m.Payload.Headers, "Subject"), -1)
}

if strings.Index(c.Format, "{headers}") != -1 {
content = strings.Replace(content, "{headers}", fmt.Sprintf("%#v", m.Payload.Headers), -1)
}
if strings.Index(c.Format, "{headers}") != -1 {
content = strings.Replace(content, "{headers}", fmt.Sprintf("%#v", m.Payload.Headers), -1)
}

if strings.Index(c.Format, "{date}") != -1 {
content = strings.Replace(content, "{date}", getHeader(m.Payload.Headers, "Date"), -1)
}
if strings.Index(c.Format, "{date}") != -1 {
content = strings.Replace(content, "{date}", getHeader(m.Payload.Headers, "Date"), -1)
}

if strings.Index(c.Format, "{snippet}") != -1 {
content = strings.Replace(content, "{snippet}", m.Snippet, -1)
}
if strings.Index(c.Format, "{snippet}") != -1 {
content = strings.Replace(content, "{snippet}", m.Snippet, -1)
}

if strings.Index(c.Format, "{body}") != -1 {
decoded, err := base64.URLEncoding.DecodeString(m.Payload.Body.Data)
if strings.Index(c.Format, "{body}") != -1 {
decoded, err := base64.URLEncoding.DecodeString(m.Payload.Body.Data)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
return
}
content = strings.Replace(content, "{body}", string(decoded), -1)
}

dt, err := time.Parse(time.RFC822Z, getHeader(m.Payload.Headers, "Date"))
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
break //continue
dt = time.Now()
}
content = strings.Replace(content, "{body}", string(decoded), -1)
}

dt, err := time.Parse(time.RFC822Z, getHeader(m.Payload.Headers, "Date"))
if err != nil {
dt = time.Now()
}
list = append(list, listItem{
Content: content,
ID: m.Id,
Subject: getHeader(m.Payload.Headers, "Subject"),
Date: dt,
Snippet: m.Snippet,
})
mut.Lock()
list = append(list, listItem{
Content: content,
ID: m.Id,
Subject: getHeader(m.Payload.Headers, "Subject"),
Date: dt,
Snippet: m.Snippet,
})
mut.Unlock()

wg.Done()
}(id)
}
wg.Wait()

sortListItems(list, c.Sort)
for _, item := range list {
Expand Down

0 comments on commit 764bc96

Please sign in to comment.