Skip to content

Commit 656eea2

Browse files
committed
expose remainingseconds as a data atom
1 parent fb071a2 commit 656eea2

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

tsunami/demo/pomodoro/app.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ type Mode struct {
1616
var (
1717
WorkMode = Mode{Name: "Work", Duration: 25}
1818
BreakMode = Mode{Name: "Break", Duration: 5}
19+
20+
// Data atom to expose remaining seconds to external systems
21+
remainingSecondsAtom = app.DataAtom("remainingSeconds", WorkMode.Duration*60)
1922
)
2023

2124
type TimerDisplayProps struct {
22-
Minutes int `json:"minutes"`
23-
Seconds int `json:"seconds"`
24-
Mode string `json:"mode"`
25+
RemainingSeconds int `json:"remainingSeconds"`
26+
Mode string `json:"mode"`
2527
}
2628

2729
type ControlButtonsProps struct {
@@ -35,6 +37,8 @@ type ControlButtonsProps struct {
3537

3638
var TimerDisplay = app.DefineComponent("TimerDisplay",
3739
func(props TimerDisplayProps) any {
40+
minutes := props.RemainingSeconds / 60
41+
seconds := props.RemainingSeconds % 60
3842
return vdom.H("div",
3943
map[string]any{"className": "bg-slate-700 p-8 rounded-lg mb-8 text-center"},
4044
vdom.H("div",
@@ -43,7 +47,7 @@ var TimerDisplay = app.DefineComponent("TimerDisplay",
4347
),
4448
vdom.H("div",
4549
map[string]any{"className": "text-6xl font-bold font-mono text-slate-100"},
46-
fmt.Sprintf("%02d:%02d", props.Minutes, props.Seconds),
50+
fmt.Sprintf("%02d:%02d", minutes, seconds),
4751
),
4852
)
4953
},
@@ -102,8 +106,6 @@ var App = app.DefineComponent("App",
102106
app.UseSetAppTitle("Pomodoro Timer (Tsunami Demo)")
103107

104108
isRunning := app.UseLocal(false)
105-
minutes := app.UseLocal(WorkMode.Duration)
106-
seconds := app.UseLocal(0)
107109
mode := app.UseLocal(WorkMode.Name)
108110
isComplete := app.UseLocal(false)
109111
startTime := app.UseRef(time.Time{})
@@ -121,19 +123,16 @@ var App = app.DefineComponent("App",
121123
if remaining <= 0 {
122124
// Timer completed
123125
isRunning.Set(false)
124-
minutes.Set(0)
125-
seconds.Set(0)
126+
remainingSecondsAtom.Set(0)
126127
isComplete.Set(true)
127128
return
128129
}
129130

130-
m := int(remaining.Minutes())
131-
s := int(remaining.Seconds()) % 60
131+
newSeconds := int(remaining.Seconds())
132132

133-
// Only send update if values actually changed
134-
if m != minutes.Get() || s != seconds.Get() {
135-
minutes.Set(m)
136-
seconds.Set(s)
133+
// Only send update if value actually changed
134+
if newSeconds != remainingSecondsAtom.Get() {
135+
remainingSecondsAtom.Set(newSeconds)
137136
}
138137
}, []any{isRunning.Get()})
139138

@@ -144,30 +143,38 @@ var App = app.DefineComponent("App",
144143

145144
isComplete.Set(false)
146145
startTime.Current = time.Now()
147-
totalDuration.Current = time.Duration(minutes.Get()) * time.Minute
146+
totalDuration.Current = time.Duration(remainingSecondsAtom.Get()) * time.Second
148147
isRunning.Set(true)
149148
}
150149

151150
pauseTimer := func() {
151+
if !isRunning.Get() {
152+
return
153+
}
154+
155+
// Calculate remaining time and update remainingSeconds
156+
elapsed := time.Since(startTime.Current)
157+
remaining := totalDuration.Current - elapsed
158+
if remaining > 0 {
159+
remainingSecondsAtom.Set(int(remaining.Seconds()))
160+
}
152161
isRunning.Set(false)
153162
}
154163

155164
resetTimer := func() {
156165
isRunning.Set(false)
157166
isComplete.Set(false)
158167
if mode.Get() == WorkMode.Name {
159-
minutes.Set(WorkMode.Duration)
168+
remainingSecondsAtom.Set(WorkMode.Duration * 60)
160169
} else {
161-
minutes.Set(BreakMode.Duration)
170+
remainingSecondsAtom.Set(BreakMode.Duration * 60)
162171
}
163-
seconds.Set(0)
164172
}
165173

166174
changeMode := func(duration int) {
167175
isRunning.Set(false)
168176
isComplete.Set(false)
169-
minutes.Set(duration)
170-
seconds.Set(0)
177+
remainingSecondsAtom.Set(duration * 60)
171178
if duration == WorkMode.Duration {
172179
mode.Set(WorkMode.Name)
173180
} else {
@@ -182,9 +189,8 @@ var App = app.DefineComponent("App",
182189
"Pomodoro Timer",
183190
),
184191
TimerDisplay(TimerDisplayProps{
185-
Minutes: minutes.Get(),
186-
Seconds: seconds.Get(),
187-
Mode: mode.Get(),
192+
RemainingSeconds: remainingSecondsAtom.Get(),
193+
Mode: mode.Get(),
188194
}),
189195
ControlButtons(ControlButtonsProps{
190196
IsRunning: isRunning.Get(),

0 commit comments

Comments
 (0)