Skip to content

Commit 9a8f74b

Browse files
committed
Extract timer implementations into self-registering sub-packages
- timer/localtimer/ and timer/webtimer/ are now independent packages that register themselves via init() using timer.Register(Factory) - timer package no longer imports its implementations; dependency arrows are reversed: implementations depend on the timer package, not vice versa - timer.go (main) blank-imports both packages to trigger registration - mob.go uses localtimer.ExecuteCommandsInBackgroundProcess/VoiceCommand directly for the moo feature https://claude.ai/code/session_01DTB35xCmhRgW5SQUsYAtAq
1 parent 03692a8 commit 9a8f74b

5 files changed

Lines changed: 38 additions & 14 deletions

File tree

mob.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/remotemobprogramming/mob/v5/help"
2222
"github.com/remotemobprogramming/mob/v5/open"
2323
"github.com/remotemobprogramming/mob/v5/say"
24-
timerpkg "github.com/remotemobprogramming/mob/v5/timer"
24+
"github.com/remotemobprogramming/mob/v5/timer/localtimer"
2525
"github.com/remotemobprogramming/mob/v5/workdir"
2626
)
2727

@@ -453,7 +453,7 @@ func currentTime() string {
453453

454454
func moo(configuration config.Configuration) {
455455
voiceMessage := "moo"
456-
err := timerpkg.ExecuteCommandsInBackgroundProcess(timerpkg.VoiceCommand(voiceMessage, configuration.VoiceCommand))
456+
err := localtimer.ExecuteCommandsInBackgroundProcess(localtimer.VoiceCommand(voiceMessage, configuration.VoiceCommand))
457457

458458
if err != nil {
459459
say.Warning(fmt.Sprintf("can't run voice command on your system (%s)", runtime.GOOS))

timer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
config "github.com/remotemobprogramming/mob/v5/configuration"
55
"github.com/remotemobprogramming/mob/v5/exit"
66
timerpkg "github.com/remotemobprogramming/mob/v5/timer"
7+
_ "github.com/remotemobprogramming/mob/v5/timer/localtimer"
8+
_ "github.com/remotemobprogramming/mob/v5/timer/webtimer"
79
)
810

911
func StartTimer(timerInMinutes string, configuration config.Configuration) {
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package timer
1+
package localtimer
22

33
import (
44
"fmt"
@@ -9,9 +9,16 @@ import (
99
config "github.com/remotemobprogramming/mob/v5/configuration"
1010
"github.com/remotemobprogramming/mob/v5/exit"
1111
"github.com/remotemobprogramming/mob/v5/say"
12+
"github.com/remotemobprogramming/mob/v5/timer"
1213
"github.com/remotemobprogramming/mob/v5/workdir"
1314
)
1415

16+
func init() {
17+
timer.Register(func(configuration config.Configuration) timer.Timer {
18+
return NewProcessLocalTimer(configuration)
19+
})
20+
}
21+
1522
// ProcessLocalTimer is a Timer implementation that uses background OS processes.
1623
type ProcessLocalTimer struct {
1724
configuration config.Configuration

timer/timer.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ type Timer interface {
1818
StartBreakTimer(minutes int) error
1919
}
2020

21-
// GetTimers returns all timers that report themselves as active.
21+
// Factory creates a Timer for the given configuration.
22+
type Factory func(configuration config.Configuration) Timer
23+
24+
var factories []Factory
25+
26+
// Register adds a Timer factory to the registry.
27+
// Implementation packages call this in their init() function.
28+
func Register(f Factory) {
29+
factories = append(factories, f)
30+
}
31+
32+
// GetTimers returns all registered timers that report themselves as active.
2233
func GetTimers(configuration config.Configuration) []Timer {
23-
all := []Timer{
24-
NewWebTimer(configuration),
25-
NewProcessLocalTimer(configuration),
26-
}
2734
var active []Timer
28-
for _, t := range all {
35+
for _, f := range factories {
36+
t := f(configuration)
2937
if t.IsActive() {
3038
active = append(active, t)
3139
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
package timer
1+
package webtimer
22

33
import (
44
"encoding/json"
55
"fmt"
66

77
config "github.com/remotemobprogramming/mob/v5/configuration"
88
"github.com/remotemobprogramming/mob/v5/httpclient"
9+
"github.com/remotemobprogramming/mob/v5/timer"
910
)
1011

12+
func init() {
13+
timer.Register(func(configuration config.Configuration) timer.Timer {
14+
return NewWebTimer(configuration)
15+
})
16+
}
17+
1118
// WebTimer is a Timer implementation that notifies a remote timer service via HTTP.
1219
type WebTimer struct {
13-
room string
14-
timerUser string
15-
timerUrl string
16-
timerInsecure bool
20+
room string
21+
timerUser string
22+
timerUrl string
23+
timerInsecure bool
1724
}
1825

1926
func NewWebTimer(configuration config.Configuration) WebTimer {

0 commit comments

Comments
 (0)