Skip to content

Commit

Permalink
chore: merge branch 'master' into v2-exp
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 12, 2024
2 parents aa74e9f + 730f5a2 commit 0719711
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
40 changes: 40 additions & 0 deletions .golangci-soft.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
run:
tests: false
issues-exit-code: 0

issues:
include:
- EXC0001
- EXC0005
- EXC0011
- EXC0012
- EXC0013

max-issues-per-linter: 0
max-same-issues: 0

linters:
enable:
- exhaustive
- goconst
- godot
- godox
- mnd
- gomoddirectives
- goprintffuncname
- misspell
- nakedret
- nestif
- noctx
- nolintlint
- prealloc
- wrapcheck

# disable default linters, they are already enabled in .golangci.yml
disable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
21 changes: 16 additions & 5 deletions stopwatch/stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type TickMsg struct {
// Note, however, that a stopwatch will reject ticks from other
// stopwatches, so it's safe to flow all TickMsgs through all stopwatches
// and have them still behave appropriately.
ID int
ID int
tag int
}

// StartStopMsg is sent when the stopwatch should start or stop.
Expand All @@ -54,6 +55,7 @@ type ResetMsg struct {
type Model struct {
d time.Duration
id int
tag int
running bool

// How long to wait before every tick. Defaults to 1 second.
Expand Down Expand Up @@ -86,7 +88,7 @@ func (m Model) Init() (Model, tea.Cmd) {
func (m Model) Start() tea.Cmd {
return tea.Batch(func() tea.Msg {
return StartStopMsg{ID: m.id, running: true}
}, tick(m.id, m.Interval))
}, tick(m.id, m.tag, m.Interval))
}

// Stop stops the stopwatch.
Expand Down Expand Up @@ -133,8 +135,17 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.running || msg.ID != m.id {
break
}

// If a tag is set, and it's not the one we expect, reject the message.
// This prevents the stopwatch from receiving too many messages and
// thus ticking too fast.
if msg.tag > 0 && msg.tag != m.tag {
return m, nil
}

m.d += m.Interval
return m, tick(m.id, m.Interval)
m.tag++
return m, tick(m.id, m.tag, m.Interval)
}

return m, nil
Expand All @@ -150,8 +161,8 @@ func (m Model) View() string {
return m.d.String()
}

func tick(id int, d time.Duration) tea.Cmd {
func tick(id int, tag int, d time.Duration) tea.Cmd {
return tea.Tick(d, func(_ time.Time) tea.Msg {
return TickMsg{ID: id}
return TickMsg{ID: id, tag: tag}
})
}
12 changes: 11 additions & 1 deletion timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type TickMsg struct {
// Timeout returns whether or not this tick is a timeout tick. You can
// alternatively listen for TimeoutMsg.
Timeout bool

tag int
}

// TimeoutMsg is a message that is sent once when the timer times out.
Expand All @@ -93,6 +95,7 @@ type Model struct {
Interval time.Duration

id int
tag int
running bool
}

Expand Down Expand Up @@ -149,6 +152,13 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
break
}

// If a tag is set, and it's not the one we expect, reject the message.
// This prevents the ticker from receiving too many messages and
// thus ticking too fast.
if msg.tag > 0 && msg.tag != m.tag {
return m, nil
}

m.Timeout -= m.Interval
return m, tea.Batch(m.tick(), m.timedout())
}
Expand Down Expand Up @@ -178,7 +188,7 @@ func (m *Model) Toggle() tea.Cmd {

func (m Model) tick() tea.Cmd {
return tea.Tick(m.Interval, func(_ time.Time) tea.Msg {
return TickMsg{ID: m.id, Timeout: m.Timedout()}
return TickMsg{ID: m.id, tag: m.tag, Timeout: m.Timedout()}
})
}

Expand Down

0 comments on commit 0719711

Please sign in to comment.