feat(clock): real ports.Clock adapter over time.Timer
- commit
- 844c8460f79c67cab0df821e4c47ac9c881ef0a6
- author
- brice · · 2026-07-02 11:59 UTC
- parent
- 4ab7e23
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +30 -0
A internal/adapters/clock/clock.go +30 -0
| @@ -0,0 +1,30 @@ | |||
| 1 | + | // Package clock provides the production ports.Clock: a thin wrapper over the | |
| 2 | + | // standard library's time package. Usecases depend on ports.Clock so their | |
| 3 | + | // time-dependent logic (the daemon's debounced render scheduler) can be driven | |
| 4 | + | // deterministically in tests via mocks, while the app wires this real clock. | |
| 5 | + | package clock | |
| 6 | + | ||
| 7 | + | import ( | |
| 8 | + | "time" | |
| 9 | + | ||
| 10 | + | "github.com/bnema/vev/internal/ports" | |
| 11 | + | ) | |
| 12 | + | ||
| 13 | + | // realClock implements ports.Clock over the wall clock. | |
| 14 | + | type realClock struct{} | |
| 15 | + | ||
| 16 | + | // New returns the production ports.Clock. | |
| 17 | + | func New() ports.Clock { return realClock{} } | |
| 18 | + | ||
| 19 | + | func (realClock) Now() time.Time { return time.Now() } | |
| 20 | + | ||
| 21 | + | func (realClock) NewTimer(d time.Duration) ports.Timer { | |
| 22 | + | return &realTimer{t: time.NewTimer(d)} | |
| 23 | + | } | |
| 24 | + | ||
| 25 | + | // realTimer adapts *time.Timer to ports.Timer. | |
| 26 | + | type realTimer struct{ t *time.Timer } | |
| 27 | + | ||
| 28 | + | func (r *realTimer) C() <-chan time.Time { return r.t.C } | |
| 29 | + | func (r *realTimer) Reset(d time.Duration) bool { return r.t.Reset(d) } | |
| 30 | + | func (r *realTimer) Stop() bool { return r.t.Stop() } | |
| @@ -0,0 +1,30 @@ | |||||
| 1 | + | // Package clock provides the production ports.Clock: a thin wrapper over the | |||
| 2 | + | // standard library's time package. Usecases depend on ports.Clock so their | |||
| 3 | + | // time-dependent logic (the daemon's debounced render scheduler) can be driven | |||
| 4 | + | // deterministically in tests via mocks, while the app wires this real clock. | |||
| 5 | + | package clock | |||
| 6 | + | ||||
| 7 | + | import ( | |||
| 8 | + | "time" | |||
| 9 | + | ||||
| 10 | + | "github.com/bnema/vev/internal/ports" | |||
| 11 | + | ) | |||
| 12 | + | ||||
| 13 | + | // realClock implements ports.Clock over the wall clock. | |||
| 14 | + | type realClock struct{} | |||
| 15 | + | ||||
| 16 | + | // New returns the production ports.Clock. | |||
| 17 | + | func New() ports.Clock { return realClock{} } | |||
| 18 | + | ||||
| 19 | + | func (realClock) Now() time.Time { return time.Now() } | |||
| 20 | + | ||||
| 21 | + | func (realClock) NewTimer(d time.Duration) ports.Timer { | |||
| 22 | + | return &realTimer{t: time.NewTimer(d)} | |||
| 23 | + | } | |||
| 24 | + | ||||
| 25 | + | // realTimer adapts *time.Timer to ports.Timer. | |||
| 26 | + | type realTimer struct{ t *time.Timer } | |||
| 27 | + | ||||
| 28 | + | func (r *realTimer) C() <-chan time.Time { return r.t.C } | |||
| 29 | + | func (r *realTimer) Reset(d time.Duration) bool { return r.t.Reset(d) } | |||
| 30 | + | func (r *realTimer) Stop() bool { return r.t.Stop() } | |||