charmbracelet / charmbracelet/bubbles

timer.Start calls timer.tick() too frequently

Open
#867 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
8.9k
Forks
457
Avg merge
1d 18h
Merged PRs (30d)
5

Description

**Describe the bug**
The timer package's documentation does not accurately describe the actual behavior of the timer.

**Setup**
N/A

**To Reproduce**
```golang

t := timer.New(5 * time.Second)

t.Start()
t.Start() // According to documentation, this second start should have no effect

time.Sleep(time.Second + delta) // Just for demo purposes

fmt.Println(t.Timeout) // 3 * time.Second
```

**Source Code**
`timer.startStop` fires off a `StartStopMsg` message which in turn causes `timer.Update` to enqueue `timer.tick`. `timer.tick` in turn enqueues `TickMsg`. `timer.Update` assumes each tick is fired at exact intervals; therefore, calling `timer.tick` multiple times artificially speeds up the timer.

[CS](https://github.com/charmbracelet/bubbles/blob/ff8b5a8e17c91972211d0b9f03e7764ddaa2f6d0/timer/timer.go#L150)

**Expected behavior**
See [documentation](https://pkg.go.dev/github.com/charmbracelet/bubbles/timer#Model.Start), which states calling `timer.Start` multiple times will have no effect. This is the expected behavior for a normal developer, but is not what actually happens.

A suggested fix will be to record the time of the last time a `TickMsg` message has been processed in `timer.Update` and subtract the duration (recommended in any case), **or** do not issue a `StartStopMsg` when `timer.running` matches the `startStop` input.

**Screenshots**
N/A

**Additional context**
The example suggested `reset` functionality as described in the example [CS](https://github.com/charmbracelet/bubbletea/blob/f9233d51192293dadda7184a4de347738606c328/examples/timer/main.go#L57) is also inaccurate. This assumes the timer is already in the run state and does nothing if the timer has already stopped.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.