Skip to content

Commit 0719711

Browse files
committed
chore: merge branch 'master' into v2-exp
2 parents aa74e9f + 730f5a2 commit 0719711

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

.golangci-soft.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
run:
2+
tests: false
3+
issues-exit-code: 0
4+
5+
issues:
6+
include:
7+
- EXC0001
8+
- EXC0005
9+
- EXC0011
10+
- EXC0012
11+
- EXC0013
12+
13+
max-issues-per-linter: 0
14+
max-same-issues: 0
15+
16+
linters:
17+
enable:
18+
- exhaustive
19+
- goconst
20+
- godot
21+
- godox
22+
- mnd
23+
- gomoddirectives
24+
- goprintffuncname
25+
- misspell
26+
- nakedret
27+
- nestif
28+
- noctx
29+
- nolintlint
30+
- prealloc
31+
- wrapcheck
32+
33+
# disable default linters, they are already enabled in .golangci.yml
34+
disable:
35+
- errcheck
36+
- gosimple
37+
- govet
38+
- ineffassign
39+
- staticcheck
40+
- unused

stopwatch/stopwatch.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type TickMsg struct {
3636
// Note, however, that a stopwatch will reject ticks from other
3737
// stopwatches, so it's safe to flow all TickMsgs through all stopwatches
3838
// and have them still behave appropriately.
39-
ID int
39+
ID int
40+
tag int
4041
}
4142

4243
// StartStopMsg is sent when the stopwatch should start or stop.
@@ -54,6 +55,7 @@ type ResetMsg struct {
5455
type Model struct {
5556
d time.Duration
5657
id int
58+
tag int
5759
running bool
5860

5961
// How long to wait before every tick. Defaults to 1 second.
@@ -86,7 +88,7 @@ func (m Model) Init() (Model, tea.Cmd) {
8688
func (m Model) Start() tea.Cmd {
8789
return tea.Batch(func() tea.Msg {
8890
return StartStopMsg{ID: m.id, running: true}
89-
}, tick(m.id, m.Interval))
91+
}, tick(m.id, m.tag, m.Interval))
9092
}
9193

9294
// Stop stops the stopwatch.
@@ -133,8 +135,17 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
133135
if !m.running || msg.ID != m.id {
134136
break
135137
}
138+
139+
// If a tag is set, and it's not the one we expect, reject the message.
140+
// This prevents the stopwatch from receiving too many messages and
141+
// thus ticking too fast.
142+
if msg.tag > 0 && msg.tag != m.tag {
143+
return m, nil
144+
}
145+
136146
m.d += m.Interval
137-
return m, tick(m.id, m.Interval)
147+
m.tag++
148+
return m, tick(m.id, m.tag, m.Interval)
138149
}
139150

140151
return m, nil
@@ -150,8 +161,8 @@ func (m Model) View() string {
150161
return m.d.String()
151162
}
152163

153-
func tick(id int, d time.Duration) tea.Cmd {
164+
func tick(id int, tag int, d time.Duration) tea.Cmd {
154165
return tea.Tick(d, func(_ time.Time) tea.Msg {
155-
return TickMsg{ID: id}
166+
return TickMsg{ID: id, tag: tag}
156167
})
157168
}

timer/timer.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type TickMsg struct {
7474
// Timeout returns whether or not this tick is a timeout tick. You can
7575
// alternatively listen for TimeoutMsg.
7676
Timeout bool
77+
78+
tag int
7779
}
7880

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

9597
id int
98+
tag int
9699
running bool
97100
}
98101

@@ -149,6 +152,13 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
149152
break
150153
}
151154

155+
// If a tag is set, and it's not the one we expect, reject the message.
156+
// This prevents the ticker from receiving too many messages and
157+
// thus ticking too fast.
158+
if msg.tag > 0 && msg.tag != m.tag {
159+
return m, nil
160+
}
161+
152162
m.Timeout -= m.Interval
153163
return m, tea.Batch(m.tick(), m.timedout())
154164
}
@@ -178,7 +188,7 @@ func (m *Model) Toggle() tea.Cmd {
178188

179189
func (m Model) tick() tea.Cmd {
180190
return tea.Tick(m.Interval, func(_ time.Time) tea.Msg {
181-
return TickMsg{ID: m.id, Timeout: m.Timedout()}
191+
return TickMsg{ID: m.id, tag: m.tag, Timeout: m.Timedout()}
182192
})
183193
}
184194

0 commit comments

Comments
 (0)