From faccccf93f18c24cf6472fa0b4d99ff56a6a3066 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 6 Feb 2019 16:46:00 -0800 Subject: [PATCH] ticker/ticker: rename ticker -> T In this commit, we rename the previously unexported ticker into T, making it exported along the way. This is nice as we now have access to the actual interface implementation without the need of making further type assertions. --- ticker/ticker.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/ticker/ticker.go b/ticker/ticker.go index 29e0c04b..782cbe5a 100644 --- a/ticker/ticker.go +++ b/ticker/ticker.go @@ -60,10 +60,10 @@ type Ticker interface { Stop() } -// ticker is the production implementation of the resumable Ticker interface. -// This allows various components to toggle their need for tick events, which -// may vary depending on system load. -type ticker struct { +// T is the production implementation of the resumable Ticker interface. This +// allows various components to toggle their need for tick events, which may +// vary depending on system load. +type T struct { // interval is the desired duration between ticks when active. interval time.Duration @@ -73,10 +73,13 @@ type ticker struct { ticker *time.Ticker } +// A compile-time constraint to ensure T satisfies the Ticker interface. +var _ Ticker = (*T)(nil) + // New returns a new ticker that signals with the given interval when not // paused. The ticker starts off inactive. -func New(interval time.Duration) Ticker { - return &ticker{ +func New(interval time.Duration) *T { + return &T{ interval: interval, } } @@ -85,18 +88,18 @@ func New(interval time.Duration) Ticker { // prescribed interval. This method returns nil when the ticker is paused. // // NOTE: Part of the Ticker interface. -func (t *ticker) Ticks() <-chan time.Time { +func (t *T) Ticks() <-chan time.Time { if t.ticker == nil { return nil } return t.ticker.C } -// Resumes starts underlying time.Ticker and causes the ticker to begin +// Resume starts underlying time.Ticker and causes the ticker to begin // delivering scheduled events. // // NOTE: Part of the Ticker interface. -func (t *ticker) Resume() { +func (t *T) Resume() { if t.ticker == nil { t.ticker = time.NewTicker(t.interval) } @@ -106,7 +109,7 @@ func (t *ticker) Resume() { // regular intervals. // // NOTE: Part of the Ticker interface. -func (t *ticker) Pause() { +func (t *T) Pause() { if t.ticker != nil { t.ticker.Stop() t.ticker = nil @@ -118,6 +121,6 @@ func (t *ticker) Pause() { // implementation, this is equivalent to Pause. // // NOTE: Part of the Ticker interface. -func (t *ticker) Stop() { +func (t *T) Stop() { t.Pause() }