This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tie/fix-ticker-stop
fix: do not tick after stop
- Loading branch information
Showing
5 changed files
with
205 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
package neo | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type ticker struct { | ||
time *Time | ||
ch chan time.Time | ||
id int | ||
dur int64 | ||
dur time.Duration | ||
} | ||
|
||
func (t *ticker) C() <-chan time.Time { | ||
return t.ch | ||
} | ||
|
||
func (t *ticker) Stop() { | ||
t.time.stopTimer(t.id) | ||
t.time.stop(t.id) | ||
} | ||
|
||
func (t *ticker) Reset(d time.Duration) { | ||
atomic.StoreInt64(&t.dur, int64(d)) | ||
t.time.resetTimer(d, t.id, t.ch) | ||
t.time.reset(d, t.id, t.do, &t.dur) | ||
} | ||
|
||
// do is the ticker’s moment callback. It sends the now time to the underlying | ||
// channel and plans a new moment for the next tick. Note that do runs under | ||
// Time’s lock. | ||
func (t *ticker) do(now time.Time) { | ||
t.ch <- now | ||
|
||
// It is safe to mutate ID without a lock since at most one moment | ||
// exists for the given ticker and moments run under the Time’s lock. | ||
t.time.resetUnlocked(t.dur, t.id, t.do, nil) | ||
|
||
// Ticker used to create a new moment for each tick and that would close | ||
// the observe channel. Maintain backwards compatibility for users that | ||
// may rely on this behavior. | ||
t.time.observeUnlocked() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.