Skip to content

Commit 864f2b6

Browse files
committed
Refactor Timer interface: constructor injection and IsActive()
- Timer interface now has IsActive() bool, StartTimer(minutes int), StartBreakTimer(minutes int) — configuration no longer passed per call - ProcessLocalTimer and WebTimer receive config via constructor (NewProcessLocalTimer / NewWebTimer) - ProcessLocalTimer.IsActive() returns configuration.TimerLocal - WebTimer.IsActive() returns room != "" - GetTimers() constructs all implementations and filters by IsActive(), so each implementation decides itself whether it should run https://claude.ai/code/session_01DTB35xCmhRgW5SQUsYAtAq
1 parent 26cd6ec commit 864f2b6

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

timer/localtimer.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,37 @@ import (
1313
)
1414

1515
// ProcessLocalTimer is a Timer implementation that uses background OS processes.
16-
type ProcessLocalTimer struct{}
16+
type ProcessLocalTimer struct {
17+
configuration config.Configuration
18+
}
19+
20+
func NewProcessLocalTimer(configuration config.Configuration) ProcessLocalTimer {
21+
return ProcessLocalTimer{configuration: configuration}
22+
}
23+
24+
func (t ProcessLocalTimer) IsActive() bool {
25+
return t.configuration.TimerLocal
26+
}
1727

