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 #6 from tdakkota/feature/add-ticker-and-timer
Add Timer and Ticker implementation
- Loading branch information
Showing
6 changed files
with
256 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= | ||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package neo | ||
|
||
import "time" | ||
|
||
type moment struct { | ||
when time.Time | ||
do func(time time.Time) | ||
} | ||
|
||
type moments []moment | ||
|
||
func (m moments) do(t time.Time) { | ||
for _, doer := range m { | ||
doer.do(t) | ||
} | ||
} | ||
|
||
func (m moments) Len() int { | ||
return len(m) | ||
} | ||
|
||
func (m moments) Less(i, j int) bool { | ||
return m[i].when.Before(m[j].when) | ||
} | ||
|
||
func (m moments) Swap(i, j int) { | ||
m[i], m[j] = m[j], m[i] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package neo | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type ticker struct { | ||
time *Time | ||
ch chan time.Time | ||
id int | ||
dur int64 | ||
} | ||
|
||
func (t *ticker) C() <-chan time.Time { | ||
return t.ch | ||
} | ||
|
||
func (t *ticker) Stop() { | ||
t.time.stopTimer(t.id) | ||
} | ||
|
||
func (t *ticker) Reset(d time.Duration) { | ||
atomic.StoreInt64(&t.dur, int64(d)) | ||
t.time.resetTimer(d, t.id, t.ch) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package neo | ||
|
||
import "time" | ||
|
||
type timer struct { | ||
time *Time | ||
ch chan time.Time | ||
id int | ||
} | ||
|
||
func (t timer) C() <-chan time.Time { | ||
return t.ch | ||
} | ||
|
||
func (t timer) Stop() bool { | ||
return t.time.stopTimer(t.id) | ||
} | ||
|
||
func (t timer) Reset(d time.Duration) { | ||
t.time.resetTimer(d, t.id, t.ch) | ||
} |