Skip to content

Commit

Permalink
fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyng committed Aug 10, 2024
1 parent b2da4f8 commit 647f1a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
15 changes: 15 additions & 0 deletions item.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"cmp"
"fmt"
"net/url"
"slices"
"strings"
"sync"
"time"

"golang.org/x/net/html"
Expand All @@ -20,6 +23,18 @@ type Item struct {
Type string `json:"type"`

Comments []*Comment

mu sync.Mutex
}

func (i *Item) AddComment(c *Comment) {
i.mu.Lock()
defer i.mu.Unlock()

i.Comments = append(i.Comments, c)
slices.SortFunc(i.Comments, func(i, j *Comment) int {
return cmp.Compare(i.Rank, j.Rank)
})
}

func humanize(t time.Time) string {
Expand Down
43 changes: 25 additions & 18 deletions pane.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"cmp"
"fmt"
"sort"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -84,10 +85,7 @@ func (p *PaneView) Update(msg bbt.Msg) (Pane, bbt.Cmd) {
return err
}

parent.Comments = append(parent.Comments, comment)
sort.Slice(parent.Comments, func(i, j int) bool {
return parent.Comments[i].Rank < parent.Comments[j].Rank
})
parent.AddComment(comment)

return ViewMsg[*Comment]{
Value: comment,
Expand Down Expand Up @@ -135,6 +133,9 @@ func (p *PaneView) View() string {
func (p *PaneView) Render() {
p.content.Reset()
if s := p.Story; s != nil {
s.mu.Lock()
defer s.mu.Unlock()

title := strings.TrimPrefix(s.Title(), fmt.Sprintf("%d. ", s.Rank+1))
fmt.Fprintln(&p.content, p.styleTitle.Render(title))

Expand All @@ -161,23 +162,29 @@ func (p *PaneView) Render() {
var lines []string
for _, comment := range comments {
if comment.By != "" {
var sb strings.Builder
render := func() string {
comment.mu.Lock()
defer comment.mu.Unlock()

by := p.styleCommentTitle.Render(comment.By)
if comment.By == s.By {
by = fmt.Sprintf("%s %s", by, p.styleOP.String())
}
var sb strings.Builder
by := p.styleCommentTitle.Render(comment.By)
if comment.By == s.By {
by = fmt.Sprintf("%s %s", by, p.styleOP.String())
}

fmt.Fprintln(&sb, by, p.styleCommentTitle.Copy().Faint(true).Render(humanize(time.Unix(comment.Time, 0))))

fmt.Fprintln(&sb, by, p.styleCommentTitle.Copy().Faint(true).Render(humanize(time.Unix(comment.Time, 0))))
sb.WriteString(HTMLText(comment.Text))

sb.WriteString(HTMLText(comment.Text))
if len(comment.Comments) > 0 {
fmt.Fprintln(&sb)
fmt.Fprint(&sb, view(style.Copy().Width(style.GetWidth()-h), comment.Comments))
}

if len(comment.Comments) > 0 {
fmt.Fprintln(&sb)
fmt.Fprint(&sb, view(style.Copy().Width(style.GetWidth()-h), comment.Comments))
return sb.String()
}

lines = append(lines, style.Render(sb.String()))
lines = append(lines, style.Render(render()))
}
}

Expand Down Expand Up @@ -295,8 +302,8 @@ func (p *PaneList) Update(msg bbt.Msg) (Pane, bbt.Cmd) {
return p, bbt.Batch(cmds...)
case ListMsg[*Story]:
items := append(p.model.Items(), msg.Value)
sort.Slice(items, func(i, j int) bool {
return items[i].(*Story).Rank < items[j].(*Story).Rank
slices.SortFunc(items, func(i, j list.Item) int {
return cmp.Compare(i.(*Story).Rank, j.(*Story).Rank)
})

return p, p.model.SetItems(items)
Expand Down

0 comments on commit 647f1a8

Please sign in to comment.