18-
func (t ProcessLocalTimer) StartTimer(minutes int, configuration config.Configuration) error {
28+
func (t ProcessLocalTimer) StartTimer(minutes int) error {
1929
timeoutInSeconds := minutes * 60
2030
if err := ExecuteCommandsInBackgroundProcess(
2131
sleepCommand(timeoutInSeconds),
22-
VoiceCommand(configuration.VoiceMessage, configuration.VoiceCommand),
23-
notifyCommand(configuration.NotifyMessage, configuration.NotifyCommand),
32+
VoiceCommand(t.configuration.VoiceMessage, t.configuration.VoiceCommand),
33+
notifyCommand(t.configuration.NotifyMessage, t.configuration.NotifyCommand),
2434
"echo \"mobTimer\"",
2535
); err != nil {
2636
return fmt.Errorf("timer couldn't be started on your system (%s): %w", runtime.GOOS, err)
2737
}
2838
return nil
2939
}
3040

31-
func (t ProcessLocalTimer) StartBreakTimer(minutes int, configuration config.Configuration) error {
41+
func (t ProcessLocalTimer) StartBreakTimer(minutes int) error {
3242
timeoutInSeconds := minutes * 60
3343
if err := ExecuteCommandsInBackgroundProcess(
3444
sleepCommand(timeoutInSeconds),
35-
VoiceCommand("mob start", configuration.VoiceCommand),
36-
notifyCommand("mob start", configuration.NotifyCommand),
45+
VoiceCommand("mob start", t.configuration.VoiceCommand),
46+
notifyCommand("mob start", t.configuration.NotifyCommand),
3747
"echo \"mobTimer\"",
3848
); err != nil {
3949
return fmt.Errorf("break timer couldn't be started on your system (%s): %w", runtime.GOOS, err)

timer/timer.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,27 @@ import (
1313

1414
// Timer abstracts timer functionality so different implementations can be used.
1515
type Timer interface {
16-
StartTimer(minutes int, configuration config.Configuration) error
17-
StartBreakTimer(minutes int, configuration config.Configuration) error
16+
IsActive() bool
17+
StartTimer(minutes int) error
18+
StartBreakTimer(minutes int) error
1819
}
1920

20-
// GetTimers returns the list of active timers based on room and configuration.
21-
// Both WebTimer and ProcessLocalTimer can be active simultaneously.
21+
// GetTimers returns all timers that report themselves as active.
2222
func GetTimers(room string, timerUser string, configuration config.Configuration) []Timer {
23-
var timers []Timer
24-
if room != "" {
25-
timers = append(timers, WebTimer{Room: room, TimerUser: timerUser})
23+
all := []Timer{
24+
NewWebTimer(room, timerUser, configuration),
25+
NewProcessLocalTimer(configuration),
2626
}
27-
if configuration.TimerLocal {
28-
timers = append(timers, ProcessLocalTimer{})
27+
var active []Timer
28+
for _, t := range all {
29+
if t.IsActive() {
30+
active = append(active, t)
31+
}
2932
}
30-
return timers
33+
return active
3134
}
3235

33-
// RunTimer parses timerInMinutes, starts all configured timers and returns any error.
36+
// RunTimer parses timerInMinutes, starts all active timers and returns any error.
3437
func RunTimer(timerInMinutes string, room string, timerUser string, configuration config.Configuration) error {
3538
err, timeoutInMinutes := toMinutes(timerInMinutes)
3639
if err != nil {
@@ -48,7 +51,7 @@ func RunTimer(timerInMinutes string, room string, timerUser string, configuratio
4851
}
4952

5053
for _, t := range timers {
51-
if err := t.StartTimer(timeoutInMinutes, configuration); err != nil {
54+
if err := t.StartTimer(timeoutInMinutes); err != nil {
5255
say.Error(err.Error())
5356
exit.Exit(1)
5457
}
@@ -58,7 +61,7 @@ func RunTimer(timerInMinutes string, room string, timerUser string, configuratio
5861
return nil
5962
}
6063

61-
// RunBreakTimer parses timerInMinutes, starts all configured break timers and returns any error.
64+
// RunBreakTimer parses timerInMinutes, starts all active break timers and returns any error.
6265
func RunBreakTimer(timerInMinutes string, room string, timerUser string, configuration config.Configuration) error {
6366
err, timeoutInMinutes := toMinutes(timerInMinutes)
6467
if err != nil {
@@ -76,7 +79,7 @@ func RunBreakTimer(timerInMinutes string, room string, timerUser string, configu
7679
}
7780

7881
for _, t := range timers {
79-
if err := t.StartBreakTimer(timeoutInMinutes, configuration); err != nil {
82+
if err := t.StartBreakTimer(timeoutInMinutes); err != nil {
8083
say.Error(err.Error())
8184
exit.Exit(1)
8285
}

timer/webtimer.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,34 @@ import (
1010

1111
// WebTimer is a Timer implementation that notifies a remote timer service via HTTP.
1212
type WebTimer struct {
13-
Room string
14-
TimerUser string
13+
room string
14+
timerUser string
15+
timerUrl string
16+
timerInsecure bool
1517
}
1618

17-
func (t WebTimer) StartTimer(minutes int, configuration config.Configuration) error {
18-
if err := httpPutTimer(minutes, t.Room, t.TimerUser, configuration.TimerUrl, configuration.TimerInsecure); err != nil {
19+
func NewWebTimer(room string, timerUser string, configuration config.Configuration) WebTimer {
20+
return WebTimer{
21+
room: room,
22+
timerUser: timerUser,
23+
timerUrl: configuration.TimerUrl,
24+
timerInsecure: configuration.TimerInsecure,
25+
}
26+
}
27+
28+
func (t WebTimer) IsActive() bool {
29+
return t.room != ""
30+
}
31+
32+
func (t WebTimer) StartTimer(minutes int) error {
33+
if err := httpPutTimer(minutes, t.room, t.timerUser, t.timerUrl, t.timerInsecure); err != nil {
1934
return fmt.Errorf("remote timer couldn't be started: %w", err)
2035
}
2136
return nil
2237
}
2338

24-
func (t WebTimer) StartBreakTimer(minutes int, configuration config.Configuration) error {
25-
if err := httpPutBreakTimer(minutes, t.Room, t.TimerUser, configuration.TimerUrl, configuration.TimerInsecure); err != nil {
39+
func (t WebTimer) StartBreakTimer(minutes int) error {
40+
if err := httpPutBreakTimer(minutes, t.room, t.timerUser, t.timerUrl, t.timerInsecure); err != nil {
2641
return fmt.Errorf("remote break timer couldn't be started: %w", err)
2742
}
2843
return nil

0 commit comments

Comments
 (0)