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.
This commit is contained in:
parent
e9fb6100f2
commit
faccccf93f
@ -60,10 +60,10 @@ type Ticker interface {
|
|||||||
Stop()
|
Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ticker is the production implementation of the resumable Ticker interface.
|
// T is the production implementation of the resumable Ticker interface. This
|
||||||
// This allows various components to toggle their need for tick events, which
|
// allows various components to toggle their need for tick events, which may
|
||||||
// may vary depending on system load.
|
// vary depending on system load.
|
||||||
type ticker struct {
|
type T struct {
|
||||||
// interval is the desired duration between ticks when active.
|
// interval is the desired duration between ticks when active.
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
|
|
||||||
@ -73,10 +73,13 @@ type ticker struct {
|
|||||||
ticker *time.Ticker
|
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
|
// New returns a new ticker that signals with the given interval when not
|
||||||
// paused. The ticker starts off inactive.
|
// paused. The ticker starts off inactive.
|
||||||
func New(interval time.Duration) Ticker {
|
func New(interval time.Duration) *T {
|
||||||
return &ticker{
|
return &T{
|
||||||
interval: interval,
|
interval: interval,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,18 +88,18 @@ func New(interval time.Duration) Ticker {
|
|||||||
// prescribed interval. This method returns nil when the ticker is paused.
|
// prescribed interval. This method returns nil when the ticker is paused.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the Ticker interface.
|
// NOTE: Part of the Ticker interface.
|
||||||
func (t *ticker) Ticks() <-chan time.Time {
|
func (t *T) Ticks() <-chan time.Time {
|
||||||
if t.ticker == nil {
|
if t.ticker == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return t.ticker.C
|
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.
|
// delivering scheduled events.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the Ticker interface.
|
// NOTE: Part of the Ticker interface.
|
||||||
func (t *ticker) Resume() {
|
func (t *T) Resume() {
|
||||||
if t.ticker == nil {
|
if t.ticker == nil {
|
||||||
t.ticker = time.NewTicker(t.interval)
|
t.ticker = time.NewTicker(t.interval)
|
||||||
}
|
}
|
||||||
@ -106,7 +109,7 @@ func (t *ticker) Resume() {
|
|||||||
// regular intervals.
|
// regular intervals.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the Ticker interface.
|
// NOTE: Part of the Ticker interface.
|
||||||
func (t *ticker) Pause() {
|
func (t *T) Pause() {
|
||||||
if t.ticker != nil {
|
if t.ticker != nil {
|
||||||
t.ticker.Stop()
|
t.ticker.Stop()
|
||||||
t.ticker = nil
|
t.ticker = nil
|
||||||
@ -118,6 +121,6 @@ func (t *ticker) Pause() {
|
|||||||
// implementation, this is equivalent to Pause.
|
// implementation, this is equivalent to Pause.
|
||||||
//
|
//
|
||||||
// NOTE: Part of the Ticker interface.
|
// NOTE: Part of the Ticker interface.
|
||||||
func (t *ticker) Stop() {
|
func (t *T) Stop() {
|
||||||
t.Pause()
|
t.Pause()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